ggplot2 recap
- Input data into
ggplot - Add visualization layers with
geom_functions - Map variables to aesthetics with
aes() - Plot summaries with
stat= - Handle collisions with
position= - Change coordinate systems with a
coord_function - Facet the plot with a
facet_function
Challenge 1:
Try to make the following plot (density plot of price in dependence on cut) and save it into variable p1.

labels
A function to add labels to a plot is labs. You can add a title, subtitle and caption as follow.
p1 + labs(title = "Poorly cut diamonds appear to fetch unexpectedly high prices",
subtitle = "The lowest cut rating is associated with the highest mode price",
caption = "Data by Hadley Wickham")

Similarly, you can change axis labels and legend.
p1 + labs(x = "Price", y = "Density", color = "Cut Rating")

themes
Themes change appearance of non-data elements.
First, you change theme manually using theme function. See the list of what you can control in the documentation. For example, you can take the previous plot and remove the legend:
p1 + theme(legend.position = "none")

Second, you can use predefined themes. Take the p1 plot and try to add the following themes:
p1 + theme_bw()p1 + theme_classic()p1 + theme_dark()p1 + theme_light()p1 + theme_linedraw()p1 + theme_minimal()p1 + theme_void()
Many developers come with their own ggplot2 themes. For example with library(ggthemes) you can try p1 + theme_wsj() to style your plot like Wall Street Journal, with library(xkcd) you can style your plot as xkcd comics and Bob Rudis creates his hrbrthemes for his style.
# devtools package can be installed as install.packages("devtools")
# hrbrthemes package can be then installed as devtools::install_github("hrbrmstr/hrbrthemes")
library(hrbrthemes)
p1 + theme_ipsum_rc() # or theme_ipsum() on Windows

scales
You can add a scale_ function for each aesthetics.
Let us plot the dependence between price and weight (carat):
s <- ggplot(diamonds, aes(carat, price)) +
geom_point(aes(color = cut))
s

Challenge 2:
Try to apply various scales to the graph s:
- for color, try
scale_color_greyandscale_color_brewer(palette = "Set1") - for axis, try
scale_x_log10()andscale_y_log10()
There are also several packages with more exotic color scales:
Challenge 3:
If we now color previous graph by price instead of cut:
s1 <- ggplot(diamonds, aes(carat, price)) +
geom_point(aes(color = price))
Try to apply various continuous color scales.
- from
library(viridis):scale_color_viridis()andscale_color_viridis(option = "A") scale_color_distiller(palette = "Spectral")scale_color_brewer(palette = "Purples")
Challenge 4:
Now experiment with labels, themes, and scales to make a clear graph how a price of diamond depends on its weight. Perhaps this one, or any other you like.

saving graphs
There is more than one way to save the graph:
- In RStudio you can use
Exporttab inPlotspanel - In
ggplot2package you can use aggsavefunction (see?ggsavefor help and examples) - You can
pdf,pnganddev.offas in base graphics
Challenge 5:
Save the plot generated in Challenge 4 into PDF file.
summary
ggplot2 in a combination with custom themes are a fast route to creation of (almost) publication-quality graphics.
Sources:
- Data Visualization with ggplot2 (rstudio::conf, Tidyverse workshop)
- R for Data Science
- ggplot2 documentation