H. Sherry Zhang Department of Statistics and Data Sciences University of Texas at Austin
Get the repository for today: library(usethis) create_from_github("SDS322E-26FALL/0302-viz1", fork = FALSE)
Learning objectives:
Develop a fundamental understanding of the grammar of graphics as implemented in ggplot2:
ggplot(): initialize a plot
geom_point(aes(x = ..., y = ..., color = ...)): add geometries and aesthetic mappings
facet_wrap(): create small multiples
scale_color_brewer(): change color palette
theme_bw() and theme(): customize appearance
Most of the plots we will create today are bad 😞, but they help us to understand the components of a ggplot before we can modify them to make better plots 😃.
mtcars: relationship between miles per gallon (mpg) and displacement (disp)
Data visualization for communicating information
However, information can be mis-communicated if the graphic is not well-made.
some plots not aesthetically pleasing,
some can be misleading, and
some are not or partial not informative.
Data visualization for communicating information
Plot the life expectancy (lifeExp) over the years (year) for all countries in the gapminder dataset
😭 This is misleading because the lines almost look like flat.
Data visualization for communicating information
Plot the displacement (disp) for different car models in the mtcars dataset.
😿 This is not informative since it is difficult to tell how displacement relates to car models.
Data visualization for communicating information
Plot the same bar chart but with colors.
🙍 This is also not informative because the colors don’t add more information to the plot and it is arguably aesthetically pleasing (or we may say it is dazzling).
Data visualization for communicating information
Plot the same bar chart but with colors representing the number of cylinders (cyl).
😃 This is informative because we can learn from the plot that larger cylinders tend to have larger displacements.
Why ggplot2?
ggplot2 is a package for data visualization based on The Grammars of Graphics by Leland Wilkinson.
Originally written by Hadley Wickham (part of his PhD dissertation), now maintained by Posit/RStudio.
Main references:
ggplot2: Elegant Graphics for Data Analysis (3e) by Hadley Wickham, Danielle Navarro, and Thomas Lin Pedersen. https://ggplot2-book.org/
Geometry is actually the most complicated among all the ggplot components. You shouldn’t try to memorize all these geometries.
Think about the type of data you have (categorical, continuous, time series, spatial, etc) and the information you would like to communicate (comparison, distribution, relationship, etc) and then decide which geometry to use.
Can you color the points by continent?
Translate to ggplot language: continent mapped to color aesthetic
gapminder |>ggplot() +geom_point(aes(x = lifeExp, y = gdpPercap, color = continent))
How many aesthetics are there?
A handful: x, y, color, fill, size, shape, alpha, linetype, group, label, …
color: the outline or border of a geometry
fill: the interior fill of a geometry
alpha: the transparency of a geometry
Each geom_*() has its required aesthetics and optional aesthetics.
geom_point() requires x and y and understand color, …
geom_segment() requires x, y, xend or yend and understand color, …
Do you know how to find out what is the required aesthetics and optional aesthetics for each geometry?
Can we use small multiples for continents?
Translate to ggplot language: facet by continent
gapminder |>ggplot() +geom_point(aes(x = lifeExp, y = gdpPercap, color = continent)) +facet_wrap(vars(continent))
How many facets are there?
You will most likely only interact with two facets: 1) facet_wrap(vars(...), ...) for one variable and 2) facet_grid(... ~ ..., ...) for two variables.
But there are other fancy facets in the wild (ggplot2 extensions):
ggh4x::facet_nested()
geofacet::facet_geo()
Can we use a different color palette?
Translate to ggplot language:
use scale_[color/fill]_[...](palette = "...") to change the color palette
gapminder |>ggplot() +geom_point(aes(x = lifeExp, y = gdpPercap, color = continent)) +facet_wrap(vars(continent)) +scale_color_brewer(palette ="Set1")