CHAa
CHAa

Reputation: 13

R: Arranging variables in forest plot

I have made a forest plot code using "Pimping your forest plot"

I would like to compare male VS female difference among variable subgroups.

To be more specific, if

Variable1: Age / Subgroups: 30-39years, 40-49years, 50-59years

Variable2: Education / Subgroups: <9years, 9-12 years

I want to compare sex difference among variables like this,(I want the forestplot variable array in this kind of way)

**Variable1

subgroups (male)

30-39 years

40-49 years

50-59 years

subgroups (female)

30-39 years

40-49 years

50-59 years

Variable2

subgroups (male)

<9 years

9-12 years

subgroups (female)

<9 years

9-12 years

**

How should I code to make this kind of forest plot?

Now I have made a forest plot in this variable order,

Variable1 30-39 years(male)

Variable1 30-39 years(female)

Variable1 40-49 years(male)

Variable1 40-49 years(female)

Variable1 50-59 years(male)

Variable1 50-59 years(female)

Variable2 <9 years (male)

Variable2 <9 years (female)

Variable2 9-12 years (male)

Variable2 9-12 years (female)

My current code is this,

Male<-structure(c(0.22,0.54,2.09,2.65,1.04,1.16,1.15,0.78,1.06,0.99,1.06,0.97,1.00,1.80,0.81), .Dim=c(5L,3L),.Dimnames=list(c("age,30-39years","age,40-49years","age,50-59years" "education,<9 years","education, 9-12 years" ),c("OR","L","U")))

Female<-structure(c(0.89,1.47,1.08,1.32,1.41,1.38,1.00,27.01,5.15,0.88,0.28,0.96,0.49,0.36,1.61),.Dim=c(17L,3L),.Dimnames=list(c("age, 30-39years","age,40-49years","age,50-59years","education,<9 years","education,   9-12 years"),c("OR","L","U")))

library(Gmisc)

forestplot2(mean=cbind(log(Male[,"OR"]),log(Female[,"OR"])), lower=cbind(log(Male[,"L"]),log(Female[,"L"])),upper=cbind(log(Male[,"U"]),log(Female[,"U"])),labeltext=rownames(Male),legend=c("Male", "Female"),clip=c(-2.5,5.0), boxsize=0.1,col=fpColors(box=c("blue", "darkred")),fn.ci_norm=c("fpDrawNormalCI", "fpDrawCircleCI"),xlab="Hypertension prevalence among sex difference",new_page=TRUE)

Please help me~ Thanks

Upvotes: 0

Views: 983

Answers (1)

Max Gordon
Max Gordon

Reputation: 5467

Please make sure that your example data is clean and working before posting. Here's a solution that I think solves your issue:

Male<-structure(c(0.22,0.54,2.09,2.65,1.04,1.16,1.15,0.78,1.06,0.99,1.06,0.97,1.00,1.80,0.81), 
                .Dim=c(5L,3L),
                .Dimnames=list(c("age,30-39years","age,40-49years","age,50-59years","education,<9 years","education, 9-12 years"),
                               c("OR","L","U")))

Female<-structure(c(0.89,1.47,1.08,1.32,1.41,1.38,1.00,27.01,5.15,0.88,0.28,0.96,0.49,0.36,1.61),
                  .Dim=c(5L,3L),
                  .Dimnames=list(c("age, 30-39years","age,40-49years","age,50-59years","education,<9 years","education,   9-12 years"),c
                                 ("OR","L","U")))

# Messy input data - something wrong
for (i in 1:nrow(Male)) {
  tmp <- Male[i,]
  low <- which.min(tmp)[1]
  high <- which.max(tmp)[1]
  Male[i,] <- c(Male[i,c(-low, -high)],
                Male[i,low],
                Male[i,high])

  tmp <- Female[i,]
  low <- which.min(tmp)[1]
  high <- which.max(tmp)[1]
  Female[i,] <- c(Female[i,c(-low, -high)],
                Female[i,low],
                Female[i,high])
}

library(forestplot)
library(abind)
Male <- Gmisc::insertRowAndKeepAttr(Male, 
                                    grep("education", rownames(Male))[1], 
                                    rName = "Education")
Female <- Gmisc::insertRowAndKeepAttr(Female, 
                                      grep("education", rownames(Female))[1], 
                                      rName = "Education")
Male <- Gmisc::insertRowAndKeepAttr(Male, 1, rName = "Age")
Female <- Gmisc::insertRowAndKeepAttr(Female, 1, rName = "Age")


out = abind(Male, Female, along = 3)
rownames(out) <- gsub("(age|education),[ ]*", "  ", rownames(out))
forestplot(out,
           xlog = TRUE,
           legend=c("Male", "Female"),
           clip=exp(c(-2.5,5.0)), 
           col=fpColors(box=c("blue", "darkred")),
           fn.ci_norm=c("fpDrawNormalCI", "fpDrawCircleCI"),
           xlab="Hypertension prevalence among sex difference",
           new_page=TRUE)

Gives this: enter image description here

Note that for simplicity I feed it a 3-dim array. Unfortunately there was a bug in the package and you need to download the development version (>1.5) of the package.

Upvotes: 2

Related Questions