这个是很长时间之前写的内容了,忘记了有没有在公众号发过,正好有人在公众号留言问这个环形的柱形图应该如何实现,就想到了这篇笔记,在公众号发一下
偶然间找到了一份教程利用ggplot2绘制环状柱形图,个人感觉非常适合用来展示叶绿体基因组蛋白编码基因的dn/ds值,因为不仅能够通过柱状图的高低来比较dn/ds值的大小,还能够通过环状展示蛋白编码基因在叶绿体基因组上所处的位置
A circular barplot is a barplot where bars are displayed along a circle instead of a line.
https://www.r-graph-gallery.com/297-circular-barplot-with-groups/
#准备数据
df<-data.frame(individual=paste("Mister",seq(1,60),sep=""),value=sample(seq(10,100),60,replace=T))
df$id<-seq(1,nrow(df))
library(ggplot2)
#简易柱形图
p<-ggplot(df,aes(x=as.factor(id),y=value))+geom_bar(stat="identity",fill=blue)#目前还是不太清楚stat参数的作用
Rplot06.png
#简易环状柱形图
p+coord_polar()
Rplot05.png
p+ylim(-100,120)+coord_polar()
#添加标签
p+coord_polar()+ylim(-100,120)+
geom_text(aes(x=id,y=value+20,label=individual),size=3)+
theme_minimal()+ylab("")+
theme(axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())
Rplot07.png
标签看起来有些乱,自己没有想到解决办法,模仿教程中的解决办法:为参数hjust和angle赋予数据来调控标签的位置
df$angle<-96-df$id*6
ggplot(df,aes(x=as.factor(id),y=value))+
geom_bar(stat="identity",fill=alpha("blue",0.7))+
coord_polar()+ylim(-100,120)+
geom_text(aes(x=id,y=value+20,label=individual,angle=angle),
size=3,hjust=0.2)+
theme_minimal()+ylab("")+xlab("")+
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.text.x = element_blank())
Rplot08.png
#在完善一下
df$angle1<-ifelse(df$id<=30,96-df$id*6,96-df$id*6+180)
df$hjust<-ifelse(df$id<=30,0.2,1)
ggplot(df,aes(x=as.factor(id),y=value))+
geom_bar(stat="identity",fill=alpha("blue",0.7))+
coord_polar()+ylim(-100,120)+
geom_text(aes(x=id,y=value+20,label=individual,
angle=angle1,hjust=hjust),size=3)+
theme_minimal()+ylab("")+xlab("")+
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.text.x = element_blank(),
panel.grid = element_blank())
Rplot09.png
叶绿体基因组通常是典型的四部分结构,如何把上图改成四部分然后添加四种不同的颜色,原教程提供的解决办法是添加缺失值,画图时就会出现空白的部分从而达到分割的目的
df1<-data.frame(individual=paste("Mister",seq(1,60),sep=""),
value=rep(c(sample(60:100,9,replace=T),NA),6))
df1$id<-seq(1,nrow(df1))
df1
df1$angle<-df$angle1
df1$hjust<-df$hjust
df1
df1$fill<-c(rep("A",10),rep("B",10),rep("C",10),rep("D",10),rep("E",10),rep("F",10))
ggplot(df1,aes(x=as.factor(id),y=value))+
geom_bar(stat="identity",aes(fill=fill))+
coord_polar()+ylim(-100,120)+
geom_text(aes(x=id,y=value+20,label=individual,
angle=angle,hjust=hjust),size=3)+
theme_minimal()+ylab("")+xlab("")+
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.text.x = element_blank(),
panel.grid = element_blank(),
legend.position="none")+
scale_fill_manual(values=c("red","yellow","blue","green","orange","skyblue"))
Rplot10.png
######小知识点:ggplot2更改绘图区空白大小 https://ggplot2.tidyverse.org/reference/element.html
theme(plot.margin=unit(c(1,1,1,1),'cm'))
#更改里面的数值即可
#比如可以比较一下以下两条命令的区别
df<-data.frame(A=1:10,B=10:1)
p<-ggplot(df,aes(x=A,y=B))+geom_point()
p+theme(plot.margin=unit(1,1,1,1),'cm')
p+theme(plot.margin=unit(2,2,2,2),'cm')
欢迎大家关注我的公众号小明的数据分析笔记本
小明的数据分析笔记本 公众号 主要分享:1、R语言和python做数据分析和数据可视化的简单小例子;2、园艺植物相关转录组学、基因组学、群体遗传学文献阅读笔记;3、生物信息学入门学习资料及自己的学习笔记!