2016-02-24

Your turn

  • What is a (data) plot?
  • What are the three most important data plots?

Your turn

How would you describe this plot?

Using the package ggplot2

Elements of a plot

  • data
  • aesthetics: mapping of variables to graphical elements
  • geom: type of plot structure to use
  • transformations: log scale, …

Additional components

  • layers: multiple geoms, multiple data sets, annotation
  • facets: show subsets in different plots
  • themes: modifying style

Have you opened your project?

Data - Currency cross rates

Extracted from http://openexchangerates.org, extracted using the json api, with the R package, jsonlite.

library(readr)
rates <- read_csv("http://dicook.github.io/Monash-R/data/rates.csv")
rates[1:5,1:8]
#> Source: local data frame [5 x 8]
#> 
#>         date      AED      AFN      ALL     AMD     ANG      AOA      ARS
#>       (date)    (dbl)    (dbl)    (dbl)   (dbl)   (dbl)    (dbl)    (dbl)
#> 1 2015-02-23 3.672900 57.33792 123.8969 478.692 1.78968 105.9075 8.702166
#> 2 2015-02-24 3.672069 57.35200 123.7132 478.608 1.78958 106.1014 8.696728
#> 3 2015-02-25 3.673324 57.32655 123.5259 478.616 1.78954 106.1913 8.715239
#> 4 2015-02-26 3.673028 57.52745 124.5801 480.294 1.78956 106.3042 8.720107
#> 5 2015-02-27 3.672648 57.33172 124.8491 478.812 1.78958 106.3389 8.721236

If you'd like to collect exchange rates yourself, see here.

Plotting points

qplot(date, AUD, data=rates)

Adding lines

qplot(date, AUD, data=rates, geom="line")

Points and lines

qplot(date, AUD, data=rates, geom=c("line", "point"))

Plot structure

  • data: rates
  • aesthetics: x=date, y=AUD
  • geom: point, line
ggplot(data=rates, aes(x=date, y=AUD)) + geom_point() + geom_line()

Multiple currencies

ggplot(data=rates, aes(x=date, y=AUD)) + geom_line() +
  geom_line(aes(y=NZD), colour="blue") + 
  geom_line(aes(y=GBP), colour="red")

Hmmm

  • That code is clunky!
  • Better to rearrange data, and then let ggplot2 handle the colors, legends, …

Better way

rates.sub <- select(rates, date, AUD, NZD, GBP)
rates.sub.m <- gather(rates.sub, currency, rate, -date)
qplot(date, rate, data=rates.sub.m, geom="line", colour=currency)

## Emphasis on magnitude of cross rate

  • On this scale we see that the GBP has a much lower ratio with the USD than AUD and NZD. This is information that is easy to obtain by eyeballing the numbers.
  • We are probably more interested in comparing the trend of rates, which would suggest to standardise the rates. We will use the traditional definition of standardising.

Scaling

rates.sub <- mutate(rates.sub, AUD=scale(AUD), NZD=scale(NZD), GBP=scale(GBP))
rates.sub$date <- as.Date(rates.sub$date)
rates.sub.m <- gather(rates.sub, currency, rate, -date)
qplot(date, rate, data=rates.sub.m, geom="line", colour=currency)

Your turn

Set the linetype/shape to be different for the different currencies.

Your turn

Set the linetype/shape to be different for the different currencies.

qplot(date, rate, data=rates.sub.m, geom="line", colour=currency, linetype=currency, lwd=I(2))

qplot(date, rate, data=rates.sub.m, geom=c("line", "point"), colour=currency, linetype=currency, shape=currency)

Scatterplot

qplot(AUD, NZD, data=rates.sub) + theme(aspect.ratio=1)

Scatterplot, time connected?

qplot(AUD, NZD, data=rates.sub, geom="line") + theme(aspect.ratio=1)

Problem: line only connects points from left to right along the x axis

Scatterplot, time connected!

qplot(AUD, NZD, data=rates.sub, geom="path", colour=order(date)) + theme(aspect.ratio=1)

Scatterplot, overlaid density

qplot(AUD, NZD, data=rates.sub, geom=c("density2d", "point")) + theme(aspect.ratio=1)

Scatterplot of lagged AUD

AUD <- rates[,c("date", "AUD")]
AUD.1 <- lag(AUD$AUD, 1)
AUD.2 <- lag(AUD$AUD, 2)
AUD.7 <- lag(AUD$AUD, 7)
qplot(AUD, AUD.1, data=rates.sub) + theme(aspect.ratio=1)
qplot(AUD, AUD.2, data=rates.sub) + theme(aspect.ratio=1)
qplot(AUD, AUD.7, data=rates.sub) + theme(aspect.ratio=1)

Your turn

Look up geom_rug in the ggplot2 cheat sheet and add the marginal distributions to a scatterplot of AUD and NZD.

Histogram

qplot(AUD, data=rates.sub, geom="histogram") 

Your turn

Only one variable was passed to the plot command, but two axes are shown in the histogram. What happened? What is plotted on the vertical axis?

Density

qplot(AUD, data=rates.sub, geom="density", fill=I("black")) 

Your turn

  • What ways have we seen data variables mapped to graphical elements so far?
  • What are the natural ways to map a categorical variable to a graphical element?

Modifying labels

qplot(date, rate, data=rates.sub.m, geom="line", colour=currency) +
  xlab("Date") + ylab("Standardized rates") + 
  ggtitle("Cross rates 23/2/2015-11/11/2015")

Equations in labels

qplot(date, rate, data=rates.sub.m, geom="line", colour=currency) +
  xlab(expression(Date[i]^2~ mu ~ pi * sigma)) + ylab("Standardized rates") + 
  ggtitle("Cross rates 23/2/2015-11/11/2015")

Scales: modifying axes

rates.sub.m$date <- as.POSIXct(rates.sub.m$date)
p <- qplot(date, rate, data = rates.sub.m, geom = "line", colour = currency) +
  scale_x_datetime(breaks = date_breaks("1 month"), labels = date_format("%b")) +
  scale_y_continuous("Standardized rates") 
p

Legend Position

p + theme(legend.position = "bottom")

Themes

library(ggthemes)
p + theme_tufte()

p + theme_economist()