1、加载R包:
library(ggplot2) # Create Elegant Data Visualisations Using the Grammar of Graphics
library(reshape2) # Flexibly Reshape Data: A Reboot of the Reshape Package
library(tidyr) # Tidy Messy Data
library(dplyr) # A Grammar of Data Manipulation
library(ggsignif) # Significance Brackets for 'ggplot2'
2、加载绘图数据并进行数据处理:
#数据——ggplot自带的ToothGrowth数据
df <- ToothGrowth
df$dose <- as.factor(df$dose)
data <- df
#计算均值及标准差
df1 <- data%>% group_by(dose)%>%
summarise(mean= mean(len), sd= sd(len))
3、绘图:
ggplot()+
geom_bar(df1,mapping=aes(x=dose,y=mean), fill = "white",
size = 1.5,color = c("#d20962","#f47721","#7ac143"),position="dodge",
stat="identity",width = 0.6)+
geom_errorbar(df1,mapping=aes(x = dose,ymin = mean-sd, ymax = mean+sd),
width = 0.3,color = c("#d20962","#f47721","#7ac143"), size=1.5)+
geom_jitter(df, mapping=aes(x=dose,y=len,fill = dose,color = dose,shape = dose),
size = 2.5,width = 0.2,alpha=0.9)+
geom_line(df1,mapping=aes(x=dose,y=mean,group=1),
size=1,color="#00aee6")+
geom_point(df1,mapping=aes(x=dose,y=mean),color="black",size=3,shape=8)+
scale_color_manual(values = c("#d20962","#f47721","#7ac143"))+
geom_signif(df,mapping=aes(x=dose,y=len),
comparisons = list(c("0.5", "1"),
c("1","2"),
c("0.5","2")),
map_signif_level=T,
tip_length=c(0,0,0,0,0,0),
y_position = c(35,40,45),
size=1, textsize = 7,
test = "t.test")+
scale_y_continuous(expand = c(0, 0), limit = c(0, 50))+
theme_classic(base_line_size = 1)+
theme(panel.grid=element_blank(),
axis.text=element_text(color='black',size=13,face = "bold"),
axis.title.y = element_text(color='black',size=15,face = "bold"),
legend.text = element_text(color='black',size=13,face = "bold"),
legend.title = element_blank(),
legend.position = "none")+
labs(x=NULL,y="Value")
特别申明:本文为转载文章,转载自 科研后花园,不代表贪吃的夜猫子立场,如若转载,请注明出处:https://mp.weixin.qq.com/s/OGhdZNJrvA1mK4pHE7JGaA