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()
Any help much appreciated
2
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:
summarise
has an argument.by
,group_by
is no longer needed;- whenever you have both
x
andy
coordinates,geom_col
does whatgeom_bar
does but with no need forstat = "identity"
; - I have added a
breaks
argument since counts are integers, therefore, discrete.
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
1