【问题标题】:Finetuning a forest plot with ggplot2使用 ggplot2 微调森林图
【发布时间】:2013-12-24 20:58:02
【问题描述】:

我正在尝试在 R 中制作森林图,显示元分析的结果。但是,我在使用 ggplot2 时遇到了问题。到目前为止,我还没有在 stackoverflow 上找到类似的问题,非常感谢一些帮助。

我现在使用的代码如下所示(我对其进行了一些更改以使其自包含):

cohort <- letters[1:15]
population <- c(  runif(15, min=2000, max=50000)) #hit1$N
beta <-  c(  runif(15, min=-1, max=2))
lower95 <- c(runif(15, min=-1.5, max=0.5))
upper95 <- c(runif(15, min=1.5, max=2.5))
type <- c("CBCL","SDQ","CBCL","SDQ","CBCL","SDQ","CBCL")
data <- as.data.frame(cbind(cohort, population, beta ,lower95,upper95,type))


ggplot(data=data, aes(x=cohort, y=beta))+
  geom_errorbar(aes(ymin=lower95, ymax=upper95), width=.667) +
  geom_point(aes(size=population, fill=type), colour="black",shape=21)+
  geom_hline(yintercept=0, linetype="dashed")+
  scale_x_discrete(name="Cohort")+
  coord_flip()+
  scale_shape(solid=FALSE)+
  scale_fill_manual(values=c( "CBCL"="white", "SDQ"="black"))+
  labs(title="Forest Plot") +
  theme_bw()

现在,我有以下问题:

  • x 轴不可读,因为所有值都重叠。
  • 右侧的图例(“人口”)显示所有值,但我只希望它显示一些任意值,例如 5000、10000 和 15000。
  • 绘图应该在 y=0 处有一条虚线,但这条线显示在绘图的最右侧,不可能是正确的。
  • 我想在每个栏的右侧添加其他文本列(以显示每个特定同类群组的其他信息)。
  • 欢迎任何使情节“更漂亮”的修改。

提前致谢!

【问题讨论】:

  • 您的前三个问题是因为您的所有变量都是cbindas.data.frame 组合的因素。如果您将type 设为与其他向量长度相同的向量,则只需执行data &lt;- data.frame(cohort, population, beta ,lower95,upper95,type)

标签: r plot ggplot2


【解决方案1】:

这似乎是您的想法:

data$beta <- as.numeric(as.character(data$beta))
data$lower95 <- as.numeric(as.character(data$lower95))
data$upper95 <- as.numeric(as.character(data$upper95))
data$population <- as.numeric(as.character(data$population))

ggplot(data=data,aes(x=beta,y=cohort))+
  geom_point(aes(size=population,fill=type), colour="black",shape=21)+
  geom_errorbarh(aes(xmin=lower95,xmax=upper95),height=0.667)+
  geom_vline(xintercept=0,linetype="dashed")+
  scale_size_continuous(breaks=c(5000,10000,15000))+
  geom_text(aes(x=2.8,label=type),size=4)

您必须使用 geom_text(...) 的参数来获得所需的标签位置,并获得所需的大小。

至于使情节更漂亮,我更喜欢这个:

ggplot(data=data,aes(x=beta,y=cohort))+
  geom_point(aes(size=population,color=type),shape=16)+
  geom_errorbarh(aes(xmin=lower95,xmax=upper95),height=0.0, colour="blue")+
  geom_vline(xintercept=0,linetype="dashed")+
  scale_size_continuous(breaks=c(5000,10000,15000))+
  geom_text(aes(x=2.8,label=type),size=4)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-11
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多