#install.packages('ggplot2')
#library(ggplot2)

GOALS: Students should be able to use ggplot2 to generate publication quality graphics and understand and use the basics of the grammar of graphics.

##DataViz

Terminology:

First Plots with GGPLOT

#gapminder <- read.csv("https://goo.gl/BtBnPg", header = T)
gapminder <- read.csv('gapminder-FiveYearData.csv', header=T)

Let’s start off with an example:

library(ggplot2)
ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
  geom_point()

NOTE:

Alone the ggplot call isn’t enough to render the plot.

ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp))
## If run, would produce a blank plot or error.
ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
  geom_point()

Challenge 1

Challenge 2

Layers

ggplot(data = gapminder, aes(x=year, y=lifeExp, by=country, color=continent)) +
  geom_line()
ggplot(data = gapminder, aes(x=year, y=lifeExp, by=country, color=continent)) +
  geom_line() + geom_point()
ggplot(data = gapminder, aes(x=year, y=lifeExp, by=country)) +
  geom_line(aes(color=continent)) + geom_point()

Challenge 3

Transformations and statistics

ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp, color=continent)) +
  geom_point()
ggplot(data = gapminder, aes(x = lifeExp, y = gdpPercap)) +
  geom_point(alpha=0.5) + scale_y_log10()
ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
  geom_point() + scale_x_log10() + geom_smooth(method="lm")
# example of assigning a plot to variable pwd
pwd <- ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
  geom_point() + scale_x_log10() + geom_smooth(method="lm", size=1.5)

pwd

Challenge 4a

Challenge 4b

Multi-panel figures: FACEting

starts.with <- substr(gapminder$country, start = 1, stop = 1)
az.countries <- gapminder[starts.with %in% c("A", "Z"), ]

Talk thru code: * We’ll start by subsetting the data using the substr function topull out a part of a character string; * in this case, the letters that occur in positions start through stop, inclusive, of the gapminder$country vector. * The operator %in% allows us to make multiple comparisons rather than write out long subsetting conditions (in this case, starts.with %in% c("A", "Z") is equivalent to starts.with == "A" | starts.with == "Z")

ggplot(data = az.countries, aes(x = year, y = lifeExp, color=continent)) +
  geom_line() + facet_wrap( ~ country)

Modifying text

Now lets clean up this figure for publication.

ggplot(data = az.countries, aes(x = year, y = lifeExp, color=continent)) +
  geom_line() + facet_wrap( ~ country) +
  labs(
    x = "Year",              # x axis title
    y = "Life expectancy",   # y axis title
    title = "Figure 1",      # main title of figure
    color = "Continent"      # title of legend
  ) +
  theme(axis.text.x=element_blank(), axis.ticks.x=element_blank())

Challenge 5

How to save your plots

ggsave('~/path/to/figure/filename.png')
ggsave(filename_to_save, file = "~/path/to/figure/filename.png") # filename, path to save location
ggsave(file = "/path/to/figure/filename.png", width = 6,
height =4)      # Plot size in units ("in", "cm", or "mm"). If not supplied, uses the size of current graphics device.
# file can be either be a device function (e.g. png), or one of "eps", "ps", "tex" (pictex), "pdf", "jpeg", "tiff", "png", "bmp", "svg" or "wmf" (windows only).

ggsave(file = "/path/to/figure/filename.eps")
ggsave(file = "/path/to/figure/filename.jpg")
ggsave(file = "/path/to/figure/filename.pdf")

Resources:

This is just a taste of what you can do with ggplot2. RStudio provides a really useful cheat sheet of the different layers available, and more extensive documentation is available on the ggplot2 website. Finally, if you have no idea how to change something, a quick Google search will usually send you to a relevant question and answer on Stack Overflow with reusable code to modify!

ggplot save reference http://ggplot2.tidyverse.org/reference/ggsave.html

ggplot cheat sheet https://www.rstudio.com/wp-content/uploads/2015/03/ggplot2-cheatsheet.pdf

ggplot site http://ggplot2.org/