title: How to create a legend in a forest plot using the forestplot package in R?
body: <p>I am trying to create a forestplot that looks like this --> </p>
  
  <p>fig.1</p>
  
  <p><a href="https://i.stack.imgur.com/MiMWK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MiMWK.png" alt="enter image description here"></a></p>
  
  <p><strong><em>I am having difficulties in creating a legend as shown on the right side of the figure.</em></strong></p>
  
  <p>My data is as follows:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I am able to get the plot but I can't get the legend as shown in right side of fig.1. </p>
  
  <p>My code is as follows:</p>
  
  <pre><code>library(forestplot)
  genes_df &lt;- read.table("data.txt", header=T, sep="\t")
  
  data &lt;- 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 &lt;- 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()
  </code></pre>
  
  <p>My y-axis labels have all the values in the columns, Gene_ID, HR, low and high. Using the argument <strong>graph.pos = 2</strong>, 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.</p>
  
  <p>However, when I try the same i.e to get a legend with the argument "legend" I get the following error :</p>
  
  <blockquote>
    <p>Error in fn.legend[[i]] : object of type 'closure' is not subsettable</p>
  </blockquote>
  
  <p>I don't understand what this error is.</p>