简体   繁体   English

ggplot2:x轴刻度之间的条形

[英]ggplot2: Bars between x-axis ticks

The data I need to visualise contains passenger loads on a train between 4 stations. 我需要可视化的数据包含4个站点之间的火车上的乘客负载。 The data is provided in an ordered fashion, ie. 数据以有序方式提供,即。 between Station A and V there are 432 passengers on the train, between V and B 543 and so on. 在A站和V站之间,火车上有432位乘客,在V站和B站之间543,依此类推。 In the coding example I have ploted the data with ggplot2. 在编码示例中,我使用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")

在此处输入图片说明

The problem: I would like to position the x-axis ticks and Station names between the bars. 问题:我想将x轴刻度和测站名称放置在条之间。 The goal is to shift the bars 50% to the right. 目标是将条形向右移动50%。

We can use position_nudge to adjust the bars: 我们可以使用position_nudge来调整柱线:

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM