Skip to content

Commit

Permalink
new worksheet
Browse files Browse the repository at this point in the history
  • Loading branch information
clauswilke committed Jul 26, 2024
1 parent 5662916 commit f2749e7
Show file tree
Hide file tree
Showing 72 changed files with 7,964 additions and 17 deletions.
179 changes: 179 additions & 0 deletions docs/materials/positconf2024-legends.qmd
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)
)
```
3 changes: 2 additions & 1 deletion docs/materials/positconf2024-pattern-fill.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -458,10 +458,11 @@ pet_ownership |>
theme_minimal_vgrid(rel_small = 1)
```

Unfortunately this is not quite what we wanted. The images are centered within the bar rather than aligned to one side. There doesn't currently seem to be a way to fix this, so we may have to go back to ggpattern.
Unfortunately this is not quite what we wanted. The images are centered within the bar rather than aligned to one side. There doesn't currently seem to be a way to fix this, so we may have to go back to ggpattern (see exercises).

## Exercises

1. Create a version of the pet ownership isotype plot where bars run vertical instead of horizontal.
2. Recreate the pet ownership isotype plot with ggpattern.

##
Loading

0 comments on commit f2749e7

Please sign in to comment.