Session 3 Visualization

Objectives and Learning

Tools and Packages

Using ggplot2 for exploratory analysis

Visulaizing statistical inference

load("/home/data/session1.RData")
load("/home/data/session2.RData")
# install.packages("ggplot2")
require(ggplot2)
require(dplyr)
prices %>% ggplot(aes(x=as.Date(date,format="%Y-%m-%d"),y=price.usd)) + geom_point(stat="identity",position="identity") 

require(dplyr)
prices %>% ggplot(aes(x=as.Date(date,format="%Y-%m-%d"),y=price.usd)) + geom_point(aes(group=factor(year),colour=factor(year))) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + ylab("price") + xlab("date")+ geom_smooth(aes(group=factor(year)),method = "lm")

require(dplyr)
prices %>% ggplot(aes(x=year,y=price.cad)) + geom_boxplot(aes(group=year),outlier.colour = "red") + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + ylab("price") + xlab("date")

require(dplyr)
prices %>% ggplot(aes(x=price.cad,y=price.usd)) + geom_point(aes(group=factor(year),colour=factor(year)))+ theme(axis.text.x = element_text(angle = 90, hjust = 1)) + ylab("USD") + xlab("CAD") + geom_smooth(aes(group=factor(year)),method = "lm")

require("gridExtra")
empty <- ggplot()+geom_point(aes(1,1), colour="white") +
     theme(                              
       plot.background = element_blank(), 
       panel.grid.major = element_blank(), 
       panel.grid.minor = element_blank(), 
       panel.border = element_blank(), 
       panel.background = element_blank(),
       axis.title.x = element_blank(),
       axis.title.y = element_blank(),
       axis.text.x = element_blank(),
       axis.text.y = element_blank(),
       axis.ticks = element_blank()
     )



scatter <- prices %>% ggplot(aes(price.cad, price.usd)) + 
  geom_point(aes(color=factor(year))) + 
  scale_color_manual(values = c("orange", "purple","green")) + 
  theme(legend.position=c(1,1),legend.justification=c(1,1)) 

#marginal density of x - plot on top
plot_top <- prices %>% ggplot(aes(price.cad, fill=factor(year))) + 
  geom_density(alpha=.5) + 
  scale_fill_manual(values = c("orange", "purple","green")) + 
  theme(legend.position = "none")

#marginal density of y - plot on the right
plot_right <- prices %>% ggplot(aes(price.usd, fill=factor(year))) + 
  geom_density(alpha=.5) + 
  coord_flip() + 
  scale_fill_manual(values = c("orange", "purple","green")) + 
  theme(legend.position = "none") 

#arrange the plots together, with appropriate height and width for each row and column
grid.arrange(plot_top, empty, scatter, plot_right, ncol=2, nrow=2, widths=c(4, 1), heights=c(1, 4))

Interactive Graphics using Java Script based plots

## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:graphics':
## 
##     layout