【问题标题】:ggplot: How can I put my legend information below the xaxis?ggplot:如何将图例信息放在 x 轴下方?
【发布时间】:2020-11-21 00:33:47
【问题描述】:

这是我的数据:

data_g <- data.frame(
  study_ID = c("ben-amitay2006","ben-ari2018", "ben-ari2019a","ben-ari2019b","connolly2004","lopez2008","phelan2009","toren2007","ben-amitay2006","ben-ari2019b","connolly2004","ben-amitay2006",
               "demaso2014","toren2007","ben-amitay2006","demaso2014","toren2007","demaso2014","ben-ari2018","ben-ari2019a","ben-ari2019b","kubota2011","sarrechia2015","ben-ari2018","ben-ari2019a",
               "sarrechia2015"),
  symptom = c("PTSD","PTSD","PTSD","PTSD","PTSD","PTSD","PTSD","PTSD",
              "Subthreshold PTSD","Subthreshold PTSD","Subthreshold PTSD","Depressive Symptoms",
              "Depressive Symptoms","Depressive Symptoms","Anxiety Sympomts","Anxiety Sympomts","Anxiety Sympomts",
              "Disruptive Behavior Sympomts","CBCL Clinical Range","CBCL Clinical Range","CBCL Clinical Range",
              "CBCL Clinical Range","CBCL Clinical Range","CBCL Borderline Range","CBCL Borderline Range","CBCL Borderline Range"),
  prevalence = c(0,10.39, 33.3, 10.2, 12, 0,0,29.03,7.7,26.4, 12,5, 4,18.52,12.5, 5,16.13,
                 16, 11, 30, 27.3, 45, 8.1,4,26.7, 3.9)
)

data_g$symptom <- factor(data_g$symptom, levels = c('PTSD', 'Subthreshold PTSD', 'Depressive Symptoms', 'Anxiety Sympomts', 'Disruptive Behavior Sympomts', 'CBCL Clinical Range', 'CBCL Borderline Range'))

library(RColorBrewer)

这是我目前所拥有的:

ggplot(data_g, aes(symptom, prevalence, fill = study_ID, label = study_ID)) + 
  geom_bar(stat="identity", position = position_dodge(preserve = 'single')) +
  scale_fill_brewer(palette = "Paired") +
  labs(x = "", fill = "Study ID") +
  ylim(0, 100) +
  theme(text = element_text(size=15)) +
  scale_x_discrete(guide = guide_axis(n.dodge = 2))+
  labs(title="Prevalence Rates of Psychological Symptoms after Surgery across Studies", x = "Psychological Symptoms", y = "Prevalence Rates in Percentage") ```

First Version with Legend

如何在 xaxis 下方插入我的图例信息,或者通过使用垂直线或其他任何东西更清楚哪些条属于哪个症状?

我会很高兴看到症状组之间的 ablines。但是,如果有人也知道如何将研究 ID 放在 xaxis 垂直下方,那就太好了!

这是我尝试过的:

ggplot(data_g, aes(symptom, prevalence, fill = study_ID, label = study_ID)) + 
  geom_bar(stat="identity", position = position_dodge(preserve = 'single')) +
  scale_fill_brewer(palette = "Paired") +
  labs(x = "", fill = "Study ID") +
  ylim(0, 100) +
  theme(text = element_text(size=15)) +
  scale_x_discrete(guide = guide_axis(n.dodge = 2))+
  geom_text(position = position_dodge(width = 1), aes(x=symptom, y=0), angle = 90, vjust=0, hjust = -0.06, size=2.5) +
  theme(legend.position = "none") +
  labs(title="Prevalence Rates of Psychological Symptoms after Surgery across Studies", x = "Psychological Symptoms", y = "Prevalence Rates in Percentage")

有了这个知识,人们就可以真正知道哪个酒吧是哪个 Study。 Second Version with Text but on the plot instead of below xaxis

【问题讨论】:

    标签: ggplot2 geom-bar


    【解决方案1】:

    实现此目的的一种方法是使用构面。而不是按症状在 x 轴方面映射症状并将 study_ID 映射在 x 轴上。试试这个:

    data_g <- data.frame(
      study_ID = c("ben-amitay2006","ben-ari2018", "ben-ari2019a","ben-ari2019b","connolly2004","lopez2008","phelan2009","toren2007","ben-amitay2006","ben-ari2019b","connolly2004","ben-amitay2006",
                   "demaso2014","toren2007","ben-amitay2006","demaso2014","toren2007","demaso2014","ben-ari2018","ben-ari2019a","ben-ari2019b","kubota2011","sarrechia2015","ben-ari2018","ben-ari2019a",
                   "sarrechia2015"),
      symptom = c("PTSD","PTSD","PTSD","PTSD","PTSD","PTSD","PTSD","PTSD",
                  "Subthreshold PTSD","Subthreshold PTSD","Subthreshold PTSD","Depressive Symptoms",
                  "Depressive Symptoms","Depressive Symptoms","Anxiety Sympomts","Anxiety Sympomts","Anxiety Sympomts",
                  "Disruptive Behavior Sympomts","CBCL Clinical Range","CBCL Clinical Range","CBCL Clinical Range",
                  "CBCL Clinical Range","CBCL Clinical Range","CBCL Borderline Range","CBCL Borderline Range","CBCL Borderline Range"),
      prevalence = c(0,10.39, 33.3, 10.2, 12, 0,0,29.03,7.7,26.4, 12,5, 4,18.52,12.5, 5,16.13,
                     16, 11, 30, 27.3, 45, 8.1,4,26.7, 3.9)
    )
    
    data_g$symptom <- factor(data_g$symptom, levels = c('PTSD', 'Subthreshold PTSD', 'Depressive Symptoms', 'Anxiety Sympomts', 'Disruptive Behavior Sympomts', 'CBCL Clinical Range', 'CBCL Borderline Range'))
    
    library(RColorBrewer)
    library(ggplot2)
    
    ggplot(data_g, aes(study_ID, prevalence, fill = study_ID)) + 
      geom_bar(stat="identity", position = position_dodge(preserve = 'single')) +
      scale_fill_brewer(palette = "Paired") +
      labs(x = "", fill = "Study ID") +
      ylim(0, 100) +
      theme(text = element_text(size=15)) +
      scale_x_discrete(guide = guide_axis(n.dodge = 1))+
      labs(title="Prevalence Rates of Psychological Symptoms after Surgery across Studies", x = "Psychological Symptoms", y = "Prevalence Rates in Percentage") +
      facet_grid(.~symptom, scales = "free_x", switch = "x", space = "free_x") +
      theme(panel.spacing.x = unit(2, "pt"), 
            strip.placement = "outside", 
            axis.text.x = element_text(angle = 90, size = 6), 
            legend.position = "none",
            strip.text.x = element_text(size = 8))
    

    【讨论】:

      猜你喜欢
      • 2021-04-08
      • 1970-01-01
      • 1970-01-01
      • 2017-07-26
      • 2020-07-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-30
      • 1970-01-01
      相关资源
      最近更新 更多