Skip to content

Commit

Permalink
prepare figures for final production
Browse files Browse the repository at this point in the history
  • Loading branch information
clauswilke committed Feb 28, 2019
1 parent 4717fac commit 5cc449b
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 180 deletions.
2 changes: 1 addition & 1 deletion aesthetic_mapping.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ Figure \@ref(fig:temp-normals-vs-time) is a fairly standard visualization for a

(ref:four-locations-temps-by-month) Monthly normal mean temperatures for four locations in the U.S. Data source: NOAA

```{r four-locations-temps-by-month, fig.width = 8.5, fig.asp = .3, fig.cap = '(ref:four-locations-temps-by-month)'}
```{r four-locations-temps-by-month, fig.width = 5.5*6/4.2, fig.asp = .3, fig.cap = '(ref:four-locations-temps-by-month)'}
month_names <- c("01" = "Jan", "02" = "Feb", "03" = "Mar", "04" = "Apr", "05" = "May", "06" = "Jun",
"07" = "Jul", "08" = "Aug", "09" = "Sep", "10" = "Oct", "11" = "Nov", "12" = "Dec")
Expand Down
18 changes: 9 additions & 9 deletions boxplots_violins.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ We can address all four shortcomings of Figure \@ref(fig:lincoln-temp-points-err

(ref:boxplot-schematic) Anatomy of a boxplot. Shown are a cloud of points (left) and the corresponding boxplot (right). Only the *y* values of the points are visualized in the boxplot. The line in the middle of the boxplot represents the median, and the box encloses the middle 50% of the data. The top and bottom whiskers extend either to the maximum and minimum of the data or to the maximum or minimum that falls within 1.5 times the height of the box, whichever yields the shorter whisker. The distances of 1.5 times the height of the box in either direction are called the upper and the lower fences. Individual data points that fall beyond the fences are referred to as outliers and are usually showns as individual dots.

```{r boxplot-schematic, fig.cap = '(ref:boxplot-schematic)'}
```{r boxplot-schematic, fig.width = 5*6/4.2, fig.cap = '(ref:boxplot-schematic)'}
set.seed(3423)
y <- c(rnorm(100), 3.4)
s <- boxplot.stats(y)
Expand All @@ -66,15 +66,15 @@ df <- data.frame(y = c(s$stats, max(y)),
label = c("minimum", "first quartile", "median", "third quartile", "maximum within upper fence", "outlier"))
p_boxplot <- ggplot(data.frame(y), aes(x = 1, y = y)) + geom_boxplot(fill = 'grey90') +
geom_text(data = df, aes(x, y, label = label), hjust = 0,
size = 12/.pt, family = dviz_font_family) +
size = 14/.pt, family = dviz_font_family) +
scale_x_continuous(limits = c(0, 3.5), expand = c(0, 0)) +
scale_y_continuous(limits = c(-2.8, 3.5), expand = c(0, 0)) +
scale_y_continuous(limits = c(-2.55, 3.5), expand = c(0, 0)) +
theme_nothing()
p_points <- ggplot(data.frame(y), aes(x = 0, y = y)) +
geom_point(position = position_jitter(width = .4, height = 0, seed = 320)) +
scale_x_continuous(limits = c(-1.8, .4), expand = c(0, 0)) +
scale_y_continuous(limits = c(-2.8, 3.5), expand = c(0, 0)) +
scale_y_continuous(limits = c(-2.55, 3.5), expand = c(0, 0)) +
theme_nothing()
plot_grid(p_points, p_boxplot, rel_widths = c(.65, 1), nrow = 1)
Expand All @@ -100,7 +100,7 @@ Boxplots were invented by the statistician John Tukey in the early 1970s, and th

(ref:violin-schematic) Anatomy of a violin plot. Shown are a cloud of points (left) and the corresponding violin plot (right). Only the *y* values of the points are visualized in the violin plot. The width of the violin at a given *y* value represents the point density at that *y* value. Technically, a violin plot is a density estimate rotated by 90 degrees and then mirrored. Violins are therefore symmetric. Violins begin and end at the minimum and maximum data values, respectively. The thickest part of the violin corresponds to the highest point density in the dataset.

```{r violin-schematic, fig.cap = '(ref:violin-schematic)'}
```{r violin-schematic, fig.width = 5*6/4.2, fig.cap = '(ref:violin-schematic)'}
set.seed(3423)
y <- c(rnorm(100), 3.4)
d <- density(y)
Expand All @@ -109,15 +109,15 @@ df <- data.frame(y = c(min(y), d$x[which(d$y == max(d$y))], max(y)),
label = c("minimum data value", "maximum point density", "maximum data value"))
p_violin <- ggplot(data.frame(y), aes(x = 1, y = y)) + geom_violin(fill = 'grey90') +
geom_text(data = df, aes(x, y, label = label), hjust = 0,
size = 12/.pt, family = dviz_font_family) +
size = 14/.pt, family = dviz_font_family) +
scale_x_continuous(limits = c(0, 3.5), expand = c(0, 0)) +
scale_y_continuous(limits = c(-2.8, 3.5), expand = c(0, 0)) +
scale_y_continuous(limits = c(-2.55, 3.5), expand = c(0, 0)) +
theme_nothing()
p_points <- ggplot(data.frame(y), aes(x = 0, y = y)) +
geom_point(position = position_jitter(width = .4, height = 0, seed = 320)) +
scale_x_continuous(limits = c(-1.8, .4), expand = c(0, 0)) +
scale_y_continuous(limits = c(-2.8, 3.5), expand = c(0, 0)) +
scale_y_continuous(limits = c(-2.55, 3.5), expand = c(0, 0)) +
theme_nothing()
plot_grid(p_points, p_violin, rel_widths = c(.65, 1), nrow = 1)
Expand Down Expand Up @@ -281,7 +281,7 @@ Ridgeline plots also work well if we want to compare two trends over time. This

(ref:dw-nominate-ridgeline) Voting patterns in the U.S. House of Representatives have become increasingly polarized. DW-NOMINATE scores are frequently used to compare voting patterns of representatives between parties and over time. Here, score distributions are shown for each Congress from 1963 to 2013 separately for Democrats and Republicans. Each Congress is represented by its first year. Original figure concept: @McDonald-DW-NOMINATE.

```{r dw-nominate-ridgeline, fig.width = 8.5, fig.asp = 0.5, fig.cap = '(ref:dw-nominate-ridgeline)'}
```{r dw-nominate-ridgeline, fig.width = 5.5*6/4.2, fig.asp = 0.5, fig.cap = '(ref:dw-nominate-ridgeline)'}
# U.S. House 1963-2013
all_house_88_113 <- dw_nominate_house %>%
filter(congress >= 88 & cd !=0 & cd != 98 & cd != 99) %>%
Expand Down
44 changes: 28 additions & 16 deletions color_basics.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ Many appropriate qualitative color scales are readily available. Figure \@ref(fi

(ref:qualitative-scales) Example qualitative color scales. The Okabe Ito scale is the default scale used throughout this book [@Okabe-Ito-CUD]. The ColorBrewer Dark2 scale is provided by the ColorBrewer project [@ColorBrewer]. The ggplot2 hue scale is the default qualitative scale in the widely used plotting software ggplot2.

```{r qualitative-scales, fig.width=6, fig.asp=3*.14, fig.cap = '(ref:qualitative-scales)'}
```{r qualitative-scales, fig.width=5*6/4.2, fig.asp=3*.14, fig.cap = '(ref:qualitative-scales)'}
p1 <- gg_color_swatches(7, title_family = dviz_font_family) +
scale_fill_OkabeIto() + ggtitle("Okabe Ito")
scale_fill_OkabeIto() + ggtitle("Okabe Ito") +
theme(plot.margin = margin(7, 1.5, 7, 1.5))
p2 <- gg_color_swatches(7, title_family = dviz_font_family) +
scale_fill_brewer(type = "qual", palette = "Dark2") + ggtitle("ColorBrewer Dark2")
scale_fill_brewer(type = "qual", palette = "Dark2") + ggtitle("ColorBrewer Dark2") +
theme(plot.margin = margin(7, 1.5, 7, 1.5))
p3 <- gg_color_swatches(7, title_family = dviz_font_family) +
scale_fill_hue() + ggtitle("ggplot2 hue")
scale_fill_hue() + ggtitle("ggplot2 hue") +
theme(plot.margin = margin(7, 1.5, 7, 1.5))
plot_grid(p1, p2, p3, ncol = 1)
```

Expand Down Expand Up @@ -77,13 +80,16 @@ Sequential scales can be based on a single hue (e.g., from dark blue to light bl

(ref:sequential-scales) Example sequential color scales. The ColorBrewer Blues scale is a monochromatic scale that varies from dark to light blue. The Heat and Viridis scales are multi-hue scales that vary from dark red to light yellow and from dark blue via green to light yellow, respectively.

```{r sequential-scales, fig.width=6, fig.asp=3*.14, fig.cap = '(ref:sequential-scales)'}
```{r sequential-scales, fig.width=5*6/4.2, fig.asp=3*.14, fig.cap = '(ref:sequential-scales)'}
p1 <- gg_color_swatches(7, title_family = dviz_font_family) +
scale_fill_brewer(type = "seq", palette = "Blues", direction = -1) + ggtitle("ColorBrewer Blues")
scale_fill_brewer(type = "seq", palette = "Blues", direction = -1) + ggtitle("ColorBrewer Blues") +
theme(plot.margin = margin(7, 1.5, 7, 1.5))
p2 <- gg_color_swatches(7, title_family = dviz_font_family) +
scale_fill_discrete_sequential("Heat") + ggtitle("Heat")
scale_fill_discrete_sequential("Heat") + ggtitle("Heat") +
theme(plot.margin = margin(7, 1.5, 7, 1.5))
p3 <- gg_color_swatches(7, title_family = dviz_font_family) +
scale_fill_viridis_d() + ggtitle("Viridis")
scale_fill_viridis_d() + ggtitle("Viridis") +
theme(plot.margin = margin(7, 1.5, 7, 1.5))
plot_grid(p1, p2, p3, ncol = 1)
```

Expand Down Expand Up @@ -137,13 +143,16 @@ In some cases, we need to visualize the deviation of data values in one of two d

(ref:diverging-scales) Example diverging color scales. Diverging scales can be thought of as two sequential scales stiched together at a common midpoint color. Common color choices for diverging scales include brown to greenish blue, pink to yellow-green, and blue to red.

```{r diverging-scales, fig.width=6, fig.asp=3*.14, fig.cap = '(ref:diverging-scales)'}
```{r diverging-scales, fig.width=5*6/4.2, fig.asp=3*.14, fig.cap = '(ref:diverging-scales)'}
p1 <- gg_color_swatches(7, title_family = dviz_font_family) +
scale_fill_discrete_divergingx(palette = "Earth") + ggtitle("CARTO Earth")
scale_fill_discrete_divergingx(palette = "Earth") + ggtitle("CARTO Earth") +
theme(plot.margin = margin(7, 1.5, 7, 1.5))
p2 <- gg_color_swatches(7, title_family = dviz_font_family) +
scale_fill_brewer(type = "div", palette = "PiYG") + ggtitle("ColorBrewer PiYG")
scale_fill_brewer(type = "div", palette = "PiYG") + ggtitle("ColorBrewer PiYG") +
theme(plot.margin = margin(7, 1.5, 7, 1.5))
p3 <- gg_color_swatches(7, title_family = dviz_font_family) +
scale_fill_discrete_diverging("Blue-Red") + ggtitle("Blue-Red")
scale_fill_discrete_diverging("Blue-Red") + ggtitle("Blue-Red") +
theme(plot.margin = margin(7, 1.5, 7, 1.5))
plot_grid(p1, p2, p3, ncol = 1)
```

Expand Down Expand Up @@ -189,18 +198,21 @@ Color can also be an effective tool to highlight specific elements in the data.

(ref:accent-scales) Example accent color scales, each with four base colors and three accent colors. Accent color scales can be derived in several different ways: (top) we can take an existing color scale (e.g., the Okabe Ito scale, Fig \@ref(fig:qualitative-scales)) and lighten and/or partially desaturate some colors while darkening others; (middle) we can take gray values and pair them with colors; (bottom) we can use an existing accent color scale, e.g. the one from the ColorBrewer project.

```{r accent-scales, fig.width=6, fig.asp=3*.14, fig.cap = '(ref:accent-scales)'}
```{r accent-scales, fig.width=5*6/4.2, fig.asp=3*.14, fig.cap = '(ref:accent-scales)'}
accent_OkabeIto <- palette_OkabeIto[c(1, 2, 7, 4, 5, 3, 6)]
accent_OkabeIto[1:4] <- desaturate(lighten(accent_OkabeIto[1:4], .4), .8)
accent_OkabeIto[5:7] <- darken(accent_OkabeIto[5:7], .3)
p1 <- gg_color_swatches(7, title_family = dviz_font_family) +
scale_fill_manual(values = accent_OkabeIto) + ggtitle("Okabe Ito Accent")
scale_fill_manual(values = accent_OkabeIto) + ggtitle("Okabe Ito Accent") +
theme(plot.margin = margin(7, 1.5, 7, 1.5))
p2 <- gg_color_swatches(7, title_family = dviz_font_family) +
scale_fill_manual(values = c("gray60", "gray70","gray80", "gray90", "#C95C4F", '#83A121', '#6B8AD5')) + ggtitle("Grays with accents")
scale_fill_manual(values = c("gray60", "gray70","gray80", "gray90", "#C95C4F", '#83A121', '#6B8AD5')) + ggtitle("Grays with accents") +
theme(plot.margin = margin(7, 1.5, 7, 1.5))
p3 <- gg_color_swatches(7, title_family = dviz_font_family) +
scale_fill_brewer(type = "qual", palette = "Accent") + ggtitle("ColorBrewer Accent")
scale_fill_brewer(type = "qual", palette = "Accent") + ggtitle("ColorBrewer Accent") +
theme(plot.margin = margin(7, 1.5, 7, 1.5))
plot_grid(p1, p2, p3, ncol = 1)
```

Expand Down
Loading

0 comments on commit 5cc449b

Please sign in to comment.