-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5662916
commit f2749e7
Showing
72 changed files
with
7,964 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
--- | ||
title: "Effective Data Visualization with ggplot2" | ||
subtitle: "Sprucing up your legends" | ||
format: html | ||
editor: visual | ||
--- | ||
|
||
## Required packages | ||
|
||
Install the required packages: | ||
|
||
```{r} | ||
#| eval = FALSE | ||
# Run this command to install the required packages. | ||
# You need to do this only once. | ||
install.packages( | ||
c( | ||
"tidyverse", "colorspace", "cowplot" | ||
) | ||
) | ||
``` | ||
|
||
## 1. Legend order | ||
|
||
By default, ggplot places legend entries in alphabetical order, and that is rarely what we want. See this example: | ||
|
||
```{r} | ||
#| message = FALSE, | ||
#| warning = FALSE | ||
library(tidyverse) | ||
library(colorspace) | ||
library(cowplot) # for theme functions | ||
tech_stocks <- read_csv("https://wilkelab.org/dataviz_shortcourse/datasets/tech_stocks.csv") |> | ||
mutate(date = ymd(date)) |> | ||
select(company, date, price_indexed) | ||
ggplot(tech_stocks) + | ||
aes(x = date, y = price_indexed) + | ||
geom_line(aes(color = company), na.rm = TRUE) + | ||
scale_x_date( | ||
limits = c( | ||
ymd("2012-06-01"), | ||
ymd("2017-05-31") | ||
), | ||
expand = c(0, 0) | ||
) + | ||
scale_y_continuous( | ||
limits = c(0, 560), | ||
expand = c(0, 0) | ||
) | ||
``` | ||
|
||
The visual order of the lines is Facebook, Alphabet, Microsoft, Apple, and we should match it by reodering the categorical variable. | ||
|
||
```{r} | ||
tech_stocks |> | ||
mutate( | ||
company = fct_relevel( | ||
company, | ||
"Facebook", "Alphabet", "Microsoft", "Apple" | ||
) | ||
) |> | ||
ggplot() + | ||
aes(x = date, y = price_indexed) + | ||
geom_line(aes(color = company), na.rm = TRUE) + | ||
scale_x_date( | ||
limits = c( | ||
ymd("2012-06-01"), | ||
ymd("2017-05-31") | ||
), | ||
expand = c(0, 0) | ||
) + | ||
scale_y_continuous( | ||
limits = c(0, 560), | ||
expand = c(0, 0) | ||
) | ||
``` | ||
|
||
This is improved, but we can do better. We can get rid of the legend entirely, by adding a secondary axis. | ||
|
||
```{r} | ||
tech_stocks_last <- tech_stocks |> | ||
filter(date == max(date)) | ||
ggplot(tech_stocks) + | ||
aes(x = date, y = price_indexed) + | ||
geom_line(aes(color = company), na.rm = TRUE) + | ||
scale_x_date( | ||
limits = c( | ||
ymd("2012-06-01"), | ||
ymd("2017-05-31") | ||
), | ||
expand = c(0, 0) | ||
) + | ||
scale_y_continuous( | ||
limits = c(0, 560), | ||
expand = c(0, 0), | ||
sec.axis = dup_axis( | ||
breaks = tech_stocks_last$price_indexed, | ||
labels = tech_stocks_last$company, | ||
name = NULL | ||
) | ||
) + | ||
guides(color = "none") | ||
``` | ||
|
||
## Exercises | ||
|
||
```{r} | ||
#| message = FALSE | ||
preprints <- read_csv("https://wilkelab.org/dataviz_shortcourse/datasets/preprints.csv") |> | ||
filter(archive %in% c("bioRxiv", "arXiv q-bio")) %>% | ||
filter(count > 0) | ||
``` | ||
|
||
```{r} | ||
preprints_final <- filter(preprints, date == max(date)) | ||
ggplot(preprints) + | ||
aes(date, count, color = archive) + | ||
geom_line(linewidth = 0.75) + | ||
scale_y_log10( | ||
limits = c(29, 1600), | ||
breaks = c(30, 100, 300, 1000), | ||
expand = c(0, 0), | ||
name = "preprints / month", | ||
sec.axis = dup_axis( | ||
breaks = preprints_final$count, | ||
labels = preprints_final$archive, | ||
name = NULL | ||
) | ||
) + | ||
scale_x_date(name = "year", expand = c(0, 0)) + | ||
scale_color_manual(values = c("#D55E00", "#0072B2"), guide = "none") + | ||
theme_minimal_grid() + | ||
theme( | ||
axis.ticks.y.right = element_blank(), | ||
axis.text.y.right = element_text( | ||
size = 14, | ||
margin = margin(0, 0, 0, 0) | ||
) | ||
) | ||
``` | ||
|
||
## 2. Legend placement | ||
|
||
```{r} | ||
ggplot(mtcars) + | ||
aes(disp, mpg, fill = hp, shape = factor(cyl), size = wt) + | ||
geom_point(color = "white") + | ||
scale_shape_manual(values = c(23, 24, 21), name = "cylinders") + | ||
scale_fill_continuous_sequential( | ||
palette = "Emrld", name = "power (hp)", | ||
breaks = c(100, 200, 300), | ||
rev = FALSE | ||
) + | ||
xlab("displacement (cu. in.)") + | ||
ylab("fuel efficiency (mpg)") + | ||
guides( | ||
shape = guide_legend(override.aes = list(size = 4, fill = "#329D84")), | ||
size = guide_legend(override.aes = list(shape = 21, fill = "#329D84"), | ||
title = "weight (1000 lbs)") | ||
) + | ||
theme_half_open() + | ||
background_grid() + | ||
theme( | ||
#legend.title = element_text(size = 12), | ||
legend.box.background = element_rect(fill = "white", color = "white"), | ||
legend.position = "inside", | ||
legend.direction = "vertical", | ||
legend.box = "horizontal", | ||
legend.justification = "center", | ||
legend.box.margin = margin(7, 7, 7, 7) | ||
) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.