查看原文
其他

R可视乎|环状柱状堆积图+分组+显著性

Editor's Note

希望以后可以在这个期刊发一篇文章💪

The following article is from 科研后花园 Author 王志山


灵感来源



代码





效果图

完整代码
1、设置工作环境:
#设置工作环境rm(list=ls())setwd("D:/桌面/环状柱状堆积图+分组+显著性")
#加载R包library(ggplot2) # Create Elegant Data Visualisations Using the Grammar of Graphicslibrary(tidyverse) # Easily Install and Load the 'Tidyverse'2、数据(随机编写,无实际意义):
#加载数据(随机编写,无实际意义)df <- read.table("data.txt", header = 1, check.names = F, sep = "\t")df$group <- factor(df$group,levels = c("M","N","G","F"))


3、数据处理:
##计算标签角度df2 <- as.data.frame(df[c(1:10,51:62,103:114,155:166,207:208),]) rownames(df2) <- 1:48df2$group <- factor(df2$group,levels = c("M","N","G","F"))df2$ID <- as.numeric(rownames(df2))number_of_bar <- nrow(df2)angle <- 90 - 360 * (df2$ID-0.5) /number_of_bardf2$hjust<-ifelse(angle < -90, 1, 0)df2$angle<-ifelse(angle < -90, angle+180, angle)#确定显著性标签位置result <- aggregate(value ~ group3, data = df, sum)#创建标签数据及位置df3 <- df2 %>% group_by(group) %>% summarize(start=min(ID), end=max(ID) - 4) %>% rowwise() %>% mutate(title=mean(c(start, end)))df3$group <- factor(df3$group,levels = c("M","N","G","F"))4、绘图(结合AI进行调整):
#绘图ggplot()+ #手动添加辅助线 geom_hline(yintercept = 0,lty="solid", color = "black",linewidth=0.6)+ geom_hline(yintercept = 20,lty="solid", color = "grey80")+ geom_hline(yintercept = 60,lty="solid", color = "grey80")+ geom_hline(yintercept = 40,lty="solid", color = "grey80")+ geom_hline(yintercept = 80,lty="solid", color = "grey80")+ geom_hline(yintercept = 100,lty="solid", color = "grey80")+ #柱状堆积图绘制 geom_col(df, mapping=aes(group3, value, fill = group), color = "grey20", linewidth = 0.5, width = 0.8)+ #y轴范围确定 scale_y_continuous(limits = c(-25,150))+ #颜色 scale_fill_manual(values = c("#4fbb98","#f46024","#dd6ab0","#7c8ebe"))+ #主题 theme_void()+ theme(legend.position = 'none' )+ #手动添加显著性 geom_text(data=df2, aes(x=ID, y=103, label=c("Contral", "ADOM", "LHy1","LHA", "LFA","SHy1","SHA","SFA","RHy1","RHA"," "," ", "Contral", "ADOM", "LHy1","LHA", "LFA","SHy1","SHA","SFA","RHy1","RHA"," "," ", "Contral", "ADOM", "LHy1","LHA", "LFA","SHy1","SHA","SFA","RHy1","RHA"," "," ", "Contral", "ADOM", "LHy1","LHA", "LFA","SHy1","SHA","SFA","RHy1","RHA"," "," "), hjust=hjust,color=group), fontface="bold", size=3, angle= df2$angle, inherit.aes = F)+ #手动添加标签 geom_text(data=df2, aes(x=ID, y=result$value+4, label=c("", "", "***","", "","","**","","","","","", "", "", "**","", "","","","***","","","","", "", "***", "***","***", "**","***","**","***","**","***","","", "", "***", "","", "","","","**","**","","",""), color=group), fontface="bold", size=4, angle= df2$angle+90, inherit.aes = F)+ #标签 #手动添加坐标及标题 geom_text(data=df2,x=11,y=30, label="Biodegradation rate(%)",color="black",size=3.5)+ geom_text(data=df2, x=-0.2,y=5,label="0",color="black",size=3)+ geom_text(data=df2, x=-0.2,y=25,label="20",color="black",size=3)+ geom_text(data=df2, x=-0.2,y=45,label="40",color="black",size=3)+ geom_text(data=df2, x=-0.2,y=65,label="60",color="black",size=3)+ geom_text(data=df2, x=-0.2,y=85,label="80",color="black",size=3)+ geom_text(data=df2, x=-0.2,y=105,label="100",color="black",size=3)+ #极坐标转换 coord_polar(direction=1)+ ##分组标签 geom_text(data=df3, aes(x = title, y = 140, label=group,color=group), hjust=c(1,1,0,0), angle=c(335,250,135,60), size=5, fontface="bold", inherit.aes = F)+ #颜色 scale_color_manual(values = c("#4fbb98","#f46024","#dd6ab0","#7c8ebe"))


完整代码(附带注释)及原始数据获取:

夸克网盘(手机端下载夸克APP进行搜索提取):

链接:https://pan.quark.cn/s/41d3ad0885dc

提取码:qDjx

更多推荐

R绘图模板——散点+回归线+误差线+显著性!!!

R绘图模板——散点+分组+size+显著性!!!

R可视化——基于MicrobiotaProcess包进行物种差异分析!

R绘图模板——气泡图+注释+配对连线!!!

R可视化——基于 ggdensity 包绘制等高线!

R绘图模板——世界地图的绘制及拓展!!!

基于MicrobiotaProcess包进行PCoA分析!

跟着Nature学绘图——柱状图+散点+配对连线+显著性!!!

R绘图模板——中国地图+散点+柱状图!!!

R绘图模板——热图和柱状堆积图组合图!!!

R绘图模板源代码获取!

扩增子测序数据分析还不会?小编整理的全套R语言代码助您轻松解决问题!

不会用代码做RDA分析?没关系,Canoco软件帮你实现!

R绘图模板——热图+网络图展示mantel test相关性!!!

R绘图模板——箱线图+点线图+显著性+分组!!!

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存