library(tidyverse)
library(gapminder)

Saving Graphs to File

library(here)
## here() starts at /Users/dianalin/STAT545-participation
(p <- ggplot(mtcars, aes(hp, wt)) + 
    geom_point())

ggsave(here("cm013","mtcars.png"), p)
## Saving 7 x 5 in image

Scales: Colour

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")

  1. Change the y-axis tick mark spacing to 10; change the colour spacing to include all powers of 10.
p_scales +
    scale_x_log10() +
    scale_colour_continuous(
        trans  = "log10", 
        breaks = 10^(1:10)
    ) +
    scale_y_continuous(breaks=10*(1:10))

  1. Specify scales::*_format in the labels argument of a scale function to do the following:
    • Change the x-axis labels to dollar format (use scales::dollar_format())
    • Change the colour labels to comma format (use scales::comma_format())
library(scales)
p_scales +
    scale_x_log10(labels=scales::dollar_format()) +
    scale_colour_continuous(
        trans  = "log10", 
        breaks = 10^(1:10),
        labels = scales::comma_format()
    ) +
    scale_y_continuous(breaks=10*(1:10))

  1. Use RColorBrewer to change the colour scheme.
    • Notice the three different types of scales: sequential, diverging, and continuous.
## All palettes the come with RColorBrewer:
RColorBrewer::display.brewer.all()

p_scales +
    scale_x_log10(labels=dollar_format()) +
    scale_colour_distiller(
        trans   = "log10",
        breaks  = 10^(1:10),
        labels  = comma_format(),
        palette = "Purples"
    ) +
    scale_y_continuous(breaks=10*(1:10))

  1. Use viridis to change the colour to a colour-blind friendly scheme
    • Hint: add scale_colour_viridis_c (c stands for continuous; d discrete).
    • You can choose a palette with option.
p_scales +
    scale_x_log10(labels=dollar_format()) +
    scale_colour_viridis_c(
        trans   = "log10",
        breaks  = 10^(1:10),
        labels  = comma_format(),
        option = "E"
    ) +
    scale_y_continuous(breaks=10*(1:10))

Theming

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).

  1. Change the theme of the following plot to 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")

  1. Then, change font size of axis labels, and the strip background colour. Others?
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(axis.title = element_text(size = 28),
          strip.background = element_rect())

Plotly

Consider the following plot:

(p <- gapminder %>% 
     filter(continent != "Oceania") %>% 
     ggplot(aes(gdpPercap, lifeExp)) +
     geom_point(aes(colour=pop), alpha=0.2) +
     scale_x_log10(labels=dollar_format()) +
     scale_colour_distiller(
         trans   = "log10",
         breaks  = 10^(1:10),
         labels  = comma_format(),
         palette = "Greens"
     ) +
     facet_wrap(~ continent) +
     scale_y_continuous(breaks=10*(1:10)) +
     theme_bw())

  1. Convert it to a plotly object by applying the ggplotly() function:
library(plotly)
p %>%
  ggplotly()
  1. You can save a plotly graph locally as an html file. Try saving the above:
    • NOTE: plotly graphs don’t seem to show up in Rmd notebooks, but they do with Rmd documents.
p %>% 
    ggplotly() %>% 
    htmlwidgets::saveWidget("~/STAT545-participation/cm013/plotly-widget.html")
  1. Run this code to see the json format underneath:
library(listviewer)
p %>% 
    ggplotly() %>% 
    plotly_json(jsonedit = TRUE)
  1. Check out code to make a plotly object from scratch using plot_ly() – scatterplot of gdpPercap vs lifeExp.
plot_ly(gapminder, 
        x = ~gdpPercap, 
        y = ~lifeExp, 
        type = "scatter",
        mode = "markers",
        opacity = 0.2) %>% 
    layout(xaxis = list(type = "log"))
  1. Add population to form a z-axis for a 3D plot:
plot_ly(gapminder, 
        x = ~gdpPercap, 
        y = ~lifeExp, 
        z = ~pop,
        type = "scatter3d",
        mode = "markers",
        opacity = 0.2)