user2363642 - 8 months ago 63

R Question

I am using "pimp your forest plot" to make some nice graphs. http://www.r-bloggers.com/pimping-your-forest-plot/

The tutorial explains how to make a nice forest plot, combing data from two countries to have a look at subgroup effects. Each country has its own distinctive shape on the graph eg sweden is a diamond... so that the subgroup effects in countries are easy to pick out.

I'm having a problem when trying to merge three dfs (for three countries) though. Instead of retaining separate shapes for each country in each subgroup (see graph when combining two countries), when I put three countries together then all the shapes for gender are circular, all the shapes for age are square and so on. Instead it should be that there is a circle, a diamond and a square to represent the effects of gender/age in each country.

Does anyone know what I'm doing wrong here? I've been retracing my steps and adding one df at a time so that I can at least try to see what I'm doing wrong: but it's not coming to me.

I've copied some dfs from the "pimp your forest plot" here: all credit for these graphs are to Max Gordon. I've made a fake third df called "finland" for the sake of an example here.

`sweden1`

coef lower upper

Males vs Female 0.04088551 0.03483956 0.04693145

85 vs 65 years -0.05515741 -0.06508088 -0.04523394

Charlsons Medium vs Low -0.03833060 -0.04727946 -0.02938173

denmark1

coef lower upper

Males vs Female 0.03462842 0.003494374 0.065762462

85 vs 65 years -0.03682791 -0.083367305 0.009711488

Charlsons Medium vs Low -0.04335537 -0.090336663 0.003625929

finland1

coef lower upper

Males vs Female 0.061 0.043 0.087

85 vs 65 years -0.080 -0.120 -0.020

Charlsons Medium vs Low -0.050 -0.075 -0.025

To make a forest plot with two countries: use Max Gordon's code from the referenced website:

`library(forestplot)`

forestplot(mean=cbind(sweden1[,"coef"], denmark1[,"coef"]),

lower=cbind(sweden1[,"lower"], denmark1[,"lower"]),

upper=cbind(sweden1[,"upper"], denmark1[,"upper"]),

labeltext=rownames(Sweden),

legend=c("Sweden", "Denmark"),

legend.pos=list(x=0.8,y=.4),

legend.gp = gpar(col="#AAAAAA"),

legend.r=unit(.1, "snpc"),

clip=c(-.2, .2),

xticks=c(-.2, -.1, .0, .1, .2),

boxsize=0.3,

col=fpColors(box=c("blue", "darkred")),

# Set the different functions

confintNormalFn=c("fpDrawDiamondCI", "fpDrawCircleCI"),

xlab="EQ-5D index",

new_page=TRUE)

I use this code to add in finland, but see how the shapes do not stay true to their groups.

`forestplot(mean=cbind(sweden1[,"coef"], denmark1[,"coef"],finland1[,"coef"]),`

lower=cbind(sweden1[,"lower"], denmark1[,"lower"],finland1[,"lower"]),

upper=cbind(sweden1[,"upper"], denmark1[,"upper"],finland1[,"upper"]),

labeltext=rownames(sweden1),

legend=c("Sweden", "Denmark", "finland1"),

# Added the clip argument as some of

# the Danish CI are way out therer

#clip=c(-.2, .2),

# Getting the ticks auto-generate is

# a nightmare - it is usually better to

# specify them on your own

# xticks=c(-.2, -.1, .0, .1, .2),

boxsize=0.3,

col=fpColors(box=c("blue", "darkred", "green")),

confintNormalFn=c("fpDrawCircleCI", "fpDrawNormalCI","fpDrawDiamondCI"),

xlab="EQ-5D index",

new_page=TRUE)

Thanks in advance.

EDIT

`sweden1 <- structure(c(0.0408855062954068, -0.0551574080806885, -0.0383305964199184,`

0.0348395599810297, -0.0650808763059716, -0.0472794647337126,

0.046931452609784, -0.0452339398554054, -0.0293817281061242), .Dim = c(3L,

3L), .Dimnames = list(c("Males vs Female", "85 vs 65 years",

"Charlsons Medium vs Low"), c("coef", "lower", "upper")))

denmark1 <- structure(c(0.0346284183072541, -0.0368279085760325, -0.0433553672510346,

0.00349437418972517, -0.0833673052667752, -0.0903366633240568,

0.065762462424783, 0.00971148811471034, 0.00362592882198759), .Dim = c(3L,

3L), .Dimnames = list(c("Males vs Female", "85 vs 65 years",

"Charlsons Medium vs Low"), c("coef", "lower", "upper")))

finland1 <- structure(c(0.061, -0.08, -0.05, 0.043, -0.12, -0.075, 0.087,

-0.02, -0.025), .Dim = c(3L, 3L), .Dimnames = list(c("Males vs Female",

"85 vs 65 years", "Charlsons Medium vs Low"), c("coef", "lower",

"upper")))

Answer

It is hard to deduce the intention of the different drawing functions in a square structure and you therefore need to supply the function a square matrix, in this case a 3x3 matrix. Not sure this was the wisest design choice from my part, but it is rather simple to fix:

```
sweden1 <-
structure(c(0.0408855062954068, -0.0551574080806885, -0.0383305964199184,
0.0348395599810297, -0.0650808763059716, -0.0472794647337126,
0.046931452609784, -0.0452339398554054, -0.0293817281061242),
.Dim = c(3L, 3L),
.Dimnames = list(c("Males vs Female", "85 vs 65 years", "Charlsons Medium vs Low"),
c("coef", "lower", "upper")))
denmark1 <- structure(c(0.0346284183072541, -0.0368279085760325, -0.0433553672510346,
0.00349437418972517, -0.0833673052667752, -0.0903366633240568,
0.065762462424783, 0.00971148811471034, 0.00362592882198759),
.Dim = c(3L, 3L),
.Dimnames = list(c("Males vs Female", "85 vs 65 years", "Charlsons Medium vs Low"),
c("coef", "lower", "upper")))
finland1 <- structure(c(0.061, -0.08, -0.05,
0.043, -0.12, -0.075,
0.087, -0.02, -0.025),
.Dim = c(3L, 3L),
.Dimnames = list(c("Males vs Female", "85 vs 65 years", "Charlsons Medium vs Low"),
c("coef", "lower", "upper")))
forestplot(mean=cbind(sweden1[,"coef"], denmark1[,"coef"],finland1[,"coef"]),
lower=cbind(sweden1[,"lower"], denmark1[,"lower"],finland1[,"lower"]),
upper=cbind(sweden1[,"upper"], denmark1[,"upper"],finland1[,"upper"]),
labeltext=rownames(sweden1),
legend=c("Sweden", "Denmark", "finland1"),
boxsize=0.1,
col=fpColors(box=c("kblue", "darkred", "darkgreen")),
fn.ci_norm=matrix(c("fpDrawCircleCI", "fpDrawNormalCI","fpDrawDiamondCI"),
nrow = 3, ncol=3, byrow=T),
xlab="EQ-5D index",
new_page=TRUE)
```

Produces this graph:

Source (Stackoverflow)