Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
clauswilke committed Jul 25, 2024
1 parent e870790 commit 37f2358
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 36 deletions.
112 changes: 98 additions & 14 deletions docs/materials/positconf2024-pattern-fill.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/materials/positconf2024-pattern-fill.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Install the required packages:
install.packages(
c(
"tidyverse", "cowplot", "sf", "ragg",
"palmerpenguins", "magick"
"palmerpenguins", "magick", "ggpattern"
)
)
```
Expand Down Expand Up @@ -462,6 +462,6 @@ Unfortunately this is not quite what we wanted. The images are centered within t

## Exercises

1. Some exercise here...
1. Create a version of the pet ownership isotype plot where bars run vertical instead of horizontal.

##
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions docs/search.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@
"description": "A bootcamp-style course on data visualization",
"author": [],
"contents": "\nThis is the home page for a dataviz shortcourse by Claus O. Wilke. The course is an abbreviated version of a semester-long course taught at The University of Texas at Austin. Many of the materials presented are taken from the book Fundamentals of Data Visualization.\n\nReuse\nText and figures are licensed under Creative Commons Attribution CC BY 4.0. Any computer code (R, HTML, CSS, etc.) in slides and worksheets, including in slide and worksheet sources, is also licensed under MIT. Note that figures in slides may be pulled in from external sources and may be licensed under different terms. For such images, image credits are available in the slide notes, accessible via pressing the letter ‘p’.\n\n\n\n",
"last_modified": "2024-07-25T18:25:05+02:00"
"last_modified": "2024-07-25T18:38:38+02:00"
},
{
"path": "lessons.html",
"title": "Lessons",
"description": "",
"author": [],
"contents": "\n\nContents\nEffective Visual Communication\nFrom Data to Visualisation 1\nFrom Data to Visualisation 2\nPrinciples of Figure Design\nReuse\n\nEffective Visual Communication\nThis lecture covers three core concepts of visual communication. First, I will discuss how to engage your audience by telling a story. Second, I will provide high-level guidance for designing visualizations that make a clear point. Third, I will describe key design principles that ensure your figures will be readable by people with vision impairments, and specifically impairments of color perception.\n\nMaterials:\n\nSlides\nFrom Data to Visualisation 1\nVisualizing amounts; visualizing distributions; visualizing associations and trends\n\nMaterials:\n\nSlides\nFrom Data to Visualisation 2\nVisualizing uncertainty; visualizing geospatial data\n\nMaterials:\n\nSlides\nPrinciples of Figure Design\nColors; compound figures; infographics\n\nMaterials:\n\nSlides\nReuse\nText and figures are licensed under Creative Commons Attribution CC BY 4.0. Any computer code (R, HTML, CSS, etc.) in slides and worksheets, including in slide and worksheet sources, is also licensed under MIT. Note that figures in slides may be pulled in from external sources and may be licensed under different terms. For such images, image credits are available in the slide notes, accessible via pressing the letter ‘p’.\n\n\n\n",
"last_modified": "2024-07-25T18:25:05+02:00"
"last_modified": "2024-07-25T18:38:39+02:00"
},
{
"path": "workshops.html",
"title": "Workshops",
"description": "",
"author": [],
"contents": "\n\nContents\nReuse\n\nMaterials for past and present workshops. Many workshop materials are based on content from my semester-long data visualization course at UT Austin.\n2024-08-12: Effective Data Visualization with ggplot2\nAll workshop materials will be posted here.\n2024-06-24: Visualizing Uncertainty and Trends\nKeynote, June 24 2024: slides\nWorkshop, June 27 2024:\nQuarto notebook\nHTML\nHTML with solutions\n\n2024-06-20: Effective Visual Communication with R\nWorkshop in support of Ukraine. To work through all the examples provided, you will need the following R packages: tidyverse, cowplot, ggiraph, sf, patchwork\nSlides Part 1\nSlides Part 2\nSlides Part 2, without interactivity (Some browsers crash when there are too many interactive examples in one slide set.)\n2024-05-28: Effective Visual Communication\nSlides Part 1 (telling a story, making a point, making figures accessible)\nSlides Part 2 (figure composition and context, text annotations, overplotting)\nReuse\nText and figures are licensed under Creative Commons Attribution CC BY 4.0. Any computer code (R, HTML, CSS, etc.) in slides and worksheets, including in slide and worksheet sources, is also licensed under MIT. Note that figures in slides may be pulled in from external sources and may be licensed under different terms.\n\n\n\n",
"last_modified": "2024-07-25T18:25:06+02:00"
"last_modified": "2024-07-25T18:38:40+02:00"
}
],
"collections": []
Expand Down
108 changes: 108 additions & 0 deletions materials/positconf2024-legends.qmd
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
Loading

0 comments on commit 37f2358

Please sign in to comment.