Sam Hoppen
Sam Hoppen

Reputation: 365

geom_col to show a ranking with the lowest number as the tallest bar

I have a dataset of rankings of different items where I want the lowest value to show the tallest bar on the chart. Below is a small sample of the data and the code to build the chart, followed what the chart currently looks like.

data <- data.frame(ranks = c("rank1","rank2", "rank3") , value = c(13,2,18))

ggplot(data) + 
  geom_col(aes(x = ranks, y = value))

enter image description here

What I'd like the chart to look like is something like this (using the same data) with the y-axis reversed: enter image description here

Upvotes: 0

Views: 141

Answers (1)

teunbrand
teunbrand

Reputation: 38063

If you can tolerate a warning, you can set ymax = after_stat(-Inf). This trick is abusing the fact that the columns are parameterised as rectangles, and is not the recommended way to use ggplot2. You can set it to -1 * desired_lowest_point too, but the scale doesn't automatically adjust the limit.

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.0.5
data <- data.frame(ranks = c("rank1","rank2", "rank3") , value = c(13,2,18))

ggplot(data) + 
  geom_col(aes(x = ranks, y = value, 
               ymax = after_scale(-Inf))) +
  scale_y_reverse()
#> Warning: Ignoring unknown aesthetics: ymax

Created on 2021-04-22 by the reprex package (v1.0.0)

Upvotes: 1

Related Questions