gpt4 book ai didi

r - 使用ggplot2微调森林图

转载 作者:行者123 更新时间:2023-12-04 03:27:46 26 4
gpt4 key购买 nike

我正在尝试在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处有一条虚线,但此线显示在图的最右边,不能正确。
  • 我想在每个栏的右侧添加其他文本列(以显示每个特定群组的其他信息)。
  • 总是欢迎进行任何使情节“更漂亮”的修改。

  • 提前致谢!

    最佳答案

    这似乎是您的想法:

    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)

    关于r - 使用ggplot2微调森林图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20766666/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com