关于 r:geom_bar 图与 position=\\”dodge\\” 和 geom_text 重复值 | 珊瑚贝

geom_bar plot with position=”dodge” and geom_text duplicating values


我查看了 SO,看到很多关于与条形相关的 geom_text 值位置的帖子,但我没有看到任何与我的问题相关的内容。对不起,如果我错过了。我正在尝试使用 position=”dodge” 在 ggpot2 中创建条形图,并且尝试在每个条形分组上方放置一个汇总值。我很接近,但是当我添加 geom_text 标签时,它会显示一堆值。理想情况下,我希望只删除所有值,但每个分组一个。我的可重现示例如下。提前感谢您提供的任何帮助!

1
2
3
4
5
6
7
gather.iris <- iris %>%
gather(key=flower_att, value=measurement, -Species) %>%
  mutate(sum_value=ifelse(Species==”setosa”, 5, ifelse(Species==”versicolor”, 7, 9)))

ggplot(data=gather.iris, aes(x=Species, y=measurement, fill=flower_att)) +
  geom_bar(stat=”identity”, position=”dodge”) +
  geom_text(aes(label=sum_value), vjust=-0.5, check_overlap=T)

enter1

  • 您希望标签的 y 值是多少?在 aes() 中,您指定了 y = measurement,因此您会为 y 上的 measurement 和 x 上的 Species 的每个唯一组合获得一个唯一标签。如果您不希望这样,请为 geom_text 图层指定不同的 y 值。通过”每个分组一个”,您想要每个 x 值(物种)一个,还是每个条形(物种:测量)一个?
  • 仔细看看 gather.iris。它有 600 行。通过这种方式绘制,您将条形图相互叠加
  • 感谢 Gregor 的快速回答。我不明白我需要为文本选择 x 和 y 值,就像我会绘图一样。这是对这个问题的一个不太好的答案。

    1
    2
    3
    4
    5
    6
    gather.iris <- iris %>% gather(key=flower_att, value=measurement, -Species) %>%
      mutate(sum_value=ifelse(Species==”setosa”, 5, ifelse(Species==”versicolor”, 7, 9)))

    ggplot(data=gather.iris, aes(x=Species, y=measurement, fill=flower_att)) +
      geom_bar(stat=”identity”, position=”dodge”) +
      geom_text(aes(y=as.numeric(Species), label=sum_value), vjust=-0.5, check_overlap=T)

    enter

  • 感谢 Gregor 的快速回答。我不明白我需要为文本选择 x 和 y 值,就像我会绘图一样。这是对这个问题的一个不太好的答案。

    1
    2
    3
    4
    5
    6
    gather.iris <- iris %>% gather(key=flower_att, value=measurement, -Species) %>%
      mutate(sum_value=ifelse(Species==”setosa”, 5, ifelse(Species==”versicolor”, 7, 9)))

    ggplot(data=gather.iris, aes(x=Species, y=measurement, fill=flower_att)) +
      geom_bar(stat=”identity”, position=”dodge”) +
      geom_text(aes(y=as.numeric(Species), label=sum_value), vjust=-0.5, check_overlap=T)

    enter



来源:https://www.codenong.com/50825273/

微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?