-
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
e870790
commit 37f2358
Showing
8 changed files
with
313 additions
and
36 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Binary file added
BIN
+53.4 KB
docs/materials/positconf2024-pattern-fill_files/figure-html/unnamed-chunk-15-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,108 @@ | ||
--- | ||
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", | ||
) | ||
) | ||
``` | ||
|
||
## 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) | ||
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") | ||
``` | ||
|
||
## 2. Legend placement |
Oops, something went wrong.