【问题标题】:ggplot: How to add a group average value as a line to a grouped bar chart in R?ggplot:如何将组平均值作为一条线添加到R中的分组条形图中?
【发布时间】:2020-05-12 23:38:57
【问题描述】:

我想将“All_Parties_average”值作为带有图例的绿线添加到我的条形图中(请参见通过链接添加的第二张图片)。非常感谢您的帮助!

现在我的图表如下所示:

My graph at the moment

这是我想要的样子:

End result

这是我的数据:

Gender <- as.factor(c("M","M","M","M","M","M","M","M","M","M","F","F","F","F","F","F","F","F","F","F"))
Political_Party <- as.factor(c("E200", "EKRE", "ERE", "Isamaa", "REF","Roh", "SDE", "Vabae", "Vasak", "KE","E200", "EKRE", "ERE", "Isamaa", "REF","Roh", "SDE", "Vabae", "Vasak", "KE"))
Media_coverage_average <- c(61,95,41,162,107,23,130,68,6,152,99,49,9,87,157,52,112,31,14,202)
All_Parties_average <- c(90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,90)

my_data <- data.frame(Sugu, Political_Party, Media_coverage_average, All_Parties_average, stringsAsFactors=F)  

还有图表的颜色:

my_colors <- c(
  `Green` <- "#00a99d",
  `Purple` <- "#9424fb",
  `Grey` <- "#a6a6a6",
  `LightPurple` <- "#c991fd",
  `LightGrey` <- "#c9c9c9",
  `DarkGrey`<- "#636363",
  `ExtraDarkGrey`<- "#4c4c4c")

还有我的图表:

my_plot <- ggplot(my_data, aes(x = Political_Party, y = Media_coverage_average, fill = Gender))+
  geom_bar(position="dodge", stat="identity")+
  geom_text(aes(label=Media_coverage_average), position=position_dodge(width=0.9), vjust=-0.25, size=6, family="Titillium Web", color= ExtraDarkGrey)+
  labs(x="Political Party", y="Average media coverage",
       caption = "Author")+
  theme_ipsum_tw(grid=FALSE, axis_text_size=18, axis_title_size = 16, caption_size = 15, base_size = 17, subtitle_size = 20)+
  scale_fill_manual(values = c(Grey, Purple), labels = c("Mehed", "Naised"))

my_plot

【问题讨论】:

    标签: r ggplot2 graph line bar-chart


    【解决方案1】:

    除了@Senzeybek 的回答,您可以通过将绿色线的颜色传递到geom_line 的aes 并使用scale_color_manual 设置颜色来添加绿色线的图例:

    library(ggplot2)
    ggplot(my_data, aes(x = Political_Party, y = Media_coverage_average, fill = Gender))+
      geom_bar(position="dodge", stat="identity")+
      geom_text(aes(label=Media_coverage_average), position=position_dodge(width=0.9), vjust=-0.25, size=6, family="Titillium Web", color= ExtraDarkGrey)+
      labs(x="Political Party", y="Average media coverage",
           caption = "Author")+
      #theme_ipsum_tw(grid=FALSE, axis_text_size=18, axis_title_size = 16, caption_size = 15, base_size = 17, subtitle_size = 20)+
      scale_fill_manual(values = c(Grey, Purple), labels = c("Mehed", "Naised"))+
      geom_hline(aes(yintercept = All_Parties_average, color = "Average", group = 1), size = 2)+
      scale_color_manual(values = "green")
    

    【讨论】:

    • 谢谢你。是否可以使线在边缘开始和结束?现在它会在边缘留下两列吗?并且还想删除图例名称颜色?再次感谢!!
    • 我相应地编辑了我的答案。简而言之,您可以将geom_line 替换为geom_hline,并将aes 中的y 替换为yintercept。
    【解决方案2】:

    也许你可以用 geom_line 添加它

    my_plot <- ggplot(my_data, aes(x = Political_Party, y = Media_coverage_average, fill = Gender))+
      geom_bar(position="dodge", stat="identity")+
      geom_text(aes(label=Media_coverage_average), position=position_dodge(width=0.9), vjust=-0.25, size=6, family="Titillium Web", color= ExtraDarkGrey)+
      labs(x="Political Party", y="Average media coverage",
           caption = "Author")+
      scale_fill_manual(values = c(Grey, Purple), labels = c("Mehed", "Naised"))+
      theme_ipsum_tw(grid=FALSE, axis_text_size=18, axis_title_size = 16, caption_size = 15, base_size = 17, subtitle_size = 20)+
        geom_line(aes(y=All_Parties_average,group=1),size=1.5,color="green")   
    

    【讨论】:

      猜你喜欢
      • 2023-01-27
      • 2018-11-25
      • 2023-04-03
      • 2020-10-23
      • 1970-01-01
      • 2020-12-02
      • 2022-01-12
      • 2020-12-31
      • 2021-02-20
      相关资源
      最近更新 更多