我正在嘗試繪制具有不同組的森林圖。我使用的代碼如下所示:
d = data.frame(Estimate = c(1.8,1.9,2.1,2.4,2.7,2.5),
Group = rep(c('Group A', 'Group B'), each = 3),
Method = rep(c('Method 1', 'Method 2', 'Method 3'), 2))
d$Lower = d$Estimate - 0.3
d$Upper = d$Estimate 0.3
ggplot(data = d, aes(y = Group, x = Estimate, xmin = Lower, xmax = Upper, color = Method))
geom_point(size = 2, position=position_dodge(width = 0.5))
geom_linerange(position=position_dodge(width = 0.5))
geom_vline(xintercept = c(2, 2.5), linetype = "dashed")
以及由此產生的情節:

垂直線 (2, 2.5) 是真正的組均值。我想將這些垂直線限制在每個組內(即,從底部到中間的第一條,從中間到頂部的第二條)。有人知道怎么做嗎?
我試過geom_segment()函式,但我認為它需要一個數字 y 輸入,而它是這里的一個因素。
提前致謝!
uj5u.com熱心網友回復:
繪制在軸上的因子是“真正的”數字,但添加了標簽,因此您可以繼續添加數欄位:
ggplot(data = d, aes(y = Group, x = Estimate, xmin = Lower, xmax = Upper,
color = Method))
geom_point(size = 2, position=position_dodge(width = 0.5))
geom_linerange(position=position_dodge(width = 0.5))
geom_segment(data = data.frame(y = c(0.67, 1.67), x = c(2, 2.5),
xend = c(2, 2.5), yend = c(1.33, 2.33)),
aes(x, y, xend = xend, yend = yend),
inherit.aes = FALSE, linetype = 2)

或者,通過一些調整:
ggplot(data = d, aes(y = Group, x = Estimate, xmin = Lower, xmax = Upper,
color = Method))
geom_linerange(position=position_dodge(width = 0.5), size = 1)
geom_point(size = 3, position=position_dodge(width = 0.5), shape = 21,
fill = "white")
geom_segment(data = data.frame(y = c(0.67, 1.67), x = c(2, 2.5),
xend = c(2, 2.5), yend = c(1.33, 2.33)),
aes(x, y, xend = xend, yend = yend),
inherit.aes = FALSE, linetype = 2)
annotate("text", c(2, 2.5), c(1.5, 2.5), size = 6,
label = c("Group mean = 2", "Group mean = 2.5"))
theme_minimal(base_size = 20)
scale_color_brewer(palette = "Set1")

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/493117.html
上一篇:設定mixfitEM圖的軸限制
下一篇:具有多個變數的構面換行
