我想创建一个具有调整值(每个层的顶线)和匹配值(每个层中x表示的线)的图,例如男人,女性,年龄和lt; 55,年龄> 55等。对于匹配的值,我想拥有钻石/其他形状或不同的颜色来突出显示。但不确定该怎么做?
我意识到我可以使用此示例http://gforge.se/2013/12/the-forestplot-of-dreams/来制作两个单独的数据集,一个用于调整值,一个用于匹配的值,然后组合 - 但宁愿不必再次输入所有值。
有人可以帮助编辑我的代码的第三部分,以制作我的图形中匹配值的差异形状或差异颜色(匹配的阀门是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"one_answers" 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)
我意识到我可以使用此示例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