- What is a (data) plot?
- What are the three most important data plots?
2016-02-24
How would you describe this plot?
Elements of a plot
Additional components
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.
qplot(date, AUD, data=rates)
qplot(date, AUD, data=rates, geom="line")
qplot(date, AUD, data=rates, geom=c("line", "point"))
ggplot(data=rates, aes(x=date, y=AUD)) + geom_point() + geom_line()
ggplot(data=rates, aes(x=date, y=AUD)) + geom_line() + geom_line(aes(y=NZD), colour="blue") + geom_line(aes(y=GBP), colour="red")
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
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)
Set the linetype/shape to be different for the different currencies.
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)
qplot(AUD, NZD, data=rates.sub) + theme(aspect.ratio=1)
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
qplot(AUD, NZD, data=rates.sub, geom="path", colour=order(date)) + theme(aspect.ratio=1)
qplot(AUD, NZD, data=rates.sub, geom=c("density2d", "point")) + theme(aspect.ratio=1)
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)
Look up geom_rug in the ggplot2 cheat sheet and add the marginal distributions to a scatterplot of AUD and NZD.
qplot(AUD, data=rates.sub, geom="histogram")
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?
qplot(AUD, data=rates.sub, geom="density", fill=I("black"))
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")
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")
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
p + theme(legend.position = "bottom")
library(ggthemes) p + theme_tufte()
p + theme_economist()