user13973103
user13973103

Reputation:

How can I change the order of subgroups in a forest plot using the meta package in R?

I would like some assistance in changing the order subgroups appear in my forest plot. I am using the meta package in R to perform my meta-analysis and generate my forest plot. I've included some example data below.

mydata <- data.frame(
  A = 1:9,
  Ms = sample(100:500, 9),
  Ss = sample(10:50, 9),
  Mr = sample(100:500, 9),
  Sr = sample(10:50, 9),
  Ns = sample(5:50, 9),
  Nr = sample(5:50, 9),
  P = sample(c("foot", "tibia", "lumbar"), 9, replace = TRUE)
  )

Here is some basic code used to run my meta-analysis.

ma <- metacont(Ns,
               Ms,
               Ss,
               Nr,
               Mr,
               Sr,
               data = mydata,
               byvar = P,
               comb.fixed = FALSE,
               comb.random = TRUE)

I use the byvar function to run a subgroup analysis. Here is the code I use to plot the results.

forest(ma,
       layout = "RevMan5",
       subgroup = TRUE,
       print.byvar = FALSE)

I would like the subgroups to be ordered foot, tibia and lumbar, rather than foot, lumbar and tibia (as shown in the link below). I'm stuck on how to do this and any help will be greatly appreciated.

enter image description here

Upvotes: 2

Views: 3461

Answers (1)

Ian Campbell
Ian Campbell

Reputation: 24810

In the most recent version of the meta package, you should set the factor levels of P in the original dataset.

Consider this original version:

set.seed(123)
mydata <- data.frame(A = 1:9,  Ms = sample(100:500, 9), Ss = sample(10:50, 9),  Mr = sample(100:500, 9),
                     Sr = sample(10:50, 9),  Ns = sample(5:50, 9),  Nr = sample(5:50, 9),
                     P = sample(c("foot", "tibia", "lumbar"), 9, replace = TRUE))

ma <- metacont(Ns, Ms, Ss, Nr, Mr, Sr, data = mydata, byvar = P, comb.fixed = FALSE, comb.random = TRUE)

enter image description here

Now change the factor levels of P:

mydata$P <- factor(mydata$P, levels = c("foot", "tibia", "lumbar"))

ma <- metacont(Ns, Ms, Ss, Nr, Mr, Sr, data = mydata, byvar = P, comb.fixed = FALSE, comb.random = TRUE)
forest(ma, layout = "RevMan5", subgroup = TRUE, print.byvar = FALSE, bysort = FALSE)

enter image description here

In previous versions you could change the $bylevs element of the metacont object.

ma$bylevs <- c("foot", "tibia", "lumbar")
forest(ma,
       layout = "RevMan5",
       subgroup = TRUE,
       print.byvar = FALSE)

Upvotes: 3

Related Questions