diff --git a/communicate-plots.Rmd b/communicate-plots.Rmd index c5afa48..bc1d315 100644 --- a/communicate-plots.Rmd +++ b/communicate-plots.Rmd @@ -174,7 +174,7 @@ ggplot(mpg, aes(displ, hwy)) + geom_text(aes(label = label), data = label, vjust = "top", hjust = "right") ``` -In these examples, I manually broke the label up into lines using `"\n"`. Another approach is to use `stringr::str_wrap()` to automatically add linebreaks, given the number of characters you want per line: +In these examples, I manually broke the label up into lines using `"\n"`. Another approach is to use `stringr::str_wrap()` to automatically add line breaks, given the number of characters you want per line: ```{r} "Increasing engine size is related to decreasing fuel economy." %>% @@ -323,7 +323,7 @@ base + theme(legend.position = "bottom") base + theme(legend.position = "right") # the default ``` -You can also use `legend.postion = "none"` to suppress the display of the legend altogether. +You can also use `legend.position = "none"` to suppress the display of the legend altogether. To control the display of individual legends, use `guides()` along with `guide_legend()` or `guide_colourbar()`. The following example shows two important settings: controlling the number of rows the legend uses with `nrow`, and overriding one of the aesthetics to make the points bigger. This is particularly useful if you have used a low `alpha` to display many points on a plot. @@ -373,7 +373,7 @@ presidential %>% ggplot(aes(start, id, colour = party)) + geom_point() + geom_segment(aes(xend = end, yend = id)) + - scale_colour_manual(values = c(Republican = "Red", Democratic = "Blue")) + 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. @@ -443,8 +443,7 @@ mpg %>% filter(displ >= 5, displ <= 7, hwy >= 10, hwy <= 30) %>% ggplot(aes(displ, hwy)) + geom_point(aes(color = class)) + - geom_smooth() + - coord_cartesian(xlim = c(5, 7), ylim = c(10, 30)) + geom_smooth() ``` You can also set the `limits` on individual scales. Reducing the limits is basically equivalent to subsetting the data. It is generally more useful if you want _expand_ the limits, for example, to match scales across different plots. For example, if we extract two classes of cars and plot them separately, it's difficult to compare the plots because all three scales (the x-axis, the y-axis, and the colour aesthetic) have different ranges. @@ -570,6 +569,6 @@ It's a good idea to name code chunks that produce figures, even if you don't rou ## Learning more -The absolute best place to learn more is the ggplot2 book: [_ggplot2: Elegant graphics for data analysis_](https://amzn.com/331924275X). It goes into much more depth about the underlying theory, and has many more examples of how to combine the individual pieces to solve practical problems. Unfortunately the book is not available online for free, although you can find the source code at . +The absolute best place to learn more is the ggplot2 book: [_ggplot2: Elegant graphics for data analysis_](https://amzn.com/331924275X). It goes into much more depth about the underlying theory, and has many more examples of how to combine the individual pieces to solve practical problems. Unfortunately, the book is not available online for free, although you can find the source code at . Another great resource is the ggplot2 extensions guide . This site lists many of the packages that extend ggplot2 with new geoms and scales. It's a great place to start if you're trying to do something that seems hard with ggplot2.