0
votes

I am trying to create a forestplot that looks like this -->

fig.1

enter image description here

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.

1
Not sure on this function, but you could recreate a similar graph in ggplot easily enough. Does the data only give the rate and bounds? Or would you be able to tease out all values to get the distribution for each gene? If you are able to get all data, then the plot is just a stacked boxplot graph. Then, you could just annotate the graph with the legend info you want, or alter the text for the legend created by ggplot. - creutzml
@creutzml actually I don't have all the values for each gene. Whatever data I have shown in the question is all that I have. Also, I tried with ggplot2 to create a similar graph but again I am stuck with the legend part. - Jesvin Joy
Well let's try troubleshooting those Error messages instead... what structure are coef, low, and high? 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, but fn.legend is a function, which is not subsettable. - creutzml
Edit the question with a reproducible example and I may be able to pinpoint it more specifically. - creutzml
@creutzml the forestplot requires arguments "mean, lower and upper". So I have created a data frame with values for these arguments. I have edited the question in the R code part. Please take a look at it. - Jesvin Joy

1 Answers

1
votes

The argument that decides on the column for the graph is: graph.pos

forestplot(main_list,
           coef,
           low,
           high,
           graph.pos = 1,
           xlog = TRUE,
           zero = 0.75,
           boxsize=0.25,
           col = fpColors(box="black",line="darkblue"),
           ci.vertices = TRUE,
           ci.vertices.height = 0.25,
           #legend = "HR",
           #legend_args = fpLegend(pos = list("topright"),title="95%- CI",r = unit(.1, "snpc"),gp = gpar(col="#CCCCCC", lwd=1.5)),
           lineheight = "auto",
           xlab = "Hazard ratio",
           title = "Hazard ratio plot")