ggplot2 tips

General usages, click here to see list of opts
  1. theme_get() will show you the “hidden” options that you can use in theme()
  2. ?opts or ?themes see lists of opetions
  3. args(function_name) will list the arguments for this function
  4. The alpha channel is normally used as an opacity channel. If a pixel has a value of 0% in its alpha channel, it is fully transparent (and, thus, invisible), whereas a value of 100% in the alpha channel gives a fully opaque pixel (traditional digital images).
Common variable
  • Two dots variable

The two dots are a visual indicator highlighting that variable is not present in the original data, but has been computed by the statistic.

  • ..density..: bin_count / sum(count)
  • ..scaled..: bin_count / max(count)
  • ..ndensity..: bin_density / max(abs(density))
  • ..ncount..: bin_count / max(abs(count))
  • count: number of points in bin
  • density: density of points in bin, scaled to integrate to 1
  • ncount: count, scaled to maximum of 1
  • ndensity: density, scaled to maximum of 1
#chang axis size
theme(axis.text.x=element_text(size=X)) + theme(axis.text.y=element_text(size=X))

#Erase labels and ticks on x,y axis  
p + theme(axis.ticks=element\_blank(), axis.text.x=element\_blank(), \  
axis.text.y=element\_blank(),axis.ticks.x = element\_blank())  
Add Title, xlab, ylab
#wrap the title, you can use "\n" to move the remaining text to a new line:
#ggplot2 doesn't have "subtitle" functionality. 
#But you can use the \n term in any of the labels to drop down a line.
theme(title="text \n more text")
xlab(NULL) + ylab(NULL)
# A scatterplot with regular (linear) axis scaling
sp <- ggplot(dat, aes(xval, yval)) + geom_point()

# log2 scaling of the y axis (with visually-equal spacing)
library(scales) # Need the scales package
sp + scale_y_continuous(trans=log2_trans())

# log2 coordinate transformation (with visually-diminishing spacing)
sp + coord_trans(y="log2")

#scale_y_log2() will do the transformation first 
#and then calculate the geoms
#coord_trans() will do the opposite: calculate the geoms first, 
#and then transform the axis.
#So sometimes you need coord_trans(ytrans = "log2") instead of #
#scale_y_log2() to avoid data loss.
Options for legends
theme(legend.key.width=unit(1, "in"),
legend.text = theme_text(size=30),
legend.title=element_blank(), #no legend title
legend.key=element_blank(), #no border for legend
legend.position="none"  #"right","left","top"
#0.08 means right away from y-axis, 
#0.8 means above from x-axis, 
#relative to the size of picture
legend.direction = "vertical",
legend.justification = "center"
Set the order of legends or other variables
#Default, ggplot2 uses alphabetical order. #
#One can change it by given vectors
foomelt$COG <- factor(foomelt$COG, levels = c("first","second",...,"last"), ordered=T)
foomelt$COG <- factor(foomelt$COG, levels = c("first","second",...,"last"))
Use facets to set the layout of pictures, also check here
#ncol=6 means horizontally, six pics one row.
#nrow=6 means vertically, six pics one column.
#scale='free' means each pic can have different axis ranges.
#each pic can also have different x-axis by 'free_x' or y-axis by 'free-y'
facet_wrap(~Size,  ncol=6,  scale='free')   
facet_grid(. ~level, nrow=6, scale="fixed")
facet_grid(vertical_level ~ horizontal_level)
# Calculate correlation coefficient
with(mtcars,cor(wt, mpg, use = "everything", method = "pearson"))
[1] -0.8676594
#annotate the plot
+ geom_abline(intercept = 37, slope = -5) + 
geom_text(data = data.frame(), aes(4.5, 30, label = "Pearson-R = -.87"))
Remove grid line and use white background
theme(panel.grid.major = element_blank(), 
panel.grid.minor = element_blank())
#theme_blank for old version
[ggplot2 layout] (
geom_boxplot, also check oneline boxplot
1.Hidden outliers
2.Adjust ylim
stats <- boxplot.stats(value)$stats
ylim_zoomin <- c(stats[1]/2,stats[5]*2)
p + coord_cartesian(ylim=ylim_zoomin)
Manually set line type and line color, ref
ggplot(mort3, aes(x = year, y = BCmort, col = State, linetype = State)) +
  geom_line(lwd = 1) +
  scale_linetype_manual(values = c(rep("solid", 10), rep("dashed", 6))) +
  scale_color_manual(values = c(brewer.pal(10, "Set3"), brewer.pal(6, "Set3"))) +
  opts(title = "BC mortality") +

scale_color_manual(values = c("red",'green','blue')
scale_color_manual(values = c(rgb(255/255,0/255,0/255),
Manually set ytics and xtics
scale_x_continuous(breaks=round(seq(min(dat$x), mx(dat$x), by=0.5),1))

sclale_y_continuous(breaks=c(8,16,100,128,512,1000))  #any number
Color usage

[color bars] (

color plate

alipay.png WeChatPay.png




生信的作用越来越大,想学的人越来越多,不管是为了以后发展,还是为了解决眼下的问题。但生信学习不是一朝一夕就可以完成的事情,也许你可以很短时间学会一个交互式软件的操作,却不能看完程序教学视频后就直接写程序。也许你可以跟着一个测序分析流程完成操作,但不懂得背后的原理,不知道什么...… Continue reading


Published on January 01, 2100


Published on January 01, 2100