【R>>boxplot】带有误差线和jitter的箱式图
2021-05-18 本文已影响0人
高大石头
箱式图在日常科研中必不可少,特别是带有errorbar和抖动点的,更是必备图。下面就记录下自己常用的boxplot常用画法:
rm(list = ls())
library(tidyverse)
library(ggsci)
ggplot(rt,aes(risk,futime/30,fill=risk))+
scale_fill_lancet(alpha = 0.7)+
geom_boxplot(outlier.size = -1,notch = T,
color="black",lwd=0.8,alpha=0.6)+
stat_boxplot(geom = "errorbar",width=0.05)+ #添加误差线
geom_point(shape=21,size=3,
position = position_jitterdodge(),
color="black",
alpha=1)+
theme_classic()+
theme(legend.position = "none",
legend.title = element_blank(),
axis.text = element_text(size = 11),
axis.title = element_text(size=13))+
labs(x="Risk",y="Survial time Month(s)")+
annotate(geom="text", cex=6,
x=1.5, y=100, # 根据自己的数据调节p value的位置
label=paste0("P ", ifelse(p<0.001, "< 0.001", paste0("= ",round(p,3)))), # 添加P值
color="black")
核心函数:
geom_boxplot(outlier.size = -1,notch = T,color="black",lwd=0.8,alpha=0.6)
-
stat_boxplot(geom = "errorbar",width=0.05)
→误差线 -
geom_point(shape=21,size=3, position = position_jitterdodge(),color="black",alpha=1)
→抖动散点