ggplot2:x轴刻度之间的条形

时间:2019-04-16 12:04:35

标签: r ggplot2 axes

我需要可视化的数据包含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")

enter image description here

问题: 我想将x轴刻度线和桩号名称放置在这些条之间。目标是将条形图向右移动50%。

1 个答案:

答案 0 :(得分:2)

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

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

enter image description here

相关问题