Search code examples
rggplot2geom-bar

Why do geom_bar bars come from the y-axis and not the x-axis?


enter image description hereI'm trying to plot some data as bars that come up from the x-axis, 'bp'. However, when I plot it the bars come from the y-axis, 'peak'. I can't for the life of me work out why. I've tried plotting the x-axis as categorical, but I need it to be continuous really. Any help much appreciated!

[![data <- data.frame(bp = c(45.1621698951309, 73.2304175051575, 100.434911611335, 126.858745160524, 
                          153.199472907515, 203.650668791208, 253.410774660789, 301.905437534337, 
                          349.97680634797, 398.748046286491, 447.533443047225, 471.148733099021, 
                          496.042935526083, 546.27225636133, 597.401025980147, 649.543985518631, 
                          701.786235475666, 754.483148227243, 805.857327616729, 855.819159304795, 
                          903.837494815388, 948.675184357849, 990.021619969437),
                   peak = c(758, 759, 768, 822, 793, 761, 768, 756, 724, 689, 657, 685, 
                            687, 680, 696, 736, 767, 779, 834, 866, 917, 951, 816))

ggplot(data, aes(x=bp, y=peak)) +
  geom_point() +
  geom_bar(stat="identity")

Solution

  • I think you're looking for orientation = "x":

    ggplot(data, aes(x=bp, y=peak)) +
      geom_point() +
      geom_bar(stat = "identity", orientation = "x")
    

    enter image description here

    Incidentally, geom_bar(stat = "identity") is just a long way of writing geom_col()