The following is a script file containing all R code of all sections in this slide set.

Introduction to ggplot

data(iris)
library(ggplot2)
library(ggthemes)
ggplot(data = iris) +
    geom_point(mapping = aes(x=Petal.Length,y=Sepal.Length, colour=Species, shape=Species)) + 
    scale_colour_colorblind()
ggplot(data = <DATA_SOURCE>) +
    <GEOM_OBJECT>(mapping = aes(<MAPPINGS>)) + 
    <EVENTUALLY_OTHER_LAYERS>

Visualizing Amounts

library(ggplot2)
ggplot(data = filter(nba,Season=="2018_19"), mapping = aes(x=Team,y=Wins)) + geom_col()
ggplot(data = filter(nba,Season=="2018_19"), mapping = aes(x=reorder(Team,-Wins), y=Wins)) +
    geom_col() + xlab("Team")
ggplot(data = filter(nba,Season=="2018_19"), mapping = aes(x=reorder(Team, Wins), y=Wins)) +
    coord_flip() + geom_col() + xlab("Team")
ggplot(data = filter(nba,Wins/(Wins+Losses) > .3), mapping = aes(x=Season)) + geom_bar()
ggplot(filter(nba,Season=="2018_19"),
       aes(x=reorder(Team,Wins), y=Wins)) +
    geom_col() + coord_flip() + xlab("Team")
ggplot(filter(nba,Season=="2018_19"),
       aes(x=reorder(Team,Wins), y=Wins)) +
    geom_point() + coord_flip() + xlab("Team")
ggplot(nba,aes(x=Team,y=Wins,fill=Season)) + geom_col(position="dodge") + coord_flip()
ggplot(nba,aes(x=Team,y=Wins,fill=Season)) + geom_col() + coord_flip()
ggplot(nba,aes(x=Team,y=Wins)) + geom_col() + facet_wrap(~Season) + coord_flip()
ggplot(nba,aes(x=Season,y=Team,fill=Wins)) + geom_tile()

Visualizing Distributions

library(ggplot2)
data(iris)
ggplot(data = iris, mapping = aes(x=Sepal.Length)) + geom_histogram()
ggplot(iris,aes(x=Sepal.Length)) + 
    geom_histogram()
ggplot(iris,aes(x=Sepal.Length)) +
    geom_histogram(bins=10)
ggplot(iris,aes(x=Petal.Width)) + 
    geom_density(fill="orange",alpha=0.5)
ggplot(iris,aes(y=Petal.Length)) + geom_boxplot() + 
    theme(axis.ticks.x = element_blank(), axis.text.x = element_blank())
ggplot(iris,aes(x=Petal.Length)) + stat_ecdf() + ylab("Cumulative frequency")
ggplot(iris, aes(sample = Petal.Width)) + stat_qq() + stat_qq_line()
ggplot(iris,aes(x=Sepal.Length,  fill=Species)) + 
    geom_histogram(alpha=0.4)
ggplot(iris,aes(x=Sepal.Length)) + geom_histogram() + facet_wrap(~ Species)
library(ggthemes)
ggplot(iris,aes(x=Sepal.Length, fill=Species)) + 
    geom_density(alpha=0.4) + scale_fill_colorblind()
ggplot(iris,aes(x=Species,y=Sepal.Length)) + 
    geom_boxplot()
ggplot(iris,aes(x=Species,y=Sepal.Length)) + 
    geom_boxplot() + geom_jitter(color="orange",alpha=0.3,width=0.1)
ggplot(iris,aes(x=Species,y=Sepal.Length)) +  geom_violin()
ggplot(iris,aes(x=Species,y=Sepal.Length)) +  geom_violin(trim=FALSE) + 
    geom_boxplot(width=0.1) + geom_jitter(position=position_jitter(0.15),alpha=0.3)
ggplot(iris,aes(x=Petal.Length,color=Species)) + stat_ecdf()+ ylab("Cumulative frequency")

Visualizing Proportions

pie(dat$Seats, labels=dat$Party)
pie(dat$Seats, labels=dat$Party)
ggplot(mutate(dat,Prop=Seats/sum(dat$Seats)), 
       aes(x=Party,y=Prop)) + geom_col() + 
    ylab("Proportion")
ggplot(mutate(dat,Prop=Seats/sum(dat$Seats)), 
       aes(x="",y=Prop,fill=Party)) + geom_col() 

X-Y Associations

ggplot(iris, aes(x=Petal.Length, y= Sepal.Width)) + geom_point()
library(ggthemes)
ggplot(iris, aes(x=Petal.Length, y= Sepal.Width, color=Species, shape=Species)) + 
    geom_point() + scale_color_colorblind()
ggplot(iris, aes(x=Petal.Length, y= Sepal.Width)) + 
    geom_point() + geom_smooth(method="loess", se=TRUE,level=0.95) + 
    facet_wrap(~ Species,scales = "free")

Multiple Associations

ggplot(iris, 
       aes(x=Petal.Length, y= Sepal.Width, size=Sepal.Length, color= Species, shape=Species)) + 
    geom_point(alpha=0.5) + scale_color_colorblind()
library(GGally)
ggpairs(iris)
ggpairs(iris,columns = 1:4,ggplot2::aes(color=Species,alpha=0.5))
data(algae,package="DMwR2")
library(dplyr)
library(forcats)
mutate(algae, 
       size = fct_relevel(size,c("small","medium","large")),
       speed = fct_relevel(speed,c("low","medium","high"))) %>%
ggplot(aes(x = size, y = speed, fill = a1)) + 
    geom_tile() +xlab("River Size") + ylab("River Speed")
mutate(algae, 
       size = fct_relevel(size,c("small","medium","large")),
       speed = fct_relevel(speed,c("low","medium","high")),
       season = fct_relevel(season,c("spring","summer","autumn","winter"))) %>%
ggplot(aes(x = size, y = speed, fill = a1)) + 
    geom_tile() + facet_wrap(~ season) + xlab("River Size") + ylab("River Speed")
library(GGally)
ggcorr(iris[,-5])
library(GGally)
ggcorr(iris[,-5],label = TRUE)
library(GGally)
ggparcoord(iris,columns = 1:4,groupColumn = "Species", alphaLines = 0.5)