gpt4 book ai didi

r - 如何在ggplot2森林图中添加聚类水平线?

转载 作者:行者123 更新时间:2023-12-04 12:04:59 31 4
gpt4 key购买 nike

我非常接近完成摘要森林图。下面包含代码和绘图图像。最后一个很好的步骤是将某些行聚集在一起。例如,Years 1-4 的条形应该聚集在一起,并用一个空格将它们与“total”和“Male”分开。本质上,所有相同颜色的条应该聚集在一起并与其他条分开。我已经为此苦苦挣扎了几个小时。有任何想法吗?我编造了数据来创建这个示例图,包含在下面的代码中......

library(ggplot2)

#MADE UP DATA
test <- data.frame(
x = c("one", "two", "three","four", "five", "six","seven", "eight", "nine","ten", "eleven", "twelve","thirteen", "fourteen"),
y = c(4.0, 4.4, 7.1, 8.2, 2.9, 3.0, 4.0, 9.0, 11.0, 7.6, 4.4, 4.6, 4.9, 5.0 ),
yhi = c(6.0, 4.8, 7.6, 8.4, 3.3, 3.1, 4.8, 10.0, 16.0, 8.0, 4.5, 5.0, 6.9, 5.7),
ylo = c(2.0, 4.2, 6.6, 8.0, 2.5, 2.9, 3.2, 8.0, 6.0, 7.2, 4.3, 4.2, 2.9, 4.3),
labpos = c(17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17),
lab = c("total","Year1","Year2","Year3","Year4","Male","Female","Infant","Child","Adult","Urban","Rural","Occupational","Non-Occupational"),
grouping = factor(c(1,2,2,2,2,3,3,4,4,4,5,5,6,6)))


test$x <- reorder(test$x, c(14,13,12,11,10,9,8,7,6,5,4,3,2,1))



# MAKE INITIAL PLOT
a <- ggplot(test, aes(x=x, y=y, ymin=ylo, ymax=yhi, colour=grouping)) + geom_pointrange(shape=15, size=1.2, position=position_dodge(width=c(0.1))) + coord_flip() + geom_hline(aes(x=0), lty=2) + xlab('Variable') + scale_x_discrete(breaks=NULL) + scale_y_continuous(limits = c(-1,26)) + theme_bw()


# ADD AXIS LABELS3
a <- a + ylab("Blood lead level (ug/dL)") + xlab("") + ggtitle("Meta-Analysis Summary") + theme(legend.position="none")

# ADD LABELS FOR EACH POINT/CI
a + geom_text(data=test, aes(x = x, y = labpos, label = lab, hjust=0, fontface="italic"))

非常感谢。

The plot I generated...

最佳答案

根据 user20650 的建议解决了这个问题。
您需要添加缺少数据的行,以便组之间有空格。例如,使用我制作的数据集:

test3 <- data.frame(
x = c("one", "two", "three","four", "five", "six","seven", "eight", "nine","ten", "eleven", "twelve","thirteen", "fourteen", "fifteen"),
y = c(4.0, NA, 4.4, 7.1, 8.2, 2.9, 3.0, 4.0, 9.0, 11.0, 7.6, 4.4, 4.6, 4.9, 5.0 ),
yhi = c(6.0, NA, 4.8, 7.6, 8.4, 3.3, 3.1, 4.8, 10.0, 16.0, 8.0, 4.5, 5.0, 6.9, 5.7),
ylo = c(2.0, NA, 4.2, 6.6, 8.0, 2.5, 2.9, 3.2, 8.0, 6.0, 7.2, 4.3, 4.2, 2.9, 4.3),
labpos = c(17, NA, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17),
lab = c("total", "", "Year1","Year2","Year3","Year4","Male","Female","Infant","Child","Adult","Urban","Rural","Occupational","Non-Occupational"),
grouping = factor(c(1,1,2,2,2,2,3,3,4,4,4,5,5,6,6)))

test3$x <- reorder(test3$x, c(15,14,13,12,11,10,9,8,7,6,5,4,3,2,1))

关于r - 如何在ggplot2森林图中添加聚类水平线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23182942/

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