Peter - 4 months ago 45

R Question

I am having a hard time with the forest plot package in R. Here is my code. Actually everything works well beside the legend.

. However for the legend, I would like to have a Blue Circle, A red Square and a green losange in stead of 3 squares.

Any idea?

Thanks in advance.

Peter

`library(forestplot)`

test_data <- data.frame(coef1=c(0.54,0.72,0.57),

coef2=c(0.59,0.79,0.58),

coef3=c(0.49,0.60,0.48),

low1=c(0.41,0.46,0.42),

low2=c(0.44,0.49,0.42),

low3=c(0.37,0.37,0.35),

high1=c(0.72,1.12,0.77),

high2=c(0.78,1.26,0.80),

high3=c(0.65,0.99,0.66))

col_no <- grep("coef", colnames(test_data))

row_names <- list(

list("Behavioral CVH","Biological CVH","Total CVH"))

coef <- with(test_data, cbind(coef1, coef2, coef3))

low <- with(test_data, cbind(low1, low2, low3))

high <- with(test_data, cbind(high1, high2, high3))

forestplot(row_names, coef, low, high,

title="Paris Prospective Study 3",

fn.ci_norm=matrix(c("fpDrawCircleCI", "fpDrawNormalCI","fpDrawDiamondCI"),

nrow = 3, ncol=3, byrow=T),

zero = c(1), boxsize=0.05,

col=fpColors(box=c("royalblue", "gold", "black"),

line=c("darkblue", "orange", "black"),

summary=c("darkblue", "red", "black"),

hrz_lines = "#444444"),

xlab="Odds ratio & 95% Confidence intervals",

vertices = TRUE,

new_page = TRUE,

legend=c("Q2 vs. Q1","Q3 vs. Q1","Q4 vs. Q1"),

legend_args = fpLegend(pos = list("topright"),

title="Legend",

r = unit(0, "snpc"),

gp = gpar(col="#CCCCCC", lwd=1.5)))

Answer

One thing you can do is use the "regular" call to `legend`

, outside the call for the forestplot.

To do that, you'll first have to call `plot.new`

:

```
plot.new()
forestplot(...) # without the legend part
legend("topright", c("Q2 vs. Q1","Q3 vs. Q1","Q4 vs. Q1"), title="Legend", border="#CCCCCC", box.lwd=1.5,
col=c("blue", "red", "green"), pch=c(16, 15, 18))
```