I want to create a plot that has adjusted values (top line for each stratum) and matched values (line represented by X in each stratum) across all strata eg men, women, age <55, age >55 etc. For the matched values, I'd like to have a diamond/other shape or different colour to highlight. But not sure how to do?
I realise I could use this example http://gforge.se/2013/12/the-forestplot-of-dreams/ to make two separate datasets, one for adjusted values and one for matched values, and then combine - but would rather not have to input all values again.
Can someone please help edit the third section of my code to make a diff shape or diff colour for the matched values in my graph (matched vals are the lines represented by X in the N column).
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"))
EDIT Below is the code to provide graph as per below advice. Thank you for suggesting this. However, to maintain the "n", "diff" and "95% CI" as per my above graph - I think my question can be simplified to "how can I amend my col=fpColors code above to paint each box a different color?"
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)
I realise I could use this example http://gforge.se/2013/12/the-forestplot-of-dreams/ to make two separate datasets, one for adjusted values and one for matched values, and then combine - but would rather not have to input all values again.
Option 1 If you want to stick with forestplot
I think the solution described in link you mentioned (or here) is optimal .
You don´t have to type all the data - just add an extra column to your dataframe and then subset it.
# 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))
Then you´ll have two separate dataframes for adjusted and matched values and can proceed as described in the link. However, with that method you can either have text OR different colors.
Option 2 If you want to have both, the text labels for each row AND different colors, you could try 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