library(tidyverse)
## ── Attaching packages ───────────────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.0.0 ✔ purrr 0.2.5
## ✔ tibble 1.4.2 ✔ dplyr 0.7.6
## ✔ tidyr 0.8.1 ✔ stringr 1.3.1
## ✔ readr 1.1.1 ✔ forcats 0.3.0
## ── Conflicts ──────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(gapminder)
library(RColorBrewer)
library(scales)
##
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
##
## discard
## The following object is masked from 'package:readr':
##
## col_factor
Make histograms of gdpPercap
for each (non-Oceania) continent by adding a line to the following code.
qualLifeExp
.scales
and ncol
arguments of the facet layer.gapminder %>%
filter(continent != "Oceania") %>%
mutate(qualLifeExp = if_else(lifeExp > 60, "high", "low")) %>%
ggplot(aes(x=gdpPercap)) +
geom_histogram() +
scale_x_log10() +
facet_wrap(~ continent, scales = "free", ncol = 3)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Grammar Component | Specification |
---|---|
data | gapminder |
statistical transform | histogram (binning and counting) |
aesthetic mapping | x=gdpPercap; y=count |
geometric object | histogram |
scale | x is log10; y is linear. |
coordinate system | Rectangular/Cartesian |
facetting | continent |
Question: What makes a plot “publication quality”?
Changing the look of a graphic can be achieved through the theme()
layer.
There are “complete themes” that come with ggplot2
, my favourite being theme_bw
(I’ve grown tired of the default gray background, so theme_bw
is refreshing).
theme_bw()
:ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
facet_wrap(~ Species) +
geom_point() +
labs(x = "Sepal Width",
y = "Sepal Length",
title = "Sepal sizes of three plant species") +
theme_bw()
ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
facet_wrap(~ Species) +
geom_point() +
labs(x = "Sepal Width",
y = "Sepal Length",
title = "Sepal sizes of three plant species") +
theme_bw() +
theme(strip.background = element_rect(fill="orange"))
Scale functions in ggplot2
take the form scale_[aesthetic]_[mapping]()
.
Let’s first focus on the following plot:
p_scales <- ggplot(gapminder, aes(gdpPercap, lifeExp)) +
geom_point(aes(colour=pop), alpha=0.2)
p_scales +
scale_x_log10() +
scale_colour_continuous(trans="log10")
# p_scales +
# scale_x_log10() +
# scale_colour_continuous(
# trans = "log10",
# breaks = FILL_IN_BREAKS
# ) +
# FILL_IN_SCALE_FUNCTION(breaks=FILL_IN_BREAKS)
scales::*_format
in the labels
argument of a scale function to do the following:
scales::dollar_format()
)scales::comma_format()
)p_scales +
scale_x_log10(labels=dollar_format()) +
scale_colour_continuous(
trans = "log10",
breaks = 10^(1:10),
labels = comma_format()
) +
scale_y_continuous(breaks=10*(1:10))
RColorBrewer
to change the colour scheme.
## All palettes the come with RColorBrewer:
# RColorBrewer::display.brewer.all()
# p_scales +
# scale_x_log10(labels=dollar_format()) +
# FILL_IN_WITH_RCOLORBREWER(
# trans = "log10",
# breaks = 10^(1:10),
# labels = comma_format(),
# palette = FILL_THIS_IN
# ) +
# scale_y_continuous(breaks=10*(1:10))
viridis
scale for a colour-blind friendly scheme.
scale_colour_viridis_c
(c
stands for continuous; d
discrete).option
.p_scales +
scale_x_log10(labels=dollar_format()) +
scale_colour_viridis_c(
trans = "log10",
breaks = 10^(1:10),
labels = comma_format()
) +
scale_y_continuous(breaks=10*(1:10))