在ggplot2中更改水平条的顺序

时间:2019-03-22 12:57:20

标签: r ggplot2

我从https://bookdown.org/MathiasHarrer/Doing_Meta_Analysis_in_R/plotting-the-summary.html借来(略微简化了)ggplot2绘图功能的代码

鉴于以下数据,函数plot_rob()会生成所示的图形。

我想按以下顺序显示条形图(从上到下):

“随机序列生成”,“分配隐藏”,“参与者失明”,“人员失明”,“结果评估者失明”,“结果数据不完整”,“治疗意图分析”,“组相似性”基线”,“共同干预”,“合规性”,“结果评估的时间安排”

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

下面的函数plot_rob():

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

2 个答案:

答案 0 :(得分:1)

正如@DS_UNI所述,您必须将name列转换为一个因数,然后指定顺序。在这种情况下,您希望它们以(反向)顺序出现,因此您可以使用以下行:

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

enter image description here

答案 1 :(得分: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"
)

我修改了@DS_UNI所隐含的功能,以生成任意顺序的水平条。

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