[英]How to create a legend in a forest plot using the forestplot package in R?
I am trying to create a forestplot that looks like this --> 我正在尝试创建一个看起来像这样的森林图->
fig.1 图。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. 我能够得到该图,但无法得到图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. 我的y轴标签在Gene_ID,HR,低和高列中具有所有值。 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. 使用参数graph.pos = 2 ,我可以分隔y轴标签:HR,低和高,并将其显示在图的右侧,并使其看起来像图的图例。
However, when I try the same ie 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 fn.legend [[i]]中的错误:“关闭”类型的对象不可子集化
I don't understand what this error is. 我不明白这个错误是什么。
The argument that decides on the column for the graph is: graph.pos
决定图的列的参数是: 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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.