Chapter 16 Revisiting earlier graphs 2
In this section, we’ll revisit earlier graphs (and make some new ones), and we’ll change some features of those graphs. Particularly, we’ll focus on changing scale
s and colour
s, and we’ll look at facet
s again.
16.1 scales
Read the section Scales, Axes & Coordinate systems.
Let’s put it to good use.
16.2 colours
Read the section colours.
Let’s add a palette from RColourBrewer:
Let’s try again:
ggplot(mpg, aes(x = cty, fill = manufacturer)) +
geom_density() +
scale_fill_brewer(palette = "Set1")
## Warning in RColorBrewer::brewer.pal(n, pal): n too large, allowed maximum for palette Set1 is 9
## Returning the palette you asked for with that many colors
What’s going on here?
Let’s see another example:
ggplot(mpg, aes(x = displ, y = cty, colour = drv)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
scale_colour_brewer(palette = "Set2")
## `geom_smooth()` using formula = 'y ~ x'
16.3 facets
Read the section Facets.
Example:
ggplot(mpg, aes(x = displ, y = cty, colour = drv)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
scale_colour_brewer(palette = "Set2") +
facet_grid(~ drv)
## `geom_smooth()` using formula = 'y ~ x'