1

我想创建一个图,其中包含所有层的调整值(每个层的顶线)和匹配值(每个层中由 X 表示的线),例如男性、女性、年龄 <55、年龄 >55 等。对于匹配值, 我想要一个钻石/其他形状或不同的颜色来突出显示。但不知道怎么办?

我意识到我可以使用这个示例http://gforge.se/2013/12/the-forestplot-of-dreams/ 制作两个单独的数据集,一个用于调整值,一个用于匹配值,然后组合 - 但宁愿不必再次输入所有值。

有人可以帮助编辑我的代码的第三部分,以便为我的图表中的匹配值制作差异形状或差异颜色(匹配的 val 是 N 列中由 X 表示的线)。

 library(forestplot)
main_acevccb <- 
  structure(list(
    mean  = c(NA, NA, NA, -1.12, -0.64, -1.55,-1.60, NA, -1.35,-1.44, -1.3, -1.2, NA, -1.29,-1.23, -2.82,-2.15, -1.84,-2.72), 
    lower = c(NA, NA, NA, -1.41, -0.84, -1.85, -1.86, NA, -1.71,-1.9, -1.57,-1.52, NA, -1.53, -1.54, -4.04, -3.61, -2.85,-4.45),
    upper = c(NA, NA, NA, -0.83, -0.44, -1.26, -1.34, NA, -1.0, -0.98,-1.04, -0.87, NA, -1.04,-0.93, -1.59,-0.68, -0.82, -0.99)),
    .Names = c("Difference", "lower", "upper"), 
    row.names = c(NA, -19L), 
    class = "data.frame")

tabletext_acevccb<-cbind(
  c("", "Analysis", "", "Male", "", "Female","", "", "Aged <55yrs","",  "Aged >=55yrs", "", "", "White", "", "Black", "", "South Asian", ""),
  c("", "N", NA, "146,763", "X",  "123,425", "X", NA, "104,584","X", "165,604", "X", NA, "258,565", "X", "4,115", "X", "5,148", "X"), 
  c(NA, "Diff Sys BP 
    CCB vs ACE/ARB", NA, "-1.12","-0.64", "-1.55", "-1.60", NA, "-1.35", "-1.44", "-1.30", "-1.20", NA, "-1.29","-1.23",  "-2.82", "-2.15", "-1.84", "-2.72"), 
  c(NA, "95% CI", NA,"-1.41 : -0.83", "-0.84 : -0.44", "-1.85 : -1.26", "-1.86 : -1.34", NA, "-1.71 : -1.0", "-1.90 : -0.98", "-1.57 : -1.04", "-1.52 : -0.87", NA, "-1.53 : -1.04", "-1.54 : -0.93", "-4.04 : -1.59", "-3.61 : -0.68", "-2.85 : -0.82", "-4.45 : -0.99"))

forestplot(tabletext_acevccb, 
           main_acevccb,new_page = TRUE,    
           hrzl_lines=list("3" = gpar(lwd=1, col="#444444")), 
           is.summary=c(TRUE, TRUE, TRUE, rep(FALSE, 16)),
           txt_gp = fpTxtGp(label=gpar(cex=0.7)
           ),
           boxsize=0.25,
           xlog=F, 
           graphwidth = unit(7.5, "cm"),
           clip= c(-3.5, 0.5),
           xticks=c(-3.5, -3.0, -2.5, -2.0, -1.5, -1.0, -0.5 , 0, 0.5),
           col=fpColors(box="royalblue",line="darkblue"))

编辑下面是根据以下建议提供图表的代码。谢谢你提出这个建议。但是,要按照上图保持“n”、“diff”和“95% CI” - 我认为我的问题可以简化为“如何修改上面的 col=fpColors 代码以将每个框涂上不同的颜色?”

 main_acevccb <- structure(list(
   analysis = c( "Male","Male", "Female", "Female", NA, "<55", "<55", ">55", ">55",NA, "White", "White", "Black","Black", "SA", "SA"),
   mean  = c( -1.12, -0.64, -1.55,-1.60, NA, -1.35,-1.44, -1.3, -1.2, NA, -1.29,-1.23, -2.82,-2.15, -1.84,-2.72), 
    lower = c( -1.41, -0.84, -1.85, -1.86, NA, -1.71,-1.9, -1.57,-1.52, NA, -1.53, -1.54, -4.04, -3.61, -2.85,-4.45),
    upper = c( -0.83, -0.44, -1.26, -1.34, NA, -1.0, -0.98,-1.04, -0.87, NA, -1.04,-0.93, -1.59,-0.68, -0.82, -0.99),
    type = c( "adjusted", "matched","adjusted", "matched", NA, "adjusted", "matched","adjusted", "matched", NA, "adjusted", "matched","adjusted", "matched", "adjusted", "matched")),
    .Names = c("Analysis","Difference", "lower", "upper", "type"), 
    row.names = (c(NA, -16L)), 
    class = "data.frame")


adjusted <- subset(main_acevccb, type!="matched"|is.na(type))
matched  <- subset(main_acevccb, type!="adjusted"|is.na(type))


forestplot(mean=cbind(adjusted[,"Difference"], matched[,"Difference"]),
           lower=cbind(adjusted[,"lower"], matched[,"lower"]), 
           upper=cbind(adjusted[,"upper"], matched[,"upper"]), 
           labeltext=matched$Analysis,
          legend=c("Adjusted", "Matched"),
          legend.pos=("bottomright"),           
          legend.gp = gpar(col="#AAAAAA"), 
          legend.r=unit(.1, "snpc"),
            fn.ci_norm = c(fpDrawNormalCI, fpDrawCircleCI),
           boxsize = .30, 
           line.margin = .5, 
           clip=c(-4.0, 1.0), 
           xticks=c(-4.0, -3.5, -3.0, -2.5, -2.0, -1.5, -1.0, -0.5, 0, 0.5, 1),
           col=fpColors(box=c("darkblue", "darkred")),
           xlab="Diff in Systolic BP CCB vs ACE-I/ARB",
           new_page=TRUE)
4

1 回答 1

1

我意识到我可以使用这个示例http://gforge.se/2013/12/the-forestplot-of-dreams/制作两个单独的数据集,一个用于调整值,一个用于匹配值,然后组合 - 但宁愿不必再次输入所有值。

选项 1 如果您想坚持使用,forestplot我认为您提到的链接(或此处)中描述的解决方案是最佳的。您不必输入所有数据 - 只需在数据框中添加一个额外的列,然后对其进行子集化。

# You can add the column like this: 
type = c(NA, NA, NA, "adjusted", "matched","adjusted", "matched", NA, "adjusted", "matched","adjusted", "matched", NA, "adjusted", "matched","adjusted", "matched", "adjusted", "matched")

# So your dataframe will look like this: 
main_acevccb <- structure(list(
    mean  = c(NA, NA, NA, -1.12, -0.64, -1.55,-1.60, NA, -1.35,-1.44, -1.3, -1.2, NA, -1.29,-1.23, -2.82,-2.15, -1.84,-2.72), 
    lower = c(NA, NA, NA, -1.41, -0.84, -1.85, -1.86, NA, -1.71,-1.9, -1.57,-1.52, NA, -1.53, -1.54, -4.04, -3.61, -2.85,-4.45),
    upper = c(NA, NA, NA, -0.83, -0.44, -1.26, -1.34, NA, -1.0, -0.98,-1.04, -0.87, NA, -1.04,-0.93, -1.59,-0.68, -0.82, -0.99),
    type = c(NA, NA, NA, "adjusted", "matched","adjusted", "matched", NA, "adjusted", "matched","adjusted", "matched", NA, "adjusted", "matched","adjusted", "matched", "adjusted", "matched")),
   .Names = c("Difference", "lower", "upper", "type"), 
   row.names = c(NA, -19L), 
   class = "data.frame")

# ...and then you subset it:
adjusted <- subset(main_acevccb, type!="matched"|is.na(type))
matched  <- subset(main_acevccb, type!="adjusted"|is.na(type))

然后,您将有两个单独的数据框用于调整和匹配的值,并且可以按照链接中的说明进行操作。但是,使用该方法,您可以使用文本或不同的颜色。

选项 2如果您想同时拥有每行的文本标签和不同的颜色,您可以尝试ggplot

# add 2 extra columns to your dataframe
y=c(19, 18, 17, 16, 15, 14,13, 12, 11,10, 9, 8, 7, 6,5, 4,3, 2,1),
Analysis=c("","","", "Male", "Male","Female","Female", "", "Aged <55yrs","Aged <55yrs",  "Aged >=55yrs", "Aged >=55yrs", "", "White", "White", "Black", "Black", "South Asian", "South Asian")),

# so it will look like this:
main_acevccb <- 
  structure(list(
    mean  = c(NA, NA, NA, -1.12, -0.64, -1.55,-1.60, NA, -1.35,-1.44, -1.3, -1.2, NA, -1.29,-1.23, -2.82,-2.15, -1.84,-2.72), 
    lower = c(NA, NA, NA, -1.41, -0.84, -1.85, -1.86, NA, -1.71,-1.9, -1.57,-1.52, NA, -1.53, -1.54, -4.04, -3.61, -2.85,-4.45),
    upper = c(NA, NA, NA, -0.83, -0.44, -1.26, -1.34, NA, -1.0, -0.98,-1.04, -0.87, NA, -1.04,-0.93, -1.59,-0.68, -0.82, -0.99),
    type = c(NA, NA, NA, "adjusted", "matched","adjusted", "matched", NA, "adjusted", "matched","adjusted", "matched", NA, "adjusted", "matched","adjusted", "matched", "adjusted", "matched"),
    y=c(19, 18, 17, 16, 15, 14,13, 12, 11,10, 9, 8, 7, 6,5, 4,3, 2,1),
    Analysis=c("","","", "Male", "Male","Female","Female", "", "Aged <55yrs","Aged <55yrs",  "Aged >=55yrs", "Aged >=55yrs", "", "White", "White", "Black", "Black", "South Asian", "South Asian")),
  .Names = c("Difference", "lower", "upper", "type", "y", "Analysis"), 
    row.names = c(NA, -19L), 
    class = "data.frame")

# and make a graph with ggplot
p <- ggplot(data=main_acevccb,
       aes(x = type,y = Difference, ymin = lower, ymax = upper ))+
  geom_pointrange(aes(col=type))+
  geom_hline(aes(fill=type),yintercept =0, linetype=2)+
  xlab('Type')+ ylab("Your axis title)")+
  geom_errorbar(aes(ymin=lower, ymax=upper,col=type),width=0.5,cex=1)+
  facet_wrap(~Analysis,strip.position="left",nrow=9,scales = "free_y") +
  theme(plot.title=element_text(size=16,face="bold"),
    axis.text.y=element_blank(),
    axis.ticks.y=element_blank(),
    axis.text.x=element_text(face="bold"),
    axis.title=element_text(size=12,face="bold"),
    strip.text.y = element_text(hjust=0,vjust = 1,angle=180,face="bold"))+
  coord_flip()
p <- p+ylim(-5, 5)

# and finally add the text labels (modify it to get the labels you want) 
p <- p+geom_text(label=c(rep("Difference (CI95%)")), y=1, hjust="left")  
p
于 2019-03-13T01:05:45.943 回答