ggplot2分组条形图饼图箱线图

2022-10-25 19:58:35 浏览数 (1)

一、分组条形图

代码语言:javascript复制
x <- read.csv("sv_distrubution.csv",header = T)  
x  
# svs <- x %>% tidyr::pivot_longer(cols = 2:5,names_to = 'variation')
svs <- x %>% gather(key = Variation,value =Number,-X)
ggplot(data = svs,aes(x=X,y=Number)) geom_bar(stat = "identity")
ggplot(data = svs,aes(x=X,y=Number,fill=Variation)) geom_bar(stat = "identity")

p <- ggplot(data = svs,aes(x=X,y=Number,fill=Variation)) geom_bar(stat = "identity") 
p   scale_x_discrete(limits=x$X)
p   scale_x_discrete(limits=x$X)   scale_fill_brewer(palette = 1,type = 'seq')
p   scale_x_discrete(limits=x$X)   scale_fill_brewer(palette = 2,type = 'seq')
p   scale_x_discrete(limits=x$X)   scale_fill_brewer(palette = 2,type = 'div')
p   scale_x_discrete(limits=x$X)   scale_fill_brewer(palette = 3,type = 'div')
p   scale_x_discrete(limits=x$X)   scale_fill_brewer(palette = 4,type = 'div')
p   scale_x_discrete(limits=x$X)   scale_fill_brewer(palette = 'Set2',type = 'div')
p   scale_x_discrete(limits=x$X)   scale_fill_brewer(palette = 'Set1',type = 'div')
p   scale_x_discrete(limits=x$X)   scale_fill_brewer(palette = 'Set1')
p   scale_x_discrete(limits=x$X)   scale_fill_brewer(palette = 'Blues')

ggplot(data = svs,aes(x=X,y=Number,fill=Variation)) geom_bar(stat = "identity",position='dodge')
ggplot(data = svs,aes(x=X,y=Number,fill=Variation)) geom_bar(stat = "identity",position='dodge2')
ggplot(data = svs,aes(x=X,y=Number,fill=Variation)) geom_bar(stat = "identity",position='fill')
ggplot(data = svs,aes(x=X,y=Number,fill=Variation)) geom_bar(stat = "identity",position='stack')

p   scale_x_discrete(limits=x$X)   scale_fill_brewer(palette = 'Set1') 
  labs(title ="SV Distribution",x="Chromosome Number",y="SV Numbers")  
  theme(legend.position = 'bottom',plot.title = element_text(hjust = 0.5))

ggplot2 绘制基因组 SV 突变堆叠条形图

代码语言:javascript复制
p   scale_x_discrete(limits=x$X)   scale_fill_brewer(palette = 'Set1') 
  labs(title ="SV Distribution",x="Chromosome Number",y="SV Numbers")  
  theme(legend.position = 'bottom',plot.title = element_text(hjust = 0.5))  
  coord_polar()  guides(fill='none')

二、饼图

代码语言:javascript复制
m <- read.table("Species.txt",sep = 't',header = FALSE)
head(m)
sum(m$V3)
name <- paste(m[,1],m[,2],'n',m$V3,'%')
name
ggplot(data = m,aes(x = "",y=V3,fill=name)) geom_bar(stat = 'identity')  
  coord_polar(theta = 'y') guides(fill = 'none') scale_fill_brewer(palette = 'Set1')
# y <- paste(m[,1],m[,2])
# x <- data.frame(name=y,values=m$V3/sum(m$V3))
# p <- ggplot(data = x,aes(x = "",y=values,fill=name)) geom_bar(stat = "identity",width = 1) guides(fill='none')
# p
# p coord_polar(theta = 'y') labs(x = '', y = '', title = '')

ggplot2 绘制饼图

三、箱线图

代码语言:javascript复制
head(ToothGrowth)
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
#按提供药物种类分组
ggplot(data = ToothGrowth,aes(x=supp,y=len,fill=supp)) geom_boxplot()
#按剂量分组
ggplot(data = ToothGrowth,aes(x=dose,y=len,group=dose,fill=dose)) geom_boxplot()
#两组
ggplot(data = ToothGrowth,aes(x=dose,y=len,group=supp:dose,fill=supp:dose)) geom_boxplot()
ggplot(data = ToothGrowth,aes(x=dose,y=len,group=supp:dose,fill=supp)) geom_boxplot()
ggplot(data = ToothGrowth,aes(x=dose,y=len,group=supp:dose,fill=dose)) geom_boxplot()
ggplot(data = ToothGrowth,aes(x=supp,y=len,group=supp:dose,fill=dose)) geom_boxplot()

#box图加抖动点
p<- ggplot(data = ToothGrowth,aes(x=dose,y=len,fill=dose)) geom_boxplot() 
  geom_jitter(width = 0.1)
p labs(title = 'ToothGrowth VC vs OJ',x='DOSE',y='Length') 
  scale_fill_brewer(palette = 'Set1')  
  theme(plot.title = element_text(hjust = 0.5))  
  theme(legend.position = 'bottom')

ggplot2 绘制箱线图加抖动的点

代码语言:javascript复制
#分面
ggplot(data = ToothGrowth,aes(x=supp,y=len,group=supp:dose,fill=supp)) geom_boxplot() 
  scale_fill_brewer(palette = 'Set1') 
  facet_grid(~ supp,scales = 'free')

ggplot2 绘制分面箱线图

写在最后:有时间我们会努力更新的。大家互动交流可以前去论坛,地址在下面,复制去浏览器即可访问,弥补下公众号没有留言功能的缺憾。

代码语言:javascript复制
bioinfoer.com

有些板块也可以预设为大家日常趣事的分享等,欢迎大家来提建议。

0 人点赞