0

I borrowed (and slightly simplified) code for a ggplot2 plotting function from: https://bookdown.org/MathiasHarrer/Doing_Meta_Analysis_in_R/plotting-the-summary.html

Given the following data, the function plot_rob() produces the graph shown.

I would like to display the bars in the following order (from top to bottom):

"Random sequence generation", "Allocation concealment", "Blinding of participants", "Blinding of personnel", "Blinding of outcome assessor", "Incomplete outcome data", "Intention to treat analysis", "Group similarity at baseline", "Co-interventions", "Compliance", "Timing of outcome assessments"

  data <-  structure(list(name = c("Random sequence generation", "Allocation concealment", 
    "Blinding of participants", "Blinding of personnel", "Blinding of outcome assessor", 
    "Incomplete outcome data", "Intention to treat analysis", "Group similarity at baseline", 
    "Co-interventions", "Compliance", "Timing of outcome assessments", 
    "Random sequence generation", "Allocation concealment", "Blinding of participants", 
    "Blinding of personnel", "Blinding of outcome assessor", "Incomplete outcome data", 
    "Intention to treat analysis", "Group similarity at baseline", 
    "Co-interventions", "Compliance", "Timing of outcome assessments", 
    "Random sequence generation", "Allocation concealment", "Blinding of participants", 
    "Blinding of personnel", "Blinding of outcome assessor", "Incomplete outcome data", 
    "Intention to treat analysis", "Group similarity at baseline", 
    "Co-interventions", "Compliance", "Timing of outcome assessments"
    ), RoB = c("U", "H", "H", "H", "H", "H", "H", "L", "L", "L", 
    "L", "U", "L", "H", "H", "H", "L", "L", "H", "L", "L", "L", "L", 
    "L", "H", "H", "H", "H", "H", "L", "L", "L", "L")), class = c("tbl_df", 
    "tbl", "data.frame"), row.names = c(NA, -33L))

The function plot_rob() below:

plot_rob <- function(rob.long) {
  rob.long$RoB<-as.factor(rob.long$RoB)
  rob.long$RoB<-factor(rob.long$RoB,levels(rob.long$RoB)[c(1,3,2)])
   rob.plot <-ggplot(data=rob.long)+
    geom_bar(mapping=aes(x=name,fill=RoB),
             width=0.7,
             position = "fill",
             color="black")+
    coord_flip(ylim = c(0,1))+
    guides(fill = guide_legend(reverse = TRUE))+
    scale_fill_manual("Risk of Bias",
                      labels = c("    High risk of bias          ",
                                 "    Unclear risk of bias       ",
                                 "    Low risk of bias  "),
                      values = c("U" = "#E2DF07",
                                 "H" = "#BF0000",
                                 "L" = "#02C100"))+
    scale_y_continuous(labels = scales::percent)+
    theme(axis.title.x=element_blank(),
          axis.title.y=element_blank(),
          axis.ticks.y=element_blank(),
          axis.text.y = element_text(size=18, color = "black"),
          axis.line.x = element_line(colour = "black",
                                     size = 0.5, linetype = "solid"),
          legend.position = "bottom",
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          panel.background = element_blank(),
          legend.background = element_rect(linetype="solid",
                                           colour ="black"),
          legend.title = element_blank(),
          legend.key.size = unit(0.75,"cm"),
          legend.text=element_text(size=12))
     return(rob.plot)
   }

Graph produced for data below

user25494
  • 1,289
  • 14
  • 27
  • make it a factor and the specify the levels arranged in reversed order, like if the order you want top to bottom is this `order <- c("Random sequence generation", "Allocation concealment", "Blinding of participants", "Blinding of personnel", "Blinding of outcome assessor", "Incomplete outcome data", "Intention to treat analysis", "Group similarity at baseline", "Co-interventions", "Compliance", "Timing of outcome assessments")` then make `name` a factor like this `rob.long$name<-factor(rob.long$name, levels=rev(order))` – DS_UNI Mar 22 '19 at 13:17
  • Why is 'levels = rev(order)' required, rather than 'levels = order'? – user25494 Mar 22 '19 at 13:39
  • Because of the axis flip by `coord_flip()`. The rightmost tick on the x axis becomes the top tick on the y axis. So you want your first entry in the name column to be on the last tick on the x axis, which puts it on the top after the flip. – Mojoesque Mar 22 '19 at 13:48

2 Answers2

1

As @DS_UNI already mentioned you have to convert the name column to a factor and then specify the order. In this case you want the (reverse) order in which they appear so you can use the following line:

library(tidyverse)
data %>% 
  mutate(name = fct_inorder(name) %>% fct_rev()) %>% 
  plot_rob()

enter image description here

Mojoesque
  • 1,166
  • 8
  • 15
0
order <- c("Random sequence generation", "Allocation concealment", "Blinding of participants", 
"Blinding of personnel", "Blinding of outcome assessor", "Incomplete outcome data", 
"Intention to treat analysis", "Group similarity at baseline", 
"Co-interventions", "Compliance", "Timing of outcome assessments"
)

I modified the function as implied by @DS_UNI to produce an arbitrary order of horizontal bars.

plot_rob <- function(rob.long, order) {
  rob.long$RoB<-as.factor(rob.long$RoB)
  rob.long$RoB<-factor(rob.long$RoB,levels(rob.long$RoB)[c(1,3,2)])
  rob.long$name<-factor(rob.long$name, levels=rev(order))
   rob.plot <-ggplot(data=rob.long)+
    geom_bar(mapping=aes(x=name,fill=RoB),
             width=0.7,
             position = "fill",
             color="black")+
    coord_flip(ylim = c(0,1))+
    guides(fill = guide_legend(reverse = TRUE))+
    scale_fill_manual("Risk of Bias",
                      labels = c("    High risk of bias          ",
                                 "    Unclear risk of bias       ",
                                 "    Low risk of bias  "),
                      values = c("U" = "#E2DF07",
                                 "H" = "#BF0000",
                                 "L" = "#02C100"))+
    scale_y_continuous(labels = scales::percent)+
    theme(axis.title.x=element_blank(),
          axis.title.y=element_blank(),
          axis.ticks.y=element_blank(),
          axis.text.y = element_text(size=18, color = "black"),
          axis.line.x = element_line(colour = "black",
                                     size = 0.5, linetype = "solid"),
          legend.position = "bottom",
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          panel.background = element_blank(),
          legend.background = element_rect(linetype="solid",
                                           colour ="black"),
          legend.title = element_blank(),
          legend.key.size = unit(0.75,"cm"),
          legend.text=element_text(size=12))
     return(rob.plot)
   }
user25494
  • 1,289
  • 14
  • 27