ggplot2
After today’s session you will be able to:
ggplot2
graphsggplot2
graph aesthetics and customize labels / colors
Two main options for data viz in R:
ggplot2
ggplot2
packageTwo main options for data viz in R:
ggplot2
ggplot2
ggplot
aes
ggplot2
ggplot2
, make a graph with the minnow data where:
ggplot2
ggplot2
needs you to specify your geometry!
geom_...
geom_bar
, geom_point
, etc.
+
to add geometries to a plot
# Make a simple `ggplot2` plot
ggplot(data = my_df, mapping = aes(x = x_var, y = y_var)) +
# Make it a scatterplot
geom_point()
ggplot2
+
to the end of the linegeom_point()
geom_point()
to geom_boxplot()
ggplot
and aes
functions
geom_boxplot
and geom_point
+
after whichever you put first, then put the other
geom_boxplot
is first?
geom_point
is first?See how points are “behind” boxplots on the left?
aes
labs
function!
labs
has arguments x
and y
that expect characters to put as titles
labs
to do do the following:
color
or fill
aesthetics
# Make a plot where the color and y-axis are mapped to the same variable
ggplot(data = my_df, mapping = aes(x = x_var, y = y_var, color = y_var)) +
# Make it a scatterplot
geom_point() +
# Add custom axis labels
labs(x = "Custom X Label", y = "Custom Y Label")
aes
call at the top?
color
to fill
. Now what does the plot look like?color = species
fill = species
color
and fill
?
scale_fill_manual()
or scale_color_manual()
values
name 1 name 2 name 3
"entry 1" "entry 2" "entry 3"
# Make a plot where the color and y-axis are mapped to the same variable
ggplot(data = my_df, mapping = aes(x = x_var, y = y_var, color = y_var)) +
# Make it a scatterplot
geom_point() +
# Add custom axis labels
labs(x = "Custom X Label", y = "Custom Y Label") +
# Customize colors
scale_color_manual(values = c("name 1" = "#00FF00", "name 2" = "#FF0000", "name 3" = "#0000FF"))