我正在使用forestplot,并希望在图中放置一系列垂直线. 我成功地添加了我想要的行,使用grid
参数.但是,我想为每行分配不同的 colored颜色 ,到目前为止,我只能让它们共享相同的 colored颜色 .
library(dplyr)
library(forestplot)
df <- data.frame(
model_name = rep(c('A','B','C','D'),3),
group = c(rep("Qrt 2",4),rep("Qrt 3",4),rep("Qrt 4",4)),
mean = c(10, 9.5, 10.5, 11, 5, 4.75, 5.25, 5.05, 2, 1.9, 1.8, 1.7),
lower = c(9, 8.5, 9.5, 10, 4, 4, 4.5, 4.25, 1.7, 1.6, 1.7, 1.65),
upper = c(11, 10.5, 10.75, 12, 6, 5, 5.5, 5.8, 2.3, 2.2, 1.9, 1.75)
)
df |>
group_by(group) |>
forestplot(
title = "Test Forest Plot",
labeltext = model_name,
boxsize = 0.2,
vertices = TRUE,
grid = structure(c(df[1,3], df[5,3], df[9,3]),
gp = gpar(lty = c(1,2,2), # line types other than the first one are ignored!
col = c("blue","green","red")))) |> # red and green are ignored!
fp_set_style(box = c("blue", "green", "red"), # no problems with these colors, tho
line = c("blue", "green", "red")) |>
fp_add_header(model_name = c("Model")) |>
fp_add_lines(h_2 = gpar(lwd = 1),
h_3 = gpar(lwd = 1),
h_4 = gpar(lwd = 1),
h_5 = gpar(lwd = 1),
h_6 = gpar(lwd = 1))
结果:
添加的3条垂直线都是蓝色的,但我希望它们是蓝色、绿色和红色,对应于相应的group
的 colored颜色 (即,蓝色、绿色、红色,从右到左).我不在乎灰零线,我暂时忽略它.
我try 了Structure()、c()和list()的各种组合,它们包含传递给grid
的参数,但到目前为止还没有成功.我还try 过fpColor:
col = fpColors(vrtcl_lines = c("blue","green","red"))
但是,同样缺乏结果.
单独控制线条类型也是很好的,我希望如果我能解决 colored颜色 问题,那么线条类型问题也可以类似地解决.
如果可能的话,有人能告诉我怎么做吗?谢谢你的考虑. (edit: made color order consistent in the text)个