Yulong Niu

个人博客

R ggplot2 notes

Posted at — Oct 2, 2013

R Package ggplot2 Notes

1. Basic grammar

1.1 Plot types

The R package ggplot2 is a famous plot tool for high quality scientific figures. The ggplot2 style figures are widely seen in papers published in high quality journals such as PNAS, Nature and Cell.

The input data should be in data frame form, and it is easily to use the function as.data.frame(). “+” is used to connect different plot statement. A typical ggplot2 plot statement is like:

require('ggplot2')
ggplot(data=mpg, mapping=aes(x=cty, y=hwy, colour=factor(year))) +
geom_point() + stat_smooth()

ggplot(): data is a data.frame class object. mapping is an aes() function to specify the X-axis and Y-axis. When a aes() is used, a figure legend will be added. If we do not want the legends appear, use show_guide = FALSE in geom_XXX or stat_XXX

geom_point(): is used to plot points with the attributes x, y, alpha, colour, fill, shape, size.

geom_line(): is used to plot points with the attributes x, y, alpha, colour, fill, shape, size.

geom_bar(): bar plot. stat = 'identity' for draw the identical, hjust and vjust is to adjust the x and y axis distance. coord_flip() to reverse X and Y axis. position = 'dodge' to set position of two bars, can be set as ‘dodge’, ‘stack’, ‘fill’ and ‘identity’.

geom_box(): boxplot.

geom_tile(): fill blocks.

# example
require('ggplot2')
p <- ggplot(mtcars, aes(factor(cyl), mpg))
# basic plot
p + geom_boxplot()
# add colors to boxes
p + geom_boxplot(aes(fill = factor(cyl)))
# change default colors
p + geom_boxplot(aes(fill = factor(cyl))) + scale_fill_manual(values = c('red', 'green', 'blue'))

geom_rect(mapping = NULL, data = NULL, stat = "identity", position = "identity", ...): plot rectangles. In aes(), xmin, xmax, ymin, and ymax are necessary. inherit.aes = FALSE may be used if new data is applied.

# example
ggplot(mtcars) +
  geom_density(aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) + 
  geom_rect(data=mtcars[1,], aes(xmin=100, xmax=200, ymin=0,ymax=Inf), fill="red", alpha=0.2)

1.2 Statistics

geom_smooth(): is used for the add smooth line with the method lm, glm, gam, loess and rlm. se = TRUE is to display the confident region. The following aesthetics x, y, alpha, colour, fill, linetype, size, weight could be added.

stat_boxplot(): plot error lines in boxplot.

1.3 Add elements

xlab(): change X axis label, set xlab('') to remove the X axis label; ylab(): change Y axis label; ggtitle(): add figure title; scale_y_continuous(limits=c(0, 20)) and scale_x_continuous(limits=c(0, 20)) to adjust range of X and Y axis.

geom_abline(intercept = 37, slope = -5): to add line.

geom_hline and geom_vline: to add horizontal and vertical lines.

# example 
p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
geom_vline(xintercept = 1:5, colour="green", linetype = "longdash")
# ggplot2 line type
d <- data.frame(lt=c('blank', 'solid', 'dashed', 'dotted', 'dotdash', 'longdash', 'twodash', '1F', 'F1', '4C88C488', '12345678'))
ggplot() +
  scale_x_continuous(name='', limits=c(0,1), breaks=NULL) +
  scale_y_discrete(name='linetype') +
  scale_linetype_identity() +
  geom_segment(data=d, mapping=aes(x=0, xend=1, y=lt, yend=lt, linetype=lt))

geom_text(): to add text. Set parse = TRUE to use expression and greek letters.

scale_fill_discrete(..., values): change labels. name to reset label names, labels to reset labels.

scale_shape_manual(..., values): change the shape of points.

scale_linetype_manual(..., values): change the types of lines. line referring R plot.name, value, labels are used to change value.

scale_color_manual is used for change the colors. Please refer to Useful color palette, Introduction of ggplot2 colors, and R Color Chart . The default ggplot2 colors are generated from the “scales” package, for example the default “hue pallet” could be view as show_col(hue_pal(h = c(0, 360) + 15, c = 100, l = 65, h.start = 0, direction = 1)(9))

scale_fill_manual(..., alues) to change filled colors.

theme is used for exact control. legend.position='none' to remove the side legend.

2. Save plot

The function ggsave() is used to save the screen plot to file. print() is also applied like:

pdf('testfile.pdf')
q <- ggplot()
print(q)
dev.off()

3. Other issues

3.1 Plot mutiple ggplot2

Use gridExtra package to plot multiple ggplot2 figures in the one figure.

# example
require('gridExtra')
# save ggplot object into a list like "plotList"
do.call(grid.arrange, plotList)

参考网址

更新记录

2014年8月28日