其他
R绘图模板——散点+箱线图+小提琴图+辅助线+显著性!
点击上方
“科研后花园”
关注我们
图片灵感来源:
效果图:
1、设置工作环境并加载所需R包:
rm(list=ls())#clear Global Environment
setwd("D:/桌面/散点+箱线图+小提琴图+辅助线+显著性")
#加载包
library(ggplot2) # Create Elegant Data Visualisations Using the Grammar of Graphics
library(ggsignif) # Significance Brackets for 'ggplot2'
library(gghalves) # Compose Half-Half Plots Using Your Favourite Geoms
2、加载绘图数据(根据原文图片随机编写数据,无实际意义):
#加载数据
df <- read.table("data.txt",header = 1)
df$group <- factor(df$group,levels = c("A","B","C","D"))
3、绘图:
##绘图
ggplot(df,aes(group,value,fill=group))+
geom_half_violin(position = position_nudge(x=0.25),side = "r",width=0.8,color=NA)+
geom_boxplot(width=0.4,size=1.2,outlier.color =NA)+
geom_jitter(aes(fill=group),shape=21,size=2.5,width=0.2)+
geom_hline(yintercept = 0, linetype = 2, color = "red",linewidth=1)+
geom_hline(yintercept = 80, linetype = 2, color = "red",linewidth=1)+
geom_signif(comparisons = list(c("A","B"),
c("A","C"),
c("C","D")),
map_signif_level = T,
test = t.test,
y_position = c(100,120,130),
tip_length = c(0,0,0,0,0,0),
size=1,color="black",textsize = 7)+
scale_y_continuous(limits = c(-20,140),breaks = c(0,40,80,120))+
theme_bw()+
theme(panel.grid = element_blank(),
panel.border = element_rect(size = 1),
axis.text.x = element_text(color = "black", size = 13),
axis.text.y = element_text(color = "black",size = 13),
legend.position = "none",
axis.ticks = element_line(color="black",linewidth = 1))+
labs(x=NULL,y=NULL)+
scale_fill_manual(values = c("#5cc3e8","#ffdb00","#79ceb8","#e95f5c"))
PS: 以上内容是小编个人学习代码笔记分享,仅供参考学习,欢迎大家一起交流学习。
代码及数据获取:
在淘宝APP中搜索【科研后花园】,进入店铺后搜索推文标题即可!
或者直接复制链接进入淘宝获取:https://item.taobao.com/item.htm?ft=t&id=729678306430
(链接失效记得后台联系小编哦)
温馨提示
如果你喜欢本文,请分享到朋友圈,想要获得更多信息,请关注我。