ggplot2

Basics

  • ggplot(), create a new ggplot
    • ggplot takes data frame instead of vectors as arguments
  • aes(), construct aesthetic mappings
  • ggsave(), save a ggplot
  • qplot, similar as plot()
  • theme, customize the non-data components of the plots
  • labs, modify axis, legend, and plot labels
  • scale, specify aesthetic values
  • guides, axes and legends that help readers interpret plots
In [12]:
df2 <- data.frame(sex = rep(c("Female", "Male"), each=3),
                  time=c("breakfeast", "Lunch", "Dinner"),
                  bill=c(10, 30, 15, 13, 40, 17) )

# setup base
gg <- ggplot(df2, aes(x=time, y=bill, group=sex))
# line plot
gg <- gg + geom_line(aes(linetype=sex, color=sex), size = 2) # auto linetype and auto color
# scatter plot
gg <- gg + geom_point(aes(colour=sex), size = 3) # auto color
# manually setup aesthetic values
gg <- gg + scale_linetype_manual(values=c("solid", "dotted"))
gg <- gg + scale_colour_manual(values=c('red', 'blue'))
# setup legend
gg <- gg + guides(color=guide_legend("SEX"), linetype=guide_legend("SEX"))
gg <- gg + theme(legend.position = c(.15, .85), legend.margin = margin(6, 6, 6, 6))
gg <- gg + theme(legend.box.background = element_rect(colour = 'blue')) # legend box
gg <- gg + theme(legend.key = element_rect(fill = "white", colour = "black")) # legend key
gg <- gg + theme(legend.key.size = unit(1, 'cm'))
gg <- gg + theme(legend.text = element_text(size = 8, colour = "red")) # legend text
gg <- gg + theme(legend.title = element_text(face = "bold", size = 18)) # legend title
# setup axis
gg <- gg + theme(axis.title = element_text(size = 16, face = "bold")) # labels
gg <- gg + theme(axis.text.x.bottom = element_text(colour = 'red'), axis.text.y.left = element_text(colour = 'blue'))
# setup limit and tick interval
gg <- gg + scale_y_continuous(limits = c(0, 40), breaks = seq(0, 40, by = 5))
gg <- gg + theme(axis.ticks.length = unit(0.25, 'cm'))
# setup background and grid
gg <- gg + theme(panel.grid = element_blank(), panel.background = element_rect(fill = "white", colour = "blue"))
gg <- gg + theme(plot.background = element_rect(fill = "gray", colour = "yellow", linetype = "dashed"))
# setup title
gg <- gg + labs(title = 'Dinning Cost') + theme(plot.title = element_text(size = rel(2)))
gg <- gg + theme(plot.title = element_text(hjust = 0.5, colour = 'red'))
# use complete theme
#gg <- gg + theme_linedraw()
print(gg)

Line plot

In [16]:
# create data frame
a = seq(0, 3.14*2, by = 0.1)
b = sin(a)
c = cos(a)
data = data.frame(
    value = a,
    sin = b,
    cos = c
)

library(ggplot2)
gg <- ggplot(data, aes(x=value))
gg <- gg + geom_line(aes(y=sin, color = "sin", linetype = 'sin'), size = 1) # setup color name
gg <- gg + geom_line(aes(y=cos, color = "cos", linetype = 'cos'),  size = 1)
gg <- gg + scale_linetype_manual(values=c(sin='dashed', cos='dotted'), name = "Line Type")
gg <- gg + scale_colour_manual(values=c(sin="blue", cos="red"), name = "Plot Color")
gg <- gg + theme(legend.key.size = unit(1, 'cm'))
gg <- gg + guides(color = guide_legend(title="Legend"), linetype = guide_legend(title="Legend"))
print(gg)

Scatter plot

In [20]:
options(scipen=999)  # turn-off scientific notation like 1e+48
library(ggplot2)
theme_set(theme_bw()) # use pre-defined theme bw
gg <- ggplot(diamonds, aes(x=carat, y=price)) + geom_point(aes(color=cut), shape = 1, stroke=1)
gg <- gg + geom_smooth(method="loess", se=F)
print(gg)

Jitter Plot

In [23]:
library(ggplot2)
data(mpg, package="ggplot2")
theme_set(theme_bw())

g <- ggplot(mpg, aes(cty, hwy))
g <- g + geom_jitter(width = .5, size=1) # the overlapping points are randomly jittered around its original position
print(g)

Counts plot

In [25]:
library(ggplot2)
data(mpg, package="ggplot2")
theme_set(theme_bw())

g <- ggplot(mpg, aes(cty, hwy))
g <- g + geom_count(col="tomato3", show.legend=F)
print(g)

Bubble Plot

In [28]:
library(ggplot2)
data(mpg, package="ggplot2")
theme_set(theme_bw())

mpg_select <- mpg[mpg$manufacturer %in% c("audi", "ford", "honda", "hyundai"), ]
g <- ggplot(mpg_select, aes(displ, cty))
g <- g + geom_jitter(aes(col=manufacturer, size=hwy))
print(g)

Box chart

In [32]:
library(ggplot2)
ToothGrowth$dose <- as.factor(ToothGrowth$dose)

p <- ggplot(ToothGrowth, aes(x=dose, y=len))  
p <- p + geom_boxplot(outlier.colour="red", outlier.shape=8, outlier.size=4, notch=TRUE)
print(p)

Tufte Boxplot

In [24]:
library(ggplot2)
theme_set(theme_bw())

g <- ggplot(mpg, aes(class, cty))
g <- g + geom_violin()
print(g)

Histogram Plot

In [36]:
library(ggplot2)
gg <- ggplot(diamonds, aes(carat, stat(density), fill = cut)) 
gg <- gg + geom_histogram(binwidth = 0.2)
print(gg)

Density plot

In [22]:
library(ggplot2)
theme_set(theme_classic())

g <- ggplot(mpg, aes(cty))
#g <- g + geom_histogram(aes(fill=factor(cyl)), binwidth = 0.5)
g <- g + geom_density(aes(fill=factor(cyl)), alpha=0.8) # a smoothed version of the histogram
print(g)

Correlogram

In [9]:
library(ggplot2)
library(ggcorrplot)
corr <- round(cor(mtcars), 1)
gg <- ggcorrplot(corr, lab = TRUE, lab_size = 3, method="circle")
print(gg)

Diverging bars

In [20]:
library(ggplot2)
theme_set(theme_bw())

mtcars$`car_name` <- rownames(mtcars)
mtcars$mpg_z <- round((mtcars$mpg - mean(mtcars$mpg))/sd(mtcars$mpg), 2)
mtcars$mpg_type <- ifelse(mtcars$mpg_z < 0, "below", "above")
mtcars <- mtcars[order(mtcars$mpg_z), ]
mtcars$`car_name` <- factor(mtcars$`car_name`, levels = mtcars$`car_name`)

gg <- ggplot(mtcars, aes(x=`car_name`, y=mpg_z, label=mpg_z))
gg <- gg + geom_bar(stat='identity', aes(fill=mpg_type), width=.5)
gg <- gg + coord_flip()
print(gg)

Area Chart

In [36]:
library(ggplot2)
data("economics", package = "ggplot2")

economics$returns_perc <- c(0, diff(economics$psavert)/economics$psavert[-length(economics$psavert)])
gg <- ggplot(economics[1:100, ], aes(date, returns_perc))
gg <- gg + geom_area()
print(gg)

Contour Map

In [27]:
gg <- ggplot(faithfuld, aes(waiting, eruptions))
gg <- gg + geom_raster(aes(fill = density), interpolate = TRUE)
print(gg)

Pie Chart

In [32]:
library(ggplot2)
theme_set(theme_classic())

pie <- ggplot(mpg, aes(x = "", fill = factor(class))) 
pie <- pie + geom_bar(width = 1) 
pie <- pie + coord_polar(theta = "y", start=0)
print(pie)