library(here) # manage file pathslibrary(tidyverse) # your friend and minelibrary(socviz) # data and some useful functionslibrary(ggrepel) # Text and labelslibrary(colorspace) # luminance-balanced paletteslibrary(scales) # scale adjustments and enhancementslibrary(ggforce) # useful enhancements to ggplot
Piece by piece, Layer by layer
Build your plots a piece at a time
asasec <-as_tibble(asasec)asasec
# A tibble: 572 × 9
Section Sname Beginning Revenues Expenses Ending Journal Year Members
<fct> <fct> <int> <int> <int> <int> <fct> <int> <int>
1 Aging and the… Aging 12752 12104 12007 12849 No 2005 598
2 Alcohol, Drug… Alco… 11933 1144 400 12677 No 2005 301
3 Altruism and … Altr… 1139 1862 1875 1126 No 2005 NA
4 Animals and S… Anim… 473 820 1116 177 No 2005 209
5 Asia/Asian Am… Asia 9056 2116 1710 9462 No 2005 365
6 Body and Embo… Body 3408 1618 1920 3106 No 2005 NA
7 Children and … Chil… 3692 3653 3713 3632 No 2005 418
8 Coll Behavior… CBSM 8127 3470 2704 8893 No 2005 708
9 Communication… CITA… 17093 4800 4804 17089 No 2005 301
10 Community and… Comm… 26598 24883 23379 28102 Yes 2005 721
# ℹ 562 more rows
Build your plots a piece at a time
Build your plots a piece at a time
Build your plots a piece at a time
asasec
# A tibble: 572 × 9
Section Sname Beginning Revenues Expenses Ending Journal Year Members
<fct> <fct> <int> <int> <int> <int> <fct> <int> <int>
1 Aging and the… Aging 12752 12104 12007 12849 No 2005 598
2 Alcohol, Drug… Alco… 11933 1144 400 12677 No 2005 301
3 Altruism and … Altr… 1139 1862 1875 1126 No 2005 NA
4 Animals and S… Anim… 473 820 1116 177 No 2005 209
5 Asia/Asian Am… Asia 9056 2116 1710 9462 No 2005 365
6 Body and Embo… Body 3408 1618 1920 3106 No 2005 NA
7 Children and … Chil… 3692 3653 3713 3632 No 2005 418
8 Coll Behavior… CBSM 8127 3470 2704 8893 No 2005 708
9 Communication… CITA… 17093 4800 4804 17089 No 2005 301
10 Community and… Comm… 26598 24883 23379 28102 Yes 2005 721
# ℹ 562 more rows
Build your plots a piece at a time
asasec |>filter(Year ==2014)
# A tibble: 52 × 9
Section Sname Beginning Revenues Expenses Ending Journal Year Members
<fct> <fct> <int> <int> <int> <int> <fct> <int> <int>
1 Aging and the… Aging 12752 12104 12007 12849 No 2014 580
2 Alcohol, Drug… Alco… 11933 1144 400 12677 No 2014 173
3 Altruism and … Altr… 1139 1862 1875 1126 No 2014 318
4 Animals and S… Anim… 473 820 1116 177 No 2014 154
5 Asia/Asian Am… Asia 9056 2116 1710 9462 No 2014 336
6 Body and Embo… Body 3408 1618 1920 3106 No 2014 312
7 Children and … Chil… 3692 3653 3713 3632 No 2014 421
8 Coll Behavior… CBSM 8127 3470 2704 8893 No 2014 835
9 Communication… CITA… 17093 4800 4804 17089 No 2014 371
10 Community and… Comm… 26598 24883 23379 28102 Yes 2014 630
# ℹ 42 more rows
Scale functions control the display of the variables they map. So to change the colors for color or fill mappings, you adjust the corresponding scale_ function, not the theme() function.
ggplot has several color palettes built in. A variety of packages provide others.
You can specify scales manually
organdata
# A tibble: 238 × 21
country year donors pop pop_dens gdp gdp_lag health health_lag
<chr> <date> <dbl> <int> <dbl> <int> <int> <dbl> <dbl>
1 Australia NA NA 17065 0.220 16774 16591 1300 1224
2 Australia 1991-01-01 12.1 17284 0.223 17171 16774 1379 1300
3 Australia 1992-01-01 12.4 17495 0.226 17914 17171 1455 1379
4 Australia 1993-01-01 12.5 17667 0.228 18883 17914 1540 1455
5 Australia 1994-01-01 10.2 17855 0.231 19849 18883 1626 1540
6 Australia 1995-01-01 10.2 18072 0.233 21079 19849 1737 1626
7 Australia 1996-01-01 10.6 18311 0.237 21923 21079 1846 1737
8 Australia 1997-01-01 10.3 18518 0.239 22961 21923 1948 1846
9 Australia 1998-01-01 10.5 18711 0.242 24148 22961 2077 1948
10 Australia 1999-01-01 8.67 18926 0.244 25445 24148 2231 2077
# ℹ 228 more rows
# ℹ 12 more variables: pubhealth <dbl>, roads <dbl>, cerebvas <int>,
# assault <int>, external <int>, txp_pop <dbl>, world <chr>, opt <chr>,
# consent_law <chr>, consent_practice <chr>, consistent <chr>, ccode <chr>
## Brighter Blue and Redparty_colors <-c("royalblue1", "red2")ggplot(data =subset(county_data, flipped =="No"),mapping =aes(x = pop,y = black/100)) +geom_point(alpha =0.15, color ="gray30",size =rel(2)) +scale_x_log10(labels =label_comma()) +geom_point(data =subset(county_data, flipped =="Yes"),mapping =aes(x = pop, y = black/100,color = partywinner16),size =rel(2)) +geom_text_repel(data =subset(county_data, flipped =="Yes"& black >25),mapping =aes(x = pop,y = black/100, label = state,family ="Tenso Slide",face ="bold"), size =rel(3.5)) +scale_color_manual(values = party_colors) +scale_y_continuous(labels =label_percent()) +labs(color ="County flipped to ... ",x ="County Population (log scale)",y ="Percent Black Population",title ="Flipped counties, 2016",caption ="Counties in gray did not flip.")
We know how to build this
## Brighter Blue and Redparty_colors <-c("royalblue1", "red2")ggplot(data =subset(county_data, flipped =="No"),mapping =aes(x = pop,y = black/100)) +geom_point(alpha =0.15, color ="gray30",size =rel(2)) +scale_x_log10(labels =label_comma()) +geom_point(data =subset(county_data, flipped =="Yes"),mapping =aes(x = pop, y = black/100,color = partywinner16),size =rel(2)) +geom_text_repel(data =subset(county_data, flipped =="Yes"& black >25),mapping =aes(x = pop,y = black/100, label = state,family ="Tenso Slide",face ="bold"), size =rel(3.5)) +scale_color_manual(values = party_colors) +scale_y_continuous(labels =label_percent()) +labs(color ="County flipped to ... ",x ="County Population (log scale)",y ="Percent Black Population",title ="Flipped counties, 2016",caption ="Counties in gray did not flip.")
Leverage ggplot’s layered approach
Layer, Highlight, Repeat
Build from ideas to data
The relationship of interest
Build from ideas to data
Theory says …
Build from ideas to data
Data suggests …
Repeat to differentiate
Pointrange
Repeat to differentiate
Add a comparison group
Layer and repeat with facets
Compare across facets
Layer and repeat across facets
Layer to compare
Layer and repeat across facets
Layer to copmare
X-Ray Vision
Seeing through it
Themes
Themes …
are controlled by the theme() function
can be bundled into functions of their own, like theme_bw() or theme_minimal()
can be set for the duration of a file or project with theme_set()
make changes that are applied additively
and most importantly …
Thematic elements do not represent data directly
Make a plot
kjh_set_classic_theme(3)
p <- organdata |>drop_na(world) |>ggplot(mapping =aes(x = roads, y = donors, color = world)) +geom_point(size =3) +labs(x ="Road Deaths", y ="Procurement Rate",title ="By Welfare State Regime")p
Add a theme … theme_bw()
p +theme_bw()
Add a theme … theme_minimal()
p +theme_minimal()
Add a theme … theme_dark()
p +theme_dark()
Adjust with the theme() function
None of this directly touches the parts of the plot that are representing your data—i.e. the visual parts that are mapped to a variable, and thus have a scale. Adjusting those is the job of the scale_ and guide() functions.
E.g, legend.position can be "none", "left", "right", "bottom", or "top"
Hence, e.g., theme(legend.position = "top"), which we have seen several times. Similarly for e.g. legend.direction (can be “horizontal” or “vertical”).
It’s a component of the plot that might be styled in several ways.
E.g., The text on the axes, or the lines in the plot panel.
If the latter …
If adjusting a thematic element ask…
Where on the plot is it?
Is it part of an axis, part of the panel, the strip (facet title) box, or the legend? This will help you find the name of the thing you want to adjust.
E.g. “I want to adjust the text for the markings on the x-axis”
You want axis.ticks.x
E.g. “I want to adjust the styling of the main y-axis grid lines inside the plot”
You want panel.grid.major.y
If adjusting a thematic element, ask…
What kind of element is it?
Is it text, or a line, or a rectangle?
This will tell you what function to use to make the adjustment to the named element.
If it’s text, adjust the element with element_text()
If it’s a line, adjust it with element_line()
If it’s a rectangle, with element_rect()
If you want to fully turn off an element, use element_blank()
For example …
“I want to adjust the styling of the plot title”
The relevant element is plot.title.
It’s text.
Inside the theme function, adjust it with element_text().
For example …
p +theme(plot.title =element_text(size =rel(3),face ="bold", color ="orange"))
For example …
“I want to adjust y axis grid lines on the plot”
The relevant elements are panel.grid.major.y and panel.grid.minor.y.
These are lines.
Inside the theme function, adjust it with element_line().
For example …
p +theme(panel.grid.major.y =element_line(color ="red"),panel.grid.minor.y =element_line(color ="black", linetype ="dotted"))
Theggthemespackage
We made this earlier. Here it is in a default theme.
See how the full function call goes inside theme_set(), including the parentheses, because we are actually running that function to set all the elements.
Calling the object now draws the plot with the thematic elements added.
Theming a plot
theme_set(theme_economist())
Calling the object now draws the plot with the thematic elements added.