ggplot2: post-hoc + 森林图
生活随笔
收集整理的這篇文章主要介紹了
ggplot2: post-hoc + 森林图
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
本文將參照 Nature Communication 上的一篇論文 Rhizosphere bacteriome structure and functions 的結(jié)果圖與代碼(見下圖),并結(jié)果
在該圖中,左側(cè)條形圖反映了各個(gè)物種數(shù)據(jù)的平均值與標(biāo)準(zhǔn)誤。右側(cè)點(diǎn)圖代表在95% CI下不同事后比較下效應(yīng)量大小的比較。
本次畫圖將結(jié)合心理學(xué)研究中的post-hoc與基于效應(yīng)量的meta-analysis,用于自己的研究之中。
生成數(shù)據(jù)
library(tidyverse) # 在該反應(yīng)時(shí)數(shù)據(jù)中,包含六個(gè)實(shí)驗(yàn)的結(jié)果,并且每個(gè)實(shí)驗(yàn)包含兩個(gè)條件(direct, indirect),因變量為反應(yīng)時(shí)。 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")) # 在效應(yīng)量數(shù)據(jù)中,也同樣有六個(gè)實(shí)驗(yàn)的數(shù)據(jù),并且效應(yīng)量是基于兩個(gè)條件得到的統(tǒng)計(jì)分析得來(lái)的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)) # 模擬效應(yīng)量置信區(qū)間數(shù)據(jù) ci <- runif(10,min = 0, max = 0.5)# 以效應(yīng)量是否大于0.3為標(biāo)準(zhǔn),大于則為顯著,反之則為不顯著。 ef <- ef %>% mutate(sig = case_when(e > 0.3 ~ "sig",e < 0.3 ~ "n.s."))# 在原數(shù)據(jù)基礎(chǔ)上,生成置信區(qū)間數(shù)據(jù) 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 的數(shù)據(jù)格式
ef 的數(shù)據(jù)格式
畫圖
# 顏色編號(hào)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
這樣一幅簡(jiǎn)單的圖就大功告成了!
總結(jié)
以上是生活随笔為你收集整理的ggplot2: post-hoc + 森林图的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: html网页静态时钟代码,网页时钟实现代
- 下一篇: C语言之杨辉三角