R语言基础绘图教程——第7章:小提琴图

2019-08-15 17:19:08 浏览数 (1)

R基础教程可先阅读:R语言编程

ggplot2绘制小提琴图

代码语言:javascript复制
library(ggplot2)
library(gplots)
library(RColorBrewer)
options(StringAsFactors=FALSE)

#read in the data file
data = read.table('violin_plot.txt', sep="t", header=T)
#take a glance at the data
head(data)
dim(data)
data$EXP = log(data$ACIN1,2)
#start to draw the violin plot
p <- ggplot(data,aes(CANCER,EXP)) 
  geom_violin(adjust=1,trim=T) 
  ggtitle("Expression of ACIN1 in different cancers")
print(p)
代码语言:javascript复制
#changeing params
p <- ggplot(data,aes(CANCER,EXP)) 
  geom_violin(adjust=1,trim=T) 
  geom_boxplot(width=0.3,fill="black",alpha=1,outlier.colour=NA) 
  stat_summary(fun.y=mean,geom="point",fill='white',shape=21,size=3) 
  theme_bw() 
  theme(axis.text.x=element_text(angle=60,hjust=1,size=8),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) 
  ylab('Expression level') 
  xlab(NULL) 
  ggtitle("Expression of ACIN1 in different cancers")
print(p)
代码语言:javascript复制
#changing params again, mainly change the color and sort the color by mean value
#library(RColorBrewer)
color1 = brewer.pal(12,"Set3")
color2 = brewer.pal(9,"Set1")
color = c(color1,color2)
p <- ggplot(data,aes(CANCER,EXP)) 
  geom_violin(adjust=1,trim=T) 
  geom_boxplot(width=0.3,fill=color,alpha=1,outlier.colour=NA) 
  stat_summary(fun.y=mean,geom="point",fill='white',shape=21,size=2) 
  theme_bw() 
  theme(axis.text.x=element_text(angle=60,hjust=1,size=10),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) 
  ylab('Expression level') 
  xlab(NULL) 
  ggtitle("Expression of ACIN1 in different cancers")
print(p)

0 人点赞