Slides


Saving your ggplot2 plots

We use the ggsave() function to export plots in any format we like. Simply include the ggsave() call after your ggplot2 building blocks, using variations of the following options

ggsave(filename = "./figures/rwanda_plot.png",  # remember "figures" folder?
                                                # ".png" suffix determines file type
       plot = rwanda_plot, # which plot to save?
       width = 8, height = 4.5, # values between 5 and 10 inches are reasonable
       units = "in", 
       dpi = 300, # 300dpi = printing quality
       scale = 0.8) # enlarge (< 1) / shrink (> 1) text

Where To Find Help

.font150[Data Visualization - A practical introduction]

by Kieran Healy (forthcoming Princeton University Press)


Where To Find Help

.font150[StackOverflow]

Tip: include the term “stack” and “ggplot” in your google query terms


Where To Find Help

.font150[R for Data Science]


Where To Find Help

#rstats Twitter hashtag


Cheatsheets


General Tips

  • Press Tab to autocomplete!
  • Use keyboard shortcuts
    • Strg/Ctrl + Shift + M for %>%
    • Alt + - for <-
    • Shift + 1/2/3 switches between RStudio panes
  • Name things often & understandably (e.g. not m1 but baseline_model)
    • Follow a style guide
  • Use comments # often and judiciously
    • Comment out several lines of code with Strg/Ctrl + Shift + c
  • Look for package vignettes on Google

class: inverse, center, middle

Questions?


Labels and Text

geom_text is useful for simple text display; geom_repel from the ggrepel package has more advanced functions.

library(tidyverse)
library(gapminder)

gapminder_africa <- gapminder %>% 
  filter(continent == "Africa") %>% 
  filter(year > 1990)

rwanda_plot <- ggplot(gapminder_africa, 
                      aes(x = year, y = lifeExp)) +
  geom_point() +
  geom_text(aes(label = country)) # `label` is an aesthetic like `color`
                                  # we map 'country' to label

Labels and Text


Labels and Text: Subset data first!

It often makes sense to create a separate data frame with the labels you want to you plot.

min_lifeexp <- gapminder_africa %>% 
  # select only observation with smallest
  # or biggest lifeExp (outliers)
  filter(lifeExp == min(lifeExp) | lifeExp == max(lifeExp)) 

rwanda_plot <- ggplot(gapminder_africa, 
                      aes(x = year, y = lifeExp)) +
  geom_point() +
  geom_text(data = min_lifeexp, 
            aes(label = country))    

Labels and Text: Subset data first!


Labels and Text: ggrepel

library(ggrepel)

rwanda_plot <- ggplot(gapminder_africa, 
                      aes(x = year, y = lifeExp)) +
  geom_point() +
  geom_label_repel(data = min_lifeexp, 
                   aes(label = country))    

Labels and Text: ggrepel


Histograms and variable distributions

lifexp_histplot <- ggplot(gapminder, 
                          aes(x = lifeExp)) + # note that no 'y' mapping
  geom_histogram() +
  facet_wrap(~ continent)

print(lifexp_histplot)


Additional exercises

  1. Plot a scatterplot of the relationship between gdpPercap and lifeExp and adjust the point size to population.

  2. Install the WDI package to access World Bank data. Use the help function help(WDI) to find out how the package’s main function WDI() works. Download data for GDP (indicator: NY.GDP.MKTP.CD) and corruption (indicator IQ.CPA.TRAN.XQ). Try to plot the two variables for all countries in the world.