Spring 2018 R Open Lab: Advanced Visualization

Apr 18
Today we will explore the advanced data visualization in R. First, we will review the basic graphic functions in R and learn how to use additional parameters to achieve different goals. Then, we will introduce the powerful package ggplot2. Here are the codes:
# Quick review of basic visualization
library(ggplot2)
plot(diamonds$carat, diamonds$price, main = “Price vs Carat”, xlab = “Carat”, ylab = “Price”)
pairs(~carat+depth+table+price, data = diamonds)
barplot(table(diamonds$cut))
hist(diamonds$price, breaks = 100)
boxplot(diamonds$price~diamonds$cut)
pie(c(10, 2, 4, 7), c(“A”, “B”, “C”, “D”))
d <- diamonds[sample(1:nrow(diamonds), 1000), ]
# Plot by factor
plot(d$carat, d$price, col = d$cut)
# Add legend
legend(“bottomright”,
legend = levels(diamonds$cut),
fill = 1:5, cex = 0.4)
# Add line
ols <- lm(price~carat, data = d)
abline(ols, lty = 2, lwd = 2)
# Add point
points(2, 2500, pch = 3)
# Add text
text(2, 2000, “new point”)
# Useful parameters
pch
main
xlab
ylab
lty # line type
lwd # line width
cex # character expand
col
 
# ggplot2 package
p <- ggplot(data = d)
p + geom_point(mapping = aes(x = carat, y = price,
col = d$cut))
# facet
p+geom_point(mapping = aes(x = carat, y = price))+
facet_wrap(~cut, nrow = 2)
p+geom_point(mapping = aes(x = carat, y = price))+
facet_grid(~cut)
# regression line
p+geom_point(mapping = aes(x = carat, y = price))+
geom_smooth(mapping = aes(x = carat, y = price), method = “auto”)
# other functions to explore
ggplot(data = )+
geom_histogram(mapping = aes())+
geom_bar(mapping = aes())+
stat_function(mapping = , fun = )+
labs(title = , x = , y = )+
geom_text()+
geom_abline()+
geom_boxplot()


Thank you all for showing up. If you have further questions regarding topics covered in the material, please feel free to drop by during next week’s lab or email me or leave a comment.
See you all next week!