资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

如何用R语言ggplot2画环状柱形图

这篇文章主要介绍“如何用R语言ggplot2画环状柱形图”,在日常操作中,相信很多人在如何用R语言ggplot2画环状柱形图问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何用R语言ggplot2画环状柱形图”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站建设、成都网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的景洪网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!


偶然间找到了一份教程利用ggplot2绘制环状柱形图,个人感觉非常适合用来展示叶绿体基因组蛋白编码基因的dn/ds值,因为不仅能够通过柱状图的高低来比较dn/ds值的大小,还能够通过环状展示蛋白编码基因在叶绿体基因组上所处的位置

A circular barplot is a barplot where bars are displayed along a circle instead of a line.

 简易版的环状柱形图 就是这样似的
如何用R语言ggplot2画环状柱形图  
 接下来重复教程

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参数的作用
 
如何用R语言ggplot2画环状柱形图  
Rplot06.png
#简易环状柱形图
p+coord_polar()
 
如何用R语言ggplot2画环状柱形图  
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())
 
如何用R语言ggplot2画环状柱形图  
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())
 
如何用R语言ggplot2画环状柱形图  
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())
 
如何用R语言ggplot2画环状柱形图  
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"))

 
如何用R语言ggplot2画环状柱形图  
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')

到此,关于“如何用R语言ggplot2画环状柱形图”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


当前名称:如何用R语言ggplot2画环状柱形图
网页地址:http://cdkjz.cn/article/igpjpg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220