【问题标题】:ggplot2: Bars between x-axis ticksggplot2:x轴刻度之间的条
【发布时间】:2019-09-06 12:44:16
【问题描述】:

我需要可视化的数据包含火车上 4 个车站之间的载客量。数据以有序的方式提供,即。 A站和V站之间有432名乘客,V和B站之间有543人,以此类推。在编码示例中,我使用 ggplot2 绘制了数据。

library(tidyverse)

df <- tribble(~Station, ~Passengers,
              "A", 432,
              "V", 543,
              "B", 435,
              "Q", 0)

df$Station <- factor(df$Station, levels=unique(df$Station))

ggplot(data = df, aes(x = Station, y = Passengers)) + 
  geom_bar(stat = "identity")

问题: 我想在条之间定位 x 轴刻度和站名。目标是将条形向右移动 50%。

【问题讨论】:

标签: r ggplot2 axes


【解决方案1】:

我们可以使用position_nudge来调整条形:

ggplot(data = df, aes(x = Station, y = Passengers)) + 
  geom_bar(stat = "identity", position = position_nudge(x = 0.5))

【讨论】:

  • 我知道答案会短得令人尴尬!谢谢
  • 你已经完成了最困难的部分——让你的因素以正确的顺序排列,包括“缺失”级别。
  • 有没有办法组合定位命令:position_nudge 和 position_stack?我没有发现任何有用的东西。我可能应该提交一个新问题。
  • 是的,听起来像是一个新问题。我不知道该怎么做。