3 min read

Grammar of Graphics (ggplot2) [Part 2]

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_grey and scale_color_brewer(palette = "Set1")
  • for axis, try scale_x_log10() and scale_y_log10()

There are also several packages with more exotic color scales:

  • ggsci: scientific journal color palettes [link]
  • wesanderson: palletes inspired by Wes Anderson’s movies [link]
  • RSkittleBrewer: for those times you want to make plots with candy-themed color schemes [link]

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() and scale_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 Export tab in Plots panel
  • In ggplot2 package you can use a ggsave function (see ?ggsave for help and examples)
  • You can pdf, png and dev.off as 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: