The transferable skills from ggplot2 are not the idiosyncrasies of plotting syntax, but a powerful way of thinking about visualization, as a way of mapping between variables and the visual properties of geometric objects that you can perceive.
– Hadley Wickham
Why ggplot2?
You have probably heard of it but why use it?
Once we understand the “grammar” making figures becomes a lot easier
Tons of organizations use it
Flexibility
Tons ways to customize appearance
Lots of functions
Lots of extensions
Reproducibility
Doesn’t require you to remember each input from a drop down menu
Defaults to universally usable formats
Replaces itself automatically in your directory
The Grammar of Graphics
Grammar
“Good grammar is just the first step of creating a good sentence”
How is the data related to the figure on the right?
Building the Plot
Body Weight of Penguins and Bill Length
Penguins
Species
Island
Building the Plot
Body Weight of Penguins and Bill Length
Penguins
Species
Island
Building the Plot
Body Weight of Penguins and Bill Length
Penguins
Species
Island
So How Did We go From?
This
To This
Making Plots
The Grammar
Component
Function
Explanation
Data
ggplot(data)
The raw data that you want to visualise.
Aesthetics
aes()
Aesthetic mappings between variables and visual properties.
Geometries
geom_*()
The geometric shapes representing the data.
Statistics
stat_*()
The statistical transformations applied to the data.
Scales
scale_*()
Maps between the data and the aesthetic dimensions.
Coordinate System
coord_*()
Maps data into the plane of the data rectangle.
Facets
facet_*()
The arrangement of the data into a grid of plots.
Visual Themes
theme() and theme_*()
The overall visual defaults of a plot.
Where do they go?
ggplot() +geom_point(data = penguins,aes(x = bill_length_mm, y = body_mass_g,shape = species, color = island),size =3)
Plotting Data
country
continent
year
lifeExp
pop
gdpPercap
Afghanistan
Asia
1952
28.801
8425333
779.4453
Afghanistan
Asia
1957
30.332
9240934
820.8530
Afghanistan
Asia
1962
31.997
10267083
853.1007
Afghanistan
Asia
1967
34.020
11537966
836.1971
Afghanistan
Asia
1972
36.088
13079460
739.9811
Afghanistan
Asia
1977
38.438
14880372
786.1134
Afghanistan
Asia
1982
39.854
12881816
978.0114
Afghanistan
Asia
1987
40.822
13867957
852.3959
Afghanistan
Asia
1992
41.674
16317921
649.3414
Afghanistan
Asia
1997
41.763
22227415
635.3414
Here is your shell script
## be sure you have done ## install.packages("gapminder")## library(gapminder)ggplot() +geom_point(data = gapminder, mapping =aes(x = gdpPercap, y = lifeExp))
Activity
Add color, size, alpha, and shape aesthetics to your graph.
Be bold be brave! Experiment!
What happens when you add more than one aesthetic?
ggplot(gapminder,aes(x = gdpPercap,y = lifeExp,color = continent)) +geom_point(alpha =0.5) +geom_smooth() +scale_x_log10() +labs(x ="GDP per cap",y ="Life Expectancy",title ="The Effect of GDP per cap on Life Expectancy")
Add viridis color scale
ggplot(gapminder,aes(x = gdpPercap,y = lifeExp,color = continent)) +geom_point(alpha =0.5) +geom_smooth() +scale_x_log10() +labs(x ="GDP per cap",y ="Life Expectanty",title ="The Effect of GDP per cap on Life Expectancy") +scale_color_viridis_d()
Add theme
ggplot(gapminder,aes(x = gdpPercap,y = lifeExp,color = continent)) +geom_point(alpha =0.5) +geom_smooth() +scale_x_log10() +labs(x ="GDP per cap",y ="Life Expectanty",title ="The Effect of GDP per cap on Life Expectancy") +scale_color_viridis_d() +theme_bw()
Facet by Continent
ggplot(gapminder,aes(x = gdpPercap,y = lifeExp,color = continent)) +geom_point(alpha =0.5) +geom_smooth() +scale_x_log10() +labs(x ="GDP per cap",y ="Life Expectanty",title ="The Effect of GDP per cap on Life Expectancy") +scale_color_viridis_d() +theme_bw() +facet_wrap(vars(continent))
Change Theme Options
ggplot(gapminder,aes(x = gdpPercap,y = lifeExp,color = continent)) +geom_point(alpha =0.5) +geom_smooth() +scale_x_log10() +labs(x ="GDP per cap",y ="Life Expectanty",title ="The Effect of GDP per cap on Life Expectancy") +scale_color_viridis_d() +theme_bw() +facet_wrap(vars(continent)) +theme(legend.position ="none")