温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2021-07-10 14:30:10 来源:亿速云 阅读:1605 作者:chen 栏目:大数据

这篇文章主要介绍“如何用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画环状柱形图”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI