【问题标题】:Forest plot in R: Trying to plot more than three pointsR中的森林图:试图绘制三个以上的点
【发布时间】:2016-09-06 21:06:09
【问题描述】:

这是我第一次在这里发布问题,如果我的问题不清楚或不完整,请原谅我。

我的场景:我有一个包含 21 个元分析分布 (Distribution1-Distribution21) 的数据框。对于每个分布,我对各自的元分析平均效应大小 (ES1-ES10) 有 10 个估计值。实际上,我通过各种敏感性分析(即异常值和发表偏倚分析)获得了元分析平均效应大小和其他九个该平均值的估计值。

使用修改后的代码(如果需要,可以提供链接;我无法发布多个链接,因为我是新用户),我能够绘制每个分布的平均估计值的三个估计值。为了让您了解我在说什么,想象一个显示平均估计值及其置信区间的数字。

这是数据框和修改后的代码:

x   |   ES1 |   ES2 |   ES3 |   ES4 |   ES5 |   ES6 |   ES7 |   ES8 |   ES9 |   ES10
Distribution1   |   -0.07   |   -0.07   |   -0.06   |   -0.07   |   -0.02   |   -0.03   |   -0.09   |   -0.07   |   0.00    |   0.01
Distribution2   |   -0.06   |   -0.06   |   -0.04   |   -0.05   |   -0.04   |   -0.05   |   -0.07   |   -0.06   |   -0.03   |   0.01
Distribution3   |   -0.08   |   -0.09   |   -0.07   |   -0.08   |   -0.01   |   -0.08   |   -0.10   |   -0.08   |   -0.01   |   0.01
Distribution4   |   -0.10   |   -0.11   |   -0.10   |   -0.09   |   -0.05   |   -0.07   |   -0.11   |   -0.10   |   -0.06   |   0.010
Distribution5   |   -0.08   |   -0.08   |   -0.06   |   -0.08   |   -0.02   |   -0.03   |   -0.10   |   -0.08   |   0.00    |   0.02
Distribution6   |   -0.09   |   -0.10   |   -0.08   |   -0.09   |   -0.03   |   -0.08   |   -0.11   |   -0.09   |   -0.03   |   0.02
Distribution7   |   -0.11   |   -0.13   |   -0.10   |   -0.11   |   -0.04   |   -0.04   |   -0.12   |   -0.11   |   -0.08   |   0.01
Distribution8   |   -0.10   |   -0.14   |   -0.06   |   -0.10   |   -0.01   |   -0.08   |   -0.13   |   -0.10   |   -0.06   |   0.04
Distribution9   |   -0.04   |   -0.05   |   -0.02   |   -0.04   |   0.00    |   -0.04   |   -0.06   |   -0.04   |   -0.06   |   0.00
Distribution10  |   -0.11   |   -0.12   |   -0.09   |   -0.11   |   -0.03   |   -0.09   |   -0.12   |   -0.11   |   -0.11   |   0.00
Distribution11  |   -0.06   |   -0.09   |   -0.04   |   -0.06   |   -0.01   |   -0.01   |   -0.09   |   -0.06   |   -0.01   |   0.04
Distribution12  |   -0.11   |   -0.11   |   -0.09   |   -0.11   |   -0.09   |   -0.10   |   -0.12   |   -0.11   |   -0.08   |   -0.03
Distribution13  |   -0.19   |   -0.22   |   -0.16   |   -0.19   |   -0.08   |   -0.17   |   -0.21   |   -0.19   |   -0.13   |   -0.01
Distribution14  |   -0.01   |   -0.02   |   0.00    |   -0.01   |   0.00    |   0.00    |   -0.03   |   -0.01   |   -0.02   |   -0.01
Distribution15  |   -0.19   |   -0.22   |   -0.16   |   -0.19   |   -0.08   |   -0.17   |   -0.21   |   -0.19   |   -0.13   |   -0.01
Distribution16  |   -0.09   |   -0.1    |   -0.08   |   -0.09   |   -0.01   |   -0.09   |   -0.11   |   -0.09   |   -0.07   |   0.00
Distribution17  |   -0.16   |   -0.19   |   -0.14   |   -0.16   |   -0.07   |   -0.12   |   -0.18   |   -0.16   |   -0.10   |   0.00
Distribution18  |   -0.05   |   -0.06   |   -0.03   |   -0.05   |   -0.02   |   -0.02   |   -0.05   |   -0.05   |   -0.02   |   0.01
Distribution19  |   -0.09   |   -0.10   |   -0.08   |   -0.09   |   -0.01   |   -0.08   |   -0.11   |   -0.09   |   -0.06   |   0.01
Distribution20  |   -0.02   |   -0.03   |   -0.01   |   -0.02   |   0.01    |   0.00    |   -0.04   |   -0.02   |   0.00    |   0.02
Distribution21  |   -0.1    |   -0.12   |   -0.09   |   -0.1    |   -0.02   |   -0.08   |   -0.12   |   -0.1    |   -0.04   |   0.02

#My APA-format theme
#https://gist.github.com/akshaycuhk/01576c57149a9a3d14514c9a3c4b4b1d

install.packages("ggplot2")
library(ggplot2)

apatheme=theme_bw()+ 
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.border=element_blank(),
axis.line=element_line(),
text=element_text(family='Times'),
legend.position='bottom', axis.text=element_text(size=14),
axis.title=element_text(size=14,face="bold"))

credplot.gg <- function(d){
# d is a data frame with 4 columns
# d$x gives variable names
# d$y gives center point
# d$ylo gives lower limits
# d$yhi gives upper limits
require(ggplot2)
p <- ggplot(d, aes(x=x, y=ES1, ymin=ES2, ymax=ES3))+
geom_pointrange()+
geom_hline(yintercept = 0, linetype=2)+
coord_flip()+
xlab('Distribution')+
ylab('Effect size')
return(p)
}

# load your data below 
d <- read.table(file.choose(), sep=",", header=TRUE)
Fig1 <-credplot.gg(d) +xlim("Distribution1", 
"Distribution2", 
"Distribution3", 
"Distribution4", 
"Distribution5",
"Distribution6", 
"Distribution7", 
"Distribution8", 
"Distribution9", 
"Distribution10", 
"Distribution11", 
"Distribution12", 
"Distribution13",
"Distribution14", 
"Distribution15", 
"Distribution16", 
"Distribution17", 
"Distribution18", 
"Distribution19",
"Distribution20",
"Distribution21")
Fig1

我还不能嵌入图像,所以这里是数据文件、脚本和图形的链接: https://www.dropbox.com/sh/aczv1dw5mjmone8/AACqekiFVdJqeA1cRvIvs7NFa?dl=0

我的问题:我有没有办法将点估计数从 3 个增加到 10 个?具体来说,我可以为所有 21 个分布(Distribution1 -> Distribution21)绘制所有十个估计值(ES1 -> ES10)吗?理想情况下,每个点估计在线上都有自己的形状/标记,以将其与其他点区分开来,并且图例会伴随该图。

感谢任何愿意帮助我的人:)

【问题讨论】:

  • 我不完全确定你希望你的输出是什么样的,但听起来这可能是一个宽格式数据集的问题,如果它是在一个更容易绘制的长格式。请参阅 thisthis 等示例。

标签: r plot ggplot2


【解决方案1】:

这就是你想要的吗?它涉及将您的数据集重塑为长格式,为每个“E”类别添加具有不同形状的点,然后通过每个“分布”的点绘制线条以模拟森林图。

library(reshape2)
dat2 = melt(d, id.vars = "x")

# Set x factor order in order that appears in data
dat2$x = factor(dat2$x, levels = unique(dat2$x))

ggplot(dat2, aes(x=x, y= value))+
    geom_point(aes(shape = variable)) +
    geom_line() +
    scale_shape_manual(values = 0:9) +
    geom_hline(yintercept = 0, linetype=2) +
    coord_flip() +
    xlab('Distribution') +
    ylab('Effect size')

请注意,当使用这么多形状时,事情会变得很丑。请参阅here 了解一些形状选项。

【讨论】:

  • 嗨@aosmith:有没有办法让 ES1->ES5 和 ES6->ES10 共享相同的形状但具有不同的颜色。具体来说,ES1 和 ES6 是否可以具有相同的形状,但一个是蓝色的,另一个是红色的?那么,ES2 和 ES7、ES3 和 ES8 等是否可以遵循这种模式?
  • @JamesF 您可以使用适当的scale_*_manual 函数为 ggplot2 中的组设置值。在你的情况下,听起来你会为你在scale_shape_manual 中通过values 列出的对使用相同的形状;有关一些简单示例,请参见帮助页面。你可以对颜色做同样的事情。如果你想要一个图例,代码会稍微复杂一些,但是有很多关于如何做到这一点的堆栈溢出示例。
猜你喜欢
  • 2017-01-12
  • 1970-01-01
  • 2016-11-10
  • 1970-01-01
  • 2013-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多