R中的森林情节:试图绘制超过三个点

时间:2016-09-06 21:06:09

标签: r plot ggplot2

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

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

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

这是数据框和改编的代码:

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个分布(分布1 - >分布21)绘制所有10个估计值(ES1 - > ES10)吗?理想情况下,每个点估计在线上都有自己的形状/标记,以区别于其他点,并且附图中会附有图例。

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

1 个答案:

答案 0 :(得分:0)

这是你想要的吗?它涉及将数据集重新整形为长格式,每个&#34; E&#34;类别,然后绘制线条通过每个&#34;分布&#34;模仿森林情节。

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')

enter image description here 请注意,使用这么多形状时,事情变得很难看。有关形状选项,请参阅here

相关问题