Grammar of graphics

Jeff Stevens

2023-03-31

Introduction

Set-up

Aesthetics

Map data to aesthetic/visual properties

Map size aesthetic to data

mpg |> ggplot(aes(x = displ, y = hwy, size = class)) + geom_point()

Apply size to all points

mpg |> ggplot(aes(x = displ, y = hwy)) + geom_point(size = 1)

Map transparency aesthetic to data

mpg |> ggplot(aes(x = displ, y = hwy, alpha = class)) + geom_point()

Apply transparency to all points

mpg |> ggplot(aes(x = displ, y = hwy)) + geom_point(alpha = 0.25)

Map shape aesthetic to data

mpg |> ggplot(aes(x = displ, y = hwy, shape = class)) + geom_point()

Tattoo figure 3.1 to forearm

plot(0:25, 0:25, pch = 0:25)

Apply shape to all points

mpg |> ggplot(aes(x = displ, y = hwy)) + geom_point(shape = 5)

Map color aesthetic to data

mpg |> ggplot(aes(x = displ, y = hwy, color = class)) + geom_point()

Assign color to data but not mapped as aesthetic

mpg |> ggplot(aes(x = displ, y = hwy)) + geom_point(color = “blue”)

What happens if we put it in the aesthetic?

mpg |> ggplot(aes(x = displ, y = hwy, color = “blue”)) + geom_point()

The color argument applies to points, lines, text, and borders

either mapped to data mpg |> ggplot(aes(x = class, color = class)) + geom_bar(show.legend = FALSE)

or uniformly mpg |> ggplot(aes(x = class)) + geom_bar(color = “blue”)

The fill arugment applies to filled areas

either mapped to data mpg |> ggplot(aes(x = class, fill = class)) + geom_bar(show.legend = FALSE)

or uniformly mpg |> ggplot(aes(x = class)) + geom_bar(fill = “blue”)

Lines can be calculated by groups

mpg |> ggplot(aes(x = displ, y = hwy, group = drv)) + geom_smooth()

Lines have additional aesthetic properties like linetype

mpg |> ggplot(aes(x = displ, y = hwy, linetype = drv)) + geom_smooth()

Line aesthetics can be applied by groups

mpg |> ggplot(aes(x = displ, y = hwy, color = drv)) + geom_smooth(show.legend = FALSE)

We can specify geom-specific aesthetics like only to lines

mpg |> ggplot(aes(x = displ, y = hwy)) + geom_point() + geom_smooth(aes(linetype = drv))

Or only to points

mpg |> ggplot(aes(x = displ, y = hwy)) + geom_point(aes(color = class)) + geom_smooth()

Or we can apply aesthetics across geoms

mpg |> ggplot(aes(x = displ, y = hwy, color = drv)) + geom_point() + geom_smooth(aes(linetype = drv))

Like lines, the bands have aesthetics, too

mpg |> ggplot(aes(x = displ, y = hwy)) + geom_point() + geom_smooth(aes(color = drv, fill = drv), alpha = 0.5)

Statistical transformations

Statistical transformations

ggplot can calculate statistics on the fly, and many geoms have some underlying statistical transformation diamonds

Bar plots count up observations of variable types with stat_count()

diamonds |> ggplot(aes(x = cut)) + geom_bar()

Summarize data with stat_summary()

diamonds |> ggplot(aes(x = cut, y = depth)) + geom_point()

Plot mean and standard error

diamonds |> ggplot(aes(x = cut, y = depth)) + stat_summary()

Plot median and range

diamonds |> ggplot(aes(x = cut, y = depth)) + stat_summary(fun.min = min, fun.max = max, fun = median)

Plot mean and 95% CI

diamonds |> ggplot(aes(x = cut, y = depth)) + stat_summary(fun.data = mean_cl_normal)

Facets

Facets

Coloring by group can be difficult to visualize with many groups mpg |> ggplot(aes(x = displ, y = hwy, color = class)) + geom_point()

Faceting pulls out groups into separate panels

mpg |> ggplot(aes(x = displ, y = hwy)) + geom_point() + facet_wrap(~ class)

More on facets later

Let’s code!

Iteration [Rmd]