rggplot2

Flipped histogram with count going left rather than right


I need to graph a flipped histogram (i.e. count on x-axis, values on y) but with the count ascending to the left rather than the right

So the mirror image of this

iris %>%
  group_by(Sepal.Length) %>%
    summarise(count = n()) -> histBar

ggplot(data = histBar,
       mapping = aes(x = Sepal.Length,
                     y = count)) +
       geom_bar(stat = "identity") +
       coord_flip()

enter image description here

Any help much appreciated


Solution

  • If you reverse the axis sense the bars will still start at zero but now the zero is on the other side of the plot.
    Other changes I have made are:

    suppressPackageStartupMessages(library(tidyverse))
    
    iris %>%
      summarise(count = n(), .by = Sepal.Length) -> histBar
    
    ggplot(data = histBar,
           mapping = aes(x = Sepal.Length, y = count)) +
      geom_col() +
      coord_flip() +
      scale_y_reverse(breaks = 0:12)
    

    Created on 2024-09-07 with reprex v2.1.0


    Off-topic?

    A plot going down just for fun.

    iris %>%
      summarise(count = n(), .by = Sepal.Length) %>%
      ggplot(aes(x = Sepal.Length, y = count)) +
      geom_col() +
      scale_y_reverse(breaks = 0:12)
    

    Created on 2024-09-07 with reprex v2.1.0

    And before the OP edit, the plot was a histogram.

    ggplot(iris, mapping = aes(y = Sepal.Length)) +
      geom_histogram(bins = 30) +
      scale_x_reverse(breaks = 0:12)
    

    Created on 2024-09-07 with reprex v2.1.0