前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[NC图表复现] ggplot2绘制分裂小提琴图

[NC图表复现] ggplot2绘制分裂小提琴图

作者头像
R语言数据分析指南
发布2024-01-17 14:06:27
3360
发布2024-01-17 14:06:27
举报
文章被收录于专栏:R语言数据分析指南

加载R包

代码语言:javascript
复制
library(tidyverse)
library(introdataviz)
library(ggpubr)
library(scales)
library(patchwork)

导入数据

代码语言:javascript
复制
df1 <- read_tsv("F1ag.txt") %>% select(2,3,-1) %>% 
  separate(`Treatment`,into=c("A","B"),sep="-",convert = T) %>% 
  mutate(B = str_replace_all(B, "[0-9]", ""))

df1$A <- factor(df1$A,levels = c("Conven","Conserv"))

数据可视化

代码语言:javascript
复制
a <- ggplot(df1,aes(x = A, y =`SOC (g kg-1)` ,fill= B))+
  geom_split_violin(trim =F,color = NA,adjust = 1.5)+
  guides(fill=guide_legend(title="group"))+
  stat_summary(fun.data = "mean_sd",position=position_dodge(0.15),geom = "errorbar",width = .1) +
  stat_summary(fun = "mean", geom = "point", position=position_dodge(0.15),show.legend = F)+
  stat_compare_means(aes(group = B), label = "p.signif",label.y=15.1, method="t.test",size=5)+
  scale_fill_manual(values=c("#788FCE","#E6956F"))+
  labs(x=NULL,y="SOC (g kg-1)")+
  theme(
        axis.text.x=element_text(angle =0,hjust =0.5,vjust =0.5,color="black",size = 10,margin = margin(b =2)),
        axis.text.y=element_text(color="black",size = 10,margin = margin(r =1)),
        panel.background = element_rect(fill = NA,color = NA),
        panel.grid.minor= element_line(size=0.2,color="#e5e5e5"),
        panel.grid.major = element_line(size=0.2,color="#e5e5e5"),
        panel.border = element_rect(fill=NA,color="black",size=1,linetype="solid"),
        legend.key=element_blank(),
        legend.title = element_blank(),
        legend.text = element_text(color="black",size=8),
        legend.spacing.x=unit(0.1,'cm'),
        legend.key.width=unit(0.5,'cm'),
        legend.key.height=unit(0.5,'cm'),
        legend.position = c(1,0), legend.justification = c(1,0),
        legend.background=element_blank())
代码语言:javascript
复制
df2 <- read_tsv("F1ag.txt") %>% select(2,4,-1) %>% 
  separate(`Treatment`,into=c("A","B"),sep="-",convert = T) %>% 
  mutate(B = str_replace_all(B, "[0-9]", ""))

df2$A <- factor(df2$A,levels = c("Conven","Conserv"))
代码语言:javascript
复制
g <- ggplot(df2,aes(x = A, y =`DOC(mg kg-1)` ,fill= B))+
  geom_split_violin(trim =F,color = NA,adjust = 1.5)+
  guides(fill=guide_legend(title="group"))+
  stat_summary(fun.data = "mean_sd",position=position_dodge(0.15),geom = "errorbar",width = .1) +
  stat_summary(fun = "mean", geom = "point", position=position_dodge(0.15),show.legend = F)+
  stat_compare_means(aes(group = B), label = "p.signif",label.y=210, method="t.test",size=5)+
  scale_fill_manual(values=c("#788FCE","#E6956F"))+
  labs(x=NULL,y="DOC(mg kg-1)")+
  theme(
        axis.text.x=element_text(angle =0,hjust =0.5,vjust =0.5,color="black",size = 10,margin = margin(b =2)),
        axis.text.y=element_text(color="black",size = 10,margin = margin(r =1)),
        panel.background = element_rect(fill = NA,color = NA),
        panel.grid.minor= element_line(size=0.2,color="#e5e5e5"),
        panel.grid.major = element_line(size=0.2,color="#e5e5e5"),
        panel.border = element_rect(fill=NA,color="black",size=1,linetype="solid"),
        legend.position ="non")

df3 <- read_tsv("F1BC.txt") %>% 
  select(2,3,-1) %>% 
  separate(`Treatment`,into=c("A","B"),sep="-",convert = T) %>% 
  mutate(B = str_replace_all(B, "[0-9]", ""))

df3$A <- factor(df3$A,levels = c("Conven","Conserv"))

b <- ggplot(df3,aes(x = A, y =`Soil temperate (℃)`,fill= B))+
  geom_split_violin(trim =F,color = NA,adjust = 1.5)+
  guides(fill=guide_legend(title="group"))+
  stat_summary(fun.data = "mean_sd",position=position_dodge(0.15),geom = "errorbar",width = .1) +
  stat_summary(fun = "mean", geom = "point", position=position_dodge(0.15),show.legend = F)+
  stat_compare_means(aes(group = B), label = "p.signif",label.y=17.1, method="t.test",size=5)+
  scale_fill_manual(values=c("#788FCE","#E6956F")) +
  labs(x=NULL,y="Soil temperate")+
  theme(
        axis.text.x=element_text(angle =0,hjust =0.5,vjust =0.5,color="black",size = 10,margin = margin(b =2)),
        axis.text.y=element_text(color="black",size = 10,margin = margin(r =1)),
        panel.background = element_rect(fill = NA,color = NA),
        panel.grid.minor= element_line(size=0.2,color="#e5e5e5"),
        panel.grid.major = element_line(size=0.2,color="#e5e5e5"),
        panel.border = element_rect(fill=NA,color="black",size=1,linetype="solid"),
        legend.position ="non")

拼图

代码语言:javascript
复制
a+b+g+ plot_annotation(tag_levels = 'A')
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-01-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 R语言数据分析指南 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 加载R包
  • 导入数据
  • 数据可视化
  • 拼图
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档