<jupyter_start><jupyter_text>Scatter Plot in R using ggplot2 (with Example)When the explanatory analysis is achieved, the data scientist has to consider the capacity of the reader to understand the underlying concepts and models.His results should be presented in a format that all stakeholders can understand. One of the best methods to communicate the results is through a graph.Graphs are an incredible tool to simplify complex analysis.<jupyter_code>library(ggplot2)
ggplot(mtcars, aes(x = drat, y = mpg)) + geom_point()
ggplot(mtcars, aes(x = mpg, y = drat)) +
    geom_point(aes(color = factor(gear)))
ggplot(mtcars, aes(x = log(mpg), y = log(drat))) +
    geom_point(aes(color = factor(gear)))
my_graph <- ggplot(mtcars, aes(x = log(mpg), y = log(drat))) +
    geom_point(aes(color = factor(gear))) +
    stat_smooth(method = "lm",
        col = "#C42005",
        se = FALSE,
        size = 2)
my_graph
my_graph +
    labs(
        title = "Plot Mile per hours and drat, in log"
         )
A <-2010
paste("The first year is", A)
B <-2018
paste("The first year is", A, "and the last year is", B)
mean_mpg <- mean(mtcars$mpg)
my_graph + labs(
    title = paste("Plot Mile per hours and drat, in log. Average mpg is", mean_mpg)
)
my_graph +
    labs(
        title =
        "Relation between Mile per hours and drat",
        subtitle =
        "Relationship break down by gear class",
        caption = "Authors By REDDYPRASDE"
    )
my_graph +
    labs(
        x = "Drat definition",
        y = "Mile per hours",
        color = "Gear",
        title = "Relation between Mile per hours and drat",
        subtitle = "Relationship break down by gear class",
        caption = "Authors own Reddy"
    )
seq(0, 12,4)<jupyter_output><empty_output><jupyter_text>1. theme_bw()2. theme_light()3. theme_classis()4. theme_linedraw()5. theme_dark()6. theme_minimal()7. theme_gray()8. theme_void()<jupyter_code>my_graph +
    theme_bw() +
    labs(
        x = "Drat definition, in log",
        y = "Mile per hours, in log",
        color = "Gear",
        title = "Relation between Mile per hours and drat",
        subtitle = "Relationship break down by gear class",
        caption = "Authors own computation"
    )
directory <-getwd()
directory
my_graph +
    theme_classic() +
    labs(
        x = "Drat definition, in log",
        y = "Mile per hours, in log",
        color = "Gear",
        title = "Relation between Mile per hours and drat",
        subtitle = "Relationship break down by gear class",
        caption = "Authors own computation"
    )
ggsave("my_fantastic_plot.png")<jupyter_output>Saving 6.67 x 6.67 in image<jupyter_text>SummaryYou can summarize the arguments to create a scatter plot in the table below:|Objective|Code||---------|----||Basic scatter plot|ggplot(df, aes(x = x1, y = y)) + geom_point()|Scatter plot with color group|ggplot(df, aes(x = x1, y = y)) + geom_point(aes(color = factor(x1)) + stat_smooth(method = "lm")|Add fitted values|ggplot(df, aes(x = x1, y = y)) + geom_point(aes(color = factor(x1))|Add title|ggplot(df, aes(x = x1, y = y)) + geom_point() + labs(title = paste("Hello Guru99"))|Add subtitle|ggplot(df, aes(x = x1, y = y)) + geom_point() + labs(subtitle = paste("Hello Guru99"))|Rename x|ggplot(df, aes(x = x1, y = y)) + geom_point() + labs(x = "X1")|Rename y|ggplot(df, aes(x = x1, y = y)) + geom_point() + labs(y = "y1")|Control the scale|ggplot(df, aes(x = x1, y = y)) + geom_point() + scale_y_continuous(breaks = seq(10, 35, by = 10)) + scale_x_continuous(breaks = seq(2, 5, by = 1)|Create logs|ggplot(df, aes(x =log(x1), y = log(y))) + geom_point()|Theme|ggplot(df, aes(x = x1, y = y)) + geom_point() + theme_classic()|Save|ggsave("my_fantastic_plot.png")  Box Plot<jupyter_code>library(dplyr)
library(ggplot2)
# Step 1
data_air <- airquality
data_air
data_air <- airquality %>%
#Step 2
select(-c(Solar.R, Temp)) %>%

#Step 3
mutate(Month = factor(Month, order = TRUE, labels = c("May", "June", "July", "August", "September")), 
       
#Step 4 
day_cat = factor(ifelse(Day < 10, "Begin", ifelse(Day < 20, "Middle", "End"))))
glimpse(data_air)
# Step 5
data_air_nona <-data_air %>% na.omit()
head(data_air_nona)
# Store the graph
box_plot <- ggplot(data_air_nona, aes(x = Month, y = Ozone))
# Add the geometric object box plot
box_plot +
    geom_boxplot()
box_plot +
  geom_boxplot()+
  coord_flip()
box_plot +
    geom_boxplot(outlier.colour = "red",
        outlier.shape = 6,
        outlier.size = 4) +
    theme_classic()
box_plot +
    geom_boxplot() +
    stat_summary(fun.y = mean,
        geom = "point",
        size = 3,
        color = "steelblue") +
    theme_classic()
box_plot +
    geom_boxplot() +
    geom_dotplot(binaxis = 'y',
        dotsize = 0.5,
        stackdir = 'center') +
    theme_classic()
ggplot(data_air_nona, aes(x = Month, y = Ozone, color = Month)) +
    geom_boxplot() +
    theme_classic()
ggplot(data_air_nona, aes(Month, Ozone)) +
    geom_boxplot(aes(fill = day_cat)) +
    theme_classic()
box_plot +
    geom_boxplot() +
    geom_jitter(shape = 15,
        color = "steelblue",
        position = position_jitter(width = 0.21)) +
    theme_classic()
box_plot +
    geom_boxplot() +
    geom_point(shape = 5,
        color = "steelblue") +
    theme_classic()
box_plot +
    geom_boxplot(notch = TRUE) +
    theme_classic()<jupyter_output>notch went outside hinges. Try setting notch=FALSE.<jupyter_text>summarizeWe can summarize the different types of box plot in the table below:|Objective|Code||---------|-----||Basic box plot|ggplot(df, aes( x = x1, y =y)) + geom_boxplot()|flip the side|ggplot(df, aes( x = x1, y =y)) + geom_boxplot() + coord_flip()|Notched box plot|ggplot(df, aes( x = x1, y =y)) + geom_boxplot(notch=TRUE)|Box plot with jittered dots|ggplot(df, aes( x = x1, y =y)) + geom_boxplot() + geom_jitter(position = position_jitter(0.21))  Bar Chart<jupyter_code>library(ggplot2)
# Most basic bar chart
ggplot(mtcars, aes(x = factor(cyl))) +
    geom_bar()
# Change the color of the bars
ggplot(mtcars, aes(x = factor(cyl))) +
    geom_bar(fill = "coral") +
    theme_classic()
# Change intensity
ggplot(mtcars,
        aes(factor(cyl))) +
    geom_bar(fill = "coral",
        alpha = 0.2) +
    theme_classic()
# Color by group
ggplot(mtcars, aes(factor(cyl),
        fill = factor(cyl))) +
    geom_bar()
data_histogram <- mtcars %>%
mutate(cyl = factor(cyl)) %>%
group_by(cyl) %>%
summarize(mean_mpg = round(mean(mpg), 2))
ggplot(data_histogram, aes(x = cyl, y = mean_mpg)) +
    geom_bar(stat = "identity")
ggplot(data_histogram, aes(x = cyl, y = mean_mpg)) +
    geom_bar(stat = "identity") +
    coord_flip()
ggplot(data_histogram, aes(x = cyl, y = mean_mpg, fill = cyl)) +
    geom_bar(stat = "identity") +
    coord_flip() +
    theme_classic()
graph <- ggplot(data_histogram ,aes(x=cyl, y=mean_mpg, fill=cyl))+
        geom_bar(stat = 'identity',
                width=0.5)+
        coord_flip()+
theme_classic()
graph
graph+geom_text(aes(label = mean_mpg),
        hjust = 1.5,
        color = "Blue",
        size = 5) +
    theme_classic()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1461354357313811, 'fraction_numerical': 0.013796987188511897, 'mean_word_length': 3.548015364916773, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '443125', 'license': 'MIT', 'n_tokens_mistral': 2763, 'n_tokens_neox': 2596, 'n_words': 666, 'path': '22.Data Visualization.ipynb', 'repo_name': 'reddyprasade/Data-Science-With-R', 'size': '7103'}