ggplot2: post-hoc + 森林图
生活随笔
收集整理的這篇文章主要介紹了
ggplot2: post-hoc + 森林图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本文將參照 Nature Communication 上的一篇論文 Rhizosphere bacteriome structure and functions 的結果圖與代碼(見下圖),并結果
在該圖中,左側條形圖反映了各個物種數據的平均值與標準誤。右側點圖代表在95% CI下不同事后比較下效應量大小的比較。
本次畫圖將結合心理學研究中的post-hoc與基于效應量的meta-analysis,用于自己的研究之中。
生成數據
library(tidyverse) # 在該反應時數據中,包含六個實驗的結果,并且每個實驗包含兩個條件(direct, indirect),因變量為反應時。 mean.data <- tibble(experiment = rep(paste0("exp",1:6),2), RT = runif(12,min = 500,max = 800),se = runif(12,min = 0, max = 120),type = c(rep("direct",6),rep("indirect",6))) mean.data$experiment <- factor(mean.data$experiment, levels = c("exp1","exp2","exp3","exp4","exp5","exp6")) # 在效應量數據中,也同樣有六個實驗的數據,并且效應量是基于兩個條件得到的統計分析得來的effect size值 ef <- tibble(experiment = c("exp1","exp2","exp3","exp4","exp5","exp6"), e = c(-.25,.1,.15,.65,.23,.31),type = rep(c("direct","indirect"),3)) # 模擬效應量置信區間數據 ci <- runif(10,min = 0, max = 0.5)# 以效應量是否大于0.3為標準,大于則為顯著,反之則為不顯著。 ef <- ef %>% mutate(sig = case_when(e > 0.3 ~ "sig",e < 0.3 ~ "n.s."))# 在原數據基礎上,生成置信區間數據 for (i in 1: nrow(ef)) {ef$conf.low[i] <- ef$e[i] - ci[i]ef$conf.high[i] <- ef$e[i] + ci[i] } head(ef) head(mean.data)mean.data 的數據格式
ef 的數據格式
畫圖
# 顏色編號cbbPalette <- c("#FFCC00", "#56B4E9")# bar plot p1 <- ggplot(mean.data,aes(experiment,RT,fill = type)) +xlim(ef$experiment) +coord_flip() +xlab("") +ylab("") +theme(panel.background = element_rect(fill = 'transparent'),panel.grid = element_blank(),axis.ticks.length = unit(0.4,"lines"), axis.ticks = element_line(color='black'),axis.line = element_line(colour = "black"),axis.title.x=element_text(colour='black', size=12,face = "bold"),axis.text=element_text(colour='black',size=10,face = "bold"),legend.title=element_blank(),legend.text=element_text(size=12,face = "bold",colour = "black",margin = margin(r = 20)),legend.position = c(-1,-0.1),legend.direction = "horizontal",legend.key.width = unit(0.8,"cm"),legend.key.height = unit(0.5,"cm")) for (i in 1:(nrow(ef) - 1)) p1 <- p1 + annotate('rect', xmin = i+0.5, xmax = i+1.5, ymin = -Inf, ymax = Inf, fill = ifelse(i %% 2 == 0, 'white', 'gray95')) p1 <- p1 + geom_errorbar(aes(ymin=RT-se,ymax=RT+se),width=0.6,position=position_dodge(0.8))+geom_col(position=position_dodge(0.8),width = 0.8, colour="black") +scale_fill_manual(values=cbbPalette) p1 p5<- p1+ coord_flip(ylim=c(400,800)) # scatter plot p2 <- ggplot(ef,aes(experiment,e,fill = sig)) +theme(panel.background = element_rect(fill = 'transparent'),panel.grid = element_blank(),axis.ticks.length = unit(0.4,"lines"), axis.ticks = element_line(color='black'),axis.line = element_line(colour = "black"),axis.title.x=element_text(colour='black', size=12,face = "bold"),axis.text=element_text(colour='black',size=10,face = "bold"),axis.text.y = element_blank(),legend.position = "none",axis.line.y = element_blank(),axis.ticks.y = element_blank(),plot.title = element_text(size = 15,face = "bold",colour = "black",hjust = 0.5)) +scale_x_discrete(limits = levels(ef$experiment)) +coord_flip(ylim=c()) +xlab("") +ylab("") +labs(title="") for (i in 1:(nrow(ef) - 1)) p2 <- p2 + annotate('rect', xmin = i+0.5, xmax = i+1.5, ymin = -Inf, ymax = Inf, fill = ifelse(i %% 2 == 0, 'white', 'gray95'))p2 <- p2 +geom_errorbar(aes(ymin = conf.low, ymax = conf.high), position = position_dodge(0.8), width = 0.5, size = 0.5) +geom_point(shape = 21,size = 4.5) +scale_fill_manual(values= c("ghostwhite","brown1")) +geom_hline(aes(yintercept = 0), linetype = 'dashed', color = 'black')p2 ## Image Stitching library(patchwork) p <- p5 + p2 + plot_layout(widths = c(2,2)) p
這樣一幅簡單的圖就大功告成了!
總結
以上是生活随笔為你收集整理的ggplot2: post-hoc + 森林图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html网页静态时钟代码,网页时钟实现代
- 下一篇: C语言之杨辉三角