ggplot2 was developed by Hadley Wickham and is based on Leland Wilkinson’s Grammar of Graphics.
ggplot2 emphasizes clarity, flexibility, and the use of the grammar of graphics philosophy.
2024
ggplot2 was developed by Hadley Wickham and is based on Leland Wilkinson’s Grammar of Graphics.
ggplot2 emphasizes clarity, flexibility, and the use of the grammar of graphics philosophy.
Grammar of Graphics: A system for describing and building graphs.
ggplot2 allows for the creation of complex plots from simple components.
The emphasis is on layering components to build up to the visualization you need.
Aesthetics define how your data is mapped to visual properties (e.g., x, y , color).
Geoms represent geometric shapes that represent data (e.g., points, lines, bars).
Scales fine-tune how aesthetics relationships arevisually encoded (e.g., color maps).
Themes control the non-data parts of the plot (e.g., plot background, grid lines, and text elements.)
Aesthetics define how your data is mapped to visual properties.
Common aesthetics include x
, y
, color
, fill
, size
, and shape
.
# `aes` used to map `displ` to x coordinates and `hwy` to y # coordinates library(ggplot2) ggplot(mpg, aes(x=displ, y=hwy)) + geom_point()
# `geom_smooth()` and `geom_point` used to represent x and y ggplot(mpg, aes(x=displ, y=hwy)) + geom_point()
# `scale_color_manual` used to control how levels of `class` # are mapped to colors. ggplot(mpg, aes(x=displ, y=hwy, color=class)) + geom_point() + scale_color_manual(values=c("compact"="blue", "suv"="red", "pickup"="green", "midsize"="purple", "minivan"="orange", "2seater"="brown", "subcompact"="pink"))
# `theme_minimal` is used set a minimal appearance to the # overall plot ggplot(mpg, aes(x=displ, y=hwy)) + geom_point() + theme_minimal()
Aesthetics define how your data is mapped to visual properties (e.g., x, y , color).
Geoms represent geometric shapes that represent data (e.g., points, lines, bars).
Scales fine-tune how aesthetics relationships arevisually encoded (e.g., color maps).
Themes control the non-data parts of the plot (e.g., plot background, grid lines, and text elements.)
Identify the major components of the following ggplot2 plot.
ggplot(mpg, aes(x=displ, y=hwy, color=class)) + geom_point() + scale_color_manual(values=c("compact"="blue", "suv"="red", "pickup"="green", "midsize"="purple", "minivan"="orange", "2seater"="brown", "subcompact"="pink")) + theme_minimal()
aes(x=displ, y=hwy, color=class)
specifies the aesthetic.
geom_point()
specifies the geometric object.
scale_color_manual
specifies the color mapping.
theme_minimal
specifies the theme.