跟着NC学作图

编程入门 行业动态 更新时间:2024-10-26 21:22:30

跟着<a href=https://www.elefans.com/category/jswz/34/1770043.html style=NC学作图"/>

跟着NC学作图


本教程绘制的图形是来自NC期刊的图形,我不知道叫什么图形,但是仔细一看就是的属于“环状柱状图”,那就是这样叫吧!
看着还算是比较新颖的,那就学一下吧!!

绘图

加载R包

library(ggplot2)

加载数据

绘制基础图形

p <- ggplot(data, aes(x=as.factor(id), y=est, fill=group)) +       # Note that id is a factor. If x is numeric, there is some space between the first bargeom_bar(aes(x=as.factor(id), y=est, fill=group), stat="identity", alpha=1) +geom_errorbar(aes(ymin = est-se, ymax  = est+se),width = 0.2,position = position_dodge(.9), size  = .5, alpha=1)

增加一个val

p2 <- p +geom_segment(data=grid_data, aes(x = end, y = 1, xend = start, yend = 1), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +geom_segment(data=grid_data, aes(x = end, y = 0.8, xend = start, yend = 0.8), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +geom_segment(data=grid_data, aes(x = end, y = 0.6, xend = start, yend = 0.6), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +geom_segment(data=grid_data, aes(x = end, y = 0.4, xend = start, yend = 0.4), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +geom_segment(data=grid_data, aes(x = end, y = 0.2, xend = start, yend = 0.2), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +geom_segment(data=grid_data, aes(x = end, y = 0, xend = start, yend = 0), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE )

在val中添加数字

p3 <- p2 +annotate("text", x = rep(max(data$id),6), y = c(0, 0.2, 0.4, 0.6, 0.8, 1), label = c("0", "0.2", "0.4", "0.6", "0.8", "1") , color="black", size=4 , angle=0, hjust=1) 

将y轴进行延伸

p4 <- p3+ geom_bar(aes(x=as.factor(id), y=est, fill=group), stat="identity", alpha=0.5) +ylim(-1,3)

进行美化

p5 <- p4 + theme_minimal() +theme(legend.position = "right",legend.title = element_blank(),legend.text = element_text(size=12),axis.text = element_blank(),axis.title = element_blank(),panel.grid = element_blank())

变成环状图形

使用coord_polar()函数进行转换

p6 <- p5 + coord_polar()

颜色美化

p7 <- p6 + scale_fill_manual(breaks = c("Intellectual disabilities","Communication disorders", "ASD","ADHD", "Specific learning disorders","Dyslexia-related phenotypes","Dysgraphia-related phenotypes","Dyscalculia-related phenotypes","Motor disorders"),values = c("#184e77","#1e6091","#1a759f","#168aad","#34a0a4","#52b69a","#76c893","#99d98c","#b5e48c"))+ggtitle("Heritability of sub-categories of Neurodevelopmental Disorders")

标注映射到柱子上

p8 <- p7 + geom_text(data=label_data, aes(x=id, y= est+se+0.2, label=pheno, hjust=hjust), color="black", alpha=1, size=4, angle= label_data$angle, inherit.aes = FALSE ) 


完整代码

ggplot(data, aes(x=as.factor(id), y=est, fill=group)) +       # Note that id is a factor. If x is numeric, there is some space between the first bargeom_bar(aes(x=as.factor(id), y=est, fill=group), stat="identity", alpha=1) +geom_errorbar(aes(ymin = est-se, ymax  = est+se),width = 0.2,position = position_dodge(.9), size  = .5, alpha=1)+# 添加 valgeom_segment(data=grid_data, aes(x = end, y = 1, xend = start, yend = 1), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +geom_segment(data=grid_data, aes(x = end, y = 0.8, xend = start, yend = 0.8), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +geom_segment(data=grid_data, aes(x = end, y = 0.6, xend = start, yend = 0.6), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +geom_segment(data=grid_data, aes(x = end, y = 0.4, xend = start, yend = 0.4), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +geom_segment(data=grid_data, aes(x = end, y = 0.2, xend = start, yend = 0.2), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +geom_segment(data=grid_data, aes(x = end, y = 0, xend = start, yend = 0), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +# 添加val中的数字annotate("text", x = rep(max(data$id),6), y = c(0, 0.2, 0.4, 0.6, 0.8, 1), label = c("0", "0.2", "0.4", "0.6", "0.8", "1") , color="black", size=4 , angle=0, hjust=1) +geom_bar(aes(x=as.factor(id), y=est, fill=group), stat="identity", alpha=0.5) +ylim(-1,3) +theme_minimal() +theme(legend.position = "right",legend.title = element_blank(),legend.text = element_text(size=12),axis.text = element_blank(),axis.title = element_blank(),panel.grid = element_blank()) +## 变环coord_polar() + ## 柱子标注scale_fill_manual(breaks = c("Intellectual disabilities","Communication disorders", "ASD","ADHD", "Specific learning disorders","Dyslexia-related phenotypes","Dysgraphia-related phenotypes","Dyscalculia-related phenotypes","Motor disorders"),## 柱子颜色values = c("#184e77","#1e6091","#1a759f","#168aad","#34a0a4","#52b69a","#76c893","#99d98c","#b5e48c"))+ggtitle("Heritability of sub-categories of Neurodevelopmental Disorders")+
## 将标注映射到柱子上geom_text(data=label_data, aes(x=id, y= est+se+0.2, label=pheno, hjust=hjust), color="black", alpha=1, size=4, angle= label_data$angle, inherit.aes = FALSE ) 

ENDING !


往期文章:
1. 最全WGCNA教程(替换数据即可出全部结果与图形)

WGCNA分析 | 全流程分析代码 | 代码一

WGCNA分析 | 全流程分析代码 | 代码二

WGCNA分析 | 全流程代码分享 | 代码三

2. 精美图形绘制教程

精美图形绘制教程


话说公众号需要标星,这样公众号的内容你才不会错过。那么,我们也动手标一下吧。


小杜的生信筆記 ,主要发表或收录生物信息学的教程,以及基于R的分析和可视化(包括数据分析,图形绘制等);分享感兴趣的文献和学习资料!!

更多推荐

跟着NC学作图

本文发布于:2023-06-20 23:57:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/808882.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!