rggplot2axes

ggplot2: Bars between x-axis ticks


The data I need to visualise contains passenger loads on a train between 4 stations. 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. In the coding example I have ploted the data with 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

The problem: I would like to position the x-axis ticks and Station names between the bars. The goal is to shift the bars 50% to the right.


Solution

  • We can use position_nudge to adjust the bars:

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

    enter image description here