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)
}