Incorporate some twitter feedback

This commit is contained in:
hadley 2016-08-17 12:40:40 -05:00
parent 31d05d3311
commit 936e0f8aa4
1 changed files with 19 additions and 4 deletions

View File

@ -6,6 +6,8 @@ In [exploratory data analysis], you learned how to use plots as tools for _explo
Now you need to _communicate_ the result of your analysis to others. Your audience will not share your background knowledge and will not be deeply invested in the data. To help these newcomers quickly build up a good mental model of the data you will need to invest considerable effort to make your plots as self-explanatory as possible. In this chapter, you'll learn some of the tools that ggplot2 provides to do so.
The focus of this chapter is on the tools that you need to create good graphics. I assume you know what you want, and you just want to know how to do it. For that reason, I highly recommend pairing this advice with a good general visualisation book. I particularly like [_The Truthful Art_](https://amzn.com/0321934075), by Albert Cairo. I doesn't teach the mechanics of creating visualisations, but instead focusses on what you need to think about in order to create effective graphics.
### 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 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.
@ -39,7 +41,7 @@ ggplot(mpg, aes(displ, hwy)) +
geom_smooth(se = FALSE) +
labs(
title = "Fuel efficiency decreases with engine size",
subtitle = "Two seaters are an exception because of their light weight",
subtitle = "Two seaters (sports cars) are an exception because of their light weight",
caption = "Data from fueleconomy.gov"
)
```
@ -51,7 +53,7 @@ ggplot(mpg, aes(displ, hwy)) +
geom_point(aes(colour = class)) +
geom_smooth(se = FALSE) +
labs(
x = "Displacement (L)",
x = "Engine displacement (L)",
y = "Highway fuel economy (mpg)",
colour = "Car type"
)
@ -74,8 +76,13 @@ ggplot(df, aes(x, y)) +
### Exercises
1. Create one plot where you customize the `title`, `subtitle`, `caption`, `x`, `y`,
and `colour` labels.
1. Create one plot of the fuel economy data with customized the `title`,
`subtitle`, `caption`, `x`, `y`, and `colour` labels.
1. The `geom_smooth()` is somewhat misleading because it the `hwy` for
large engines is skewed upwards because of the lightweight sports
cars with big engines. Use your modelling tools to fit and display
a better model.
1. Take an exploratory graphic that you've created in the last month, and add
informative titles to make it easier for others to understand.
@ -341,6 +348,14 @@ ggplot(mpg, aes(displ, hwy)) +
scale_colour_brewer(palette = "Set1")
```
Don't forget simpler techniques. If there are just a few colours, you can add a redundant shape mapping. This will also ensure your plot works well in black and white.
```{r}
ggplot(mpg, aes(displ, hwy)) +
geom_point(aes(color = drv, shape = drv)) +
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.
```{r brewer, fig.asp = 2.5, echo = FALSE, fig.cap = "All ColourBrewer scales."}