diff --git a/communicate-plots.Rmd b/communicate-plots.Rmd index 55af73a..5111e2c 100644 --- a/communicate-plots.Rmd +++ b/communicate-plots.Rmd @@ -8,7 +8,7 @@ Now you need to _communicate_ the result of your analysis to others. Your audien ### Prerequisites -In this chapter, we'll focus once again on ggplot2. We'll also use a little dplyr for data manipulation, and a few ggplot2 extension packages, including __ggrepel__ and __viridis__. Rather than loading those extension here we'll refer to their functions explicitly with the `::` notation. That will help make it obvious what functions are built into ggplot2, and what functions come from other packages. +In this chapter, we'll focus once again on ggplot2. We'll also use a little dplyr for data manipulation, and a few ggplot2 extension packages, including __ggrepel__ and __viridis__. Rather than loading those extensions here we'll refer to their functions explicitly with the `::` notation. That will help make it obvious what functions are built into ggplot2, and what functions come from other packages. ```{r, message = FALSE} library(ggplot2) @@ -74,11 +74,11 @@ ggplot(df, aes(x, y)) + ### Exercises -1. Create one plot that combines the `title`, `subtitle`, `caption`, `x`, `y`, +1. Create one plot where you customize the `title`, `subtitle`, `caption`, `x`, `y`, and `colour` labels. 1. Take an exploratory graphic that you've created in the last month, and add - explanatory titles to make it easier for others to understand. + informative titles to make it easier for others to understand. ## Annotations @@ -171,7 +171,7 @@ I manually broke the label up into lines using `"\n"`. Another approach is to us writeLines() ``` -Also note the use of `hjust` and `vjust` to control the the alignment of the label. \@ref(fig:just) shows all nine possible combinations. +Also note the use of `hjust` and `vjust` to control the the alignment of the label. Figure \@ref(fig:just) shows all nine possible combinations. ```{r just, echo = FALSE, fig.cap = "All nine combinations of `hjust` and `vjust`."} vjust <- c(bottom = 0, center = 0.5, top = 1) @@ -202,10 +202,10 @@ Remember, as well as `geom_text()` you have all the other geoms in ggplot2 avail `ymin`, `ymax`. * Use `geom_segment()` with the `arrow` argument to draw attention - to a point with a arrow. Use aesthetics `x` and `y` to define the + to a point with an arrow. Use aesthetics `x` and `y` to define the starting location, and `xend` and `yend` to define the end location. -The only limitation is your imagination! (and your patience at position annotations in a way that looks good). +The only limitation is your imagination! (And your patience at position annotations in a way that looks good.) ### Exercises @@ -215,7 +215,7 @@ The only limitation is your imagination! (and your patience at position annotati 1. Read the documentation for `annotate()`. How can you use it to add a text label to a plot without having to create a tibble? -1. How do labels with `geom_text()` interract with facetting? How can you +1. How do labels with `geom_text()` interract with faceting? How can you add a label to a single facet? How can you put a different label in each facet? (Hint: think about the underlying data.) @@ -325,9 +325,9 @@ ggplot(mpg, aes(displ, hwy)) + ### Replacing a scale -Instead of just tweaking the detail a little, you can also replace the scale altogether. We'll focus on colour scales because there are many options, and they're the scales you're mostly likely to want to change. The same principles apply to the other aesthetics. All colour scales have two variants `scale_colour_x()` and `scale_fill_x()` for the `colour` and `fill` aesthetics respectically. (And the colour scales are available in both UK and US spellings.) +Instead of just tweaking the detail a little, you can also replace the scale altogether. We'll focus on colour scales because there are many options, and they're the scales you're mostly likely to want to change. The same principles apply to the other aesthetics. All colour scales have two variants: `scale_colour_x()` and `scale_fill_x()` for the `colour` and `fill` aesthetics respectively (And the colour scales are available in both UK and US spellings.) -The default categorical scale picks colours that are evenly spaced around the colour wheel. A useful alternative are the ColourBrewer scales which have been hand tuned to work better for people with common types of colour blindness. The two plots below don't look that different, but there's enough difference in the shades of red and green that they can be distinguished even by people with red-green colour blindness. +The default categorical scale picks colours that are evenly spaced around the colour wheel. Useful alternatives are the ColourBrewer scales which have been hand tuned to work better for people with common types of colour blindness. The two plots below don't look that different, but there's enough difference in the shades of red and green that they can be distinguished even by people with red-green colour blindness. ```{r, fig.align = "default", out.width = "50%"} ggplot(mpg, aes(displ, hwy)) + @@ -338,14 +338,14 @@ ggplot(mpg, aes(displ, hwy)) + scale_colour_brewer(palette = "Set1") ``` -Figure \@ref(fig-brewer) shows the complete list of all palettes. The sequential (top) and diverging (bottom) palettes are particularly useful if your categorical values are ordered, or have a "middle". This often arises if you've used `cut()` to make a continuous varible into a categorical variable. +Figure \@ref(fig:brewer) shows the complete list of all palettes. The sequential (top) and diverging (bottom) palettes are particularly useful if your categorical values are ordered, or have a "middle". This often arises if you've used `cut()` to make a continuous varible into a categorical variable. ```{r brewer, fig.asp = 2.5, echo = FALSE, fig.cap = "All ColourBrewer scales."} par(mar = c(0, 3, 0, 0)) RColorBrewer::display.brewer.all() ``` -When you have a predefined mapping between values and colours use `scale_colour_manual()`. For example, if we map Presidential party to colour, we want to use the standard mapping of red for Republicans and blue for Democrats: +When you have a predefined mapping between values and colours use `scale_colour_manual()`. For example, if we map presidential party to colour, we want to use the standard mapping of red for Republicans and blue for Democrats: ```{r} presidential %>% @@ -356,7 +356,7 @@ presidential %>% scale_colour_manual(values = c(Republican = "Red", Democratic = "Blue")) ``` -For continuous colour, you can use the built-in `scale_colour_gradient()` (or `scale_fill_gradient()`). If you have a diverging scale, you can use `scale_colour_gradient2()`. That allows you to give (for example), positive and negative values different colours. That's sometimes also useful if you want to distinguish points above or below the mean. +For continuous colour, you can use the built-in `scale_colour_gradient()` or `scale_fill_gradient()`. If you have a diverging scale, you can use `scale_colour_gradient2()`. That allows you to give, for example, positive and negative values different colours. That's sometimes also useful if you want to distinguish points above or below the mean. Another option is `scale_colour_viridis()` provided by the __viridis__ package. It's a continuous analog of the categorical Brewer scales. The designers, Nathaniel Smith and Stéfan van der Walt, carefully tailored a continuous colour scheme that has good perceptual properities. Here's an example from the viridis vignette. @@ -392,11 +392,11 @@ ggplot(df, aes(x, y)) + 1. Combining the two variants shown above. 1. Improve the display of the y axis. - 1. Labelling each term with the name of the President. + 1. Labelling each term with the name of the president. 1. Adding informative plot labels. 1. Placing breaks every 4 years (this is trickier than it seems!). -1. Use `override.aes` to make the legend on the following plot more useful. +1. Use `override.aes` to make the legend on the following plot easier to see. ```{r, dev = "png"} ggplot(diamonds, aes(carat, price)) + @@ -427,7 +427,7 @@ mpg %>% coord_cartesian(xlim = c(5, 7), ylim = c(10, 30)) ``` -You can also setting the `limits` on individual scales. If you are reducing the limits, this is basically equivalent to subsetting the data. It's more useful if you want _expand_ the limits. This is useful if you want to match scales across different plots. Take the following toy example: if we extract out two classes of car and plot them separately, it's hard to compare the plots because all three scales have different ranges. +You can also set the `limits` on individual scales. If you are reducing the limits, this is basically equivalent to subsetting the data. It's more useful if you want _expand_ the limits, for example for matching scales across different plots. Take the following toy example: if we extract out two classes of cars and plot them separately, it's hard to compare the plots because all three scales have different ranges. ```{r out.width = "50%", fig.align = "default", fig.width = 4} suv <- mpg %>% filter(class == "suv") @@ -460,7 +460,7 @@ ggplot(compact, aes(displ, hwy, colour = drv)) + col_scale ``` -In this case you could have used facetting, but this technique is broadly useful if you want to make your plots are comparable even when spread across multiple pages of your final report. +In this case you could have used faceting, but this technique is broadly useful if you want to make your plots are comparable even when spread across multiple pages of your final report. ## Themes @@ -479,7 +479,7 @@ ggplot2 includes eight themes by default, as shown in Figure \@ref(fig:themes). knitr::include_graphics("images/visualization-themes.png") ``` -Many people wonder why the default theme has a grey background. This was a deliberate choice because it puts the data forward while still making the grid lines visible. The white grid lines are still visible (which is important because they significantly aid position judgements), but they have little visual impact and we can easily tune them out. The grey background gives the plot a similar typographic colour to the text, ensuring that the graphics fit in with the flow of a document without jumping out with a bright white background. Finally, the grey background creates a continuous field of colour which ensures that the plot is perceived as a single visual entity. +Many people wonder why the default theme has a grey background. This was a deliberate choice because it puts the data forward while still making the grid lines visible. The white grid lines are visible (which is important because they significantly aid position judgements), but they have little visual impact and we can easily tune them out. The grey background gives the plot a similar typographic colour to the text, ensuring that the graphics fit in with the flow of a document without jumping out with a bright white background. Finally, the grey background creates a continuous field of colour which ensures that the plot is perceived as a single visual entity. It's also possible to control individual components of each theme, like the size and colour of the font used for the y axis. This unfortunately is outside the scope of this book, so you'll need to read the ggplot2 book for the full details. You can also create your own themes if you have a corporate style or you're trying to match a journal style.