跟着Nature学作图:ggplot2包绘制森林图/误差棒图

 数据分析服务请访问以下链接:

文章发表技术服务,数据分析服务

数据和代码获取:请查看主页个人信息

大家好,今天我将介绍如何使用R语言ggplot2包绘制个性化误差棒图和森林图。

1 误差棒图(Error Bar Plot):用于表示测量数据的中心趋势和误差范围。它通常包括一个表示中心趋势(如均值或中位数)的点,以及表示误差范围的线段或区域。误差范围可以表示标准差、标准误差、置信区间等。

2 森林图(Forest Plot):用于可视化多个研究结果或效应估计及其置信区间。它通常以垂直线段表示每个效应估计的置信区间,并根据效应估计的大小进行排序。森林图常用于荟萃分析和医学研究中。

图片

图片样式灵感来源于Nature杂志的一篇文章:

图片

接下来我们用文章的实例数据来进行可视化展示:

Step1:数据载入

rm(list=ls())
library(tidyverse)
library(cowplot)

load('dnds.comp1.rda')
load('dnds.comp2.rda')
load('ALL_plot.rda')
load('shading.rda')

head(All_plot)

图片

Step2:定义绘图元素


type1 <- "seeding"
type2 <- "primary_nonMetastatic"

col.palette  <- setNames(c("#762a83", "#5aae61"), c(type1, type2))
fill.palette <- setNames(adjustcolor(c("#762a83", "#c2a5cf", "#5aae61"), alpha.f = 0.75), c(paste0(type1, " favored"), paste0(type2, " and ", type1), paste0(type2, " favored")))
cols <- paste( "point", c( 'w', 'w_low', 'w_high'), sep = '_' )

Step3:绘制误差棒图

ggplot(All_plot) +
  geom_point(aes(x = gene_name, y = get(cols[1]), colour = type), position = position_dodge(0.5), size = 2) +
  geom_errorbar(aes(x = gene_name, y = get(cols[1]), ymin = get(cols[2]), 
                    ymax = get(cols[3]), colour = type), position = position_dodge(0.5), size = 0.75, width = 0.5) +
  scale_y_continuous(trans = 'log10', limits = c(0.1, 1300),
                     breaks = 10^(-1:3), labels = c('<0.1',10^(0:2), '>1000')) +
  geom_hline(yintercept = 1) +
  labs(x = "",
       y = paste0("dNdS point mutations")) +
  theme_cowplot(font_size = 16) +
  theme(legend.title = element_blank(), 
        legend.position = "none") +
  coord_flip() +
  ggsci::scale_color_aaas() +
  ggsci::scale_fill_aaas()

图片

Step4:绘制森林图


ggplot(All_plot) +
  geom_pointrange(aes(x = gene_name, y = get(cols[1]), ymin = get(cols[2]), 
                      ymax = get(cols[3]), colour = type), position = position_dodge(0.5)) +
  geom_rect(data = shading, aes(xmin=x1, xmax=x2, ymin=y1, ymax=y2, fill=cat), alpha = 0.3) +
  geom_pointrange(aes(x = gene_name, y = get(cols[1]), ymin = get(cols[2]), 
                      ymax = get(cols[3]), colour = type), position = position_dodge(0.5), size = 0.75) +
  scale_color_manual(values = col.palette) +
  scale_fill_manual(values = fill.palette) +
  scale_y_continuous(trans = 'log10', limits = c(0.1, 1300),
                     breaks = 10^(-1:3), labels = c('<0.1',10^(0:2), '>1000')) +
  geom_hline(yintercept = 1) +
  labs(x = "",
       y = paste0("dNdS point mutations")) +
  theme_cowplot(font_size = 16) +
  theme(axis.text.x = element_text( angle = 45, hjust = 1),legend.title = element_blank())

图片

划重点:上述代码【geom_pointrange】函数用来绘制森林图;【geom_errorbar】函数用于绘制误差棒图。【geom_rect】函数增加图层,用来映射三种不同的绘图背景,这也是一个不错的绘图技巧~

Step5:个性化:改变横坐标轴字体颜色

# 定义横坐标轴颜色
color = ifelse(as.character(All_plot %>%
                              arrange(gene_name) %>%
                              pull(gene_name) %>%
                              unique()) %in% as.character(dnds.comp1 %>%
                                                            mutate(qvec = p.adjust(pvec, "fdr")) %>% 
                                                            filter(qvec < 0.05) %>% 
                                                            pull(genestotest)), 
               col.palette[type1], ifelse(as.character(All_plot %>% 
                                                         arrange(gene_name) %>% 
                                                         pull(gene_name) %>% 
                                                         unique()) %in% as.character(dnds.comp2 %>% 
                                                                                       mutate(qvec = p.adjust(pvec,"fdr")) %>% 
                                                                                       filter(qvec < 0.05) %>% 
                                                                                       pull(genestotest)), col.palette[type2], "black"))
ggplot(All_plot) +
  geom_pointrange(aes(x = gene_name, y = get(cols[1]), ymin = get(cols[2]), 
                      ymax = get(cols[3]), colour = type), position = position_dodge(0.5)) +
  geom_rect(data = shading, aes(xmin=x1, xmax=x2, ymin=y1, ymax=y2, fill=cat), alpha = 0.3) +
  geom_pointrange(aes(x = gene_name, y = get(cols[1]), ymin = get(cols[2]), 
                      ymax = get(cols[3]), colour = type), position = position_dodge(0.5), size = 0.75) +
  scale_color_manual(values = col.palette) +
  scale_fill_manual(values = fill.palette) +
  scale_y_continuous(trans = 'log10', limits = c(0.1, 1300),
                     breaks = 10^(-1:3), labels = c('<0.1',10^(0:2), '>1000')) +
  geom_hline(yintercept = 1) +
  labs(x = "",
       y = paste0("dNdS point mutations")) +
  theme_cowplot(font_size = 16) +
  theme(axis.text.x = element_text( angle = 45, hjust = 1,color = color), legend.title = element_blank(), legend.position = "none")

图片

关键词:“森林图”获得本期代码和数据

 “森林图”获得本期代码和数据

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值