I am trying to create a forestplot that looks like this -->
fig.1
I am having difficulties in creating a legend as shown on the right side of the figure.
My data is as follows:
Gene_ID HR low high
Gene_1 0.83 0.78 0.89
Gene_2 0.61 0.51 0.74
Gene_3 0.85 0.8 0.9
Gene_4 0.77 0.7 0.84
Gene_5 0.75 0.68 0.83
Gene_6 0.81 0.76 0.87
Gene_7 0.85 0.81 0.9
Gene_8 0.8 0.74 0.86
Gene_9 0.82 0.76 0.88
Gene_10 0.8 0.73 0.87
I am able to get the plot but I can't get the legend as shown in right side of fig.1.
My code is as follows:
library(forestplot)
genes_df <- read.table("data.txt", header=T, sep="\t")
data <- structure(list(HR = c(NA,genes_df$HR),
low = c(NA,genes_df$low),
high = c(NA,genes_df$high)),
.Names = c("HR", "low", "high"),
row.names = c(NA,-11L),
class = "data.frame")
labels <- cbind(c("Gene_ID","Gene_1","Gene_2","Gene_3","Gene_4","Gene_5","Gene_6","Gene_7","Gene_8","Gene_9","Gene_10"),
c("HR","0.83","0.61","0.85","0.77","0.75","0.81","0.85","0.8","0.82","0.8"),
c("low","0.78","0.51","0.8","0.7","0.68","0.76","0.81","0.74","0.76","0.73"),
c("high","0.89","0.74","0.9","0.84","0.83","0.87","0.9","0.86","0.88","0.87"))
print("....Creating the plot....")
jpeg(filename="Hazard_ratio_plot.jpg",units="cm",width=20,height=17, res=800)
forestplot(labels,
data,new_page = TRUE,
boxsize = .25,
zero = 0.707,
ci.vertices = TRUE,
ci.vertices.height = 0.25,
xlog=TRUE,
cex = 0.1,
graph.pos = 2,
lwd.zero = gpar(lty=1, alpha = 1),
lineheight = "auto",
title = "Hazard ratio plot",
txt_gp = fpTxtGp(label=gpar(fontfamily="Calibri")),
col = fpColors(box="blue",line="black",zero = "black"),
xlab="Hazard ratio")
dev.off()
My y-axis labels have all the values in the columns, Gene_ID, HR, low and high. Using the argument graph.pos = 2, I am able to separate the y-axis labels: HR, low and high and get it to the right side of the plot and make it seem like a legend for the plot.
However, when I try the same i.e to get a legend with the argument "legend" I get the following error :
Error in fn.legend[[i]] : object of type 'closure' is not subsettable
I don't understand what this error is.

coef,low, andhigh? I'm guessing one of those is different the others with differing number of observations or different structure. So make sure those are the same. If they are, then the first error should be resolved. Also, it seems like that second error message came up, because of how you tried to subset the data. For whatever reason,fn.legend[[i]]was attempted to execute, butfn.legendis a function, which isnot subsettable. - creutzml