# 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:

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.

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.

### 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.

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.

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:

## 3. Other issues

### 3.1 Plot mutiple ggplot2

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

2014年8月28日