1

I am trying to create a bar graph that plots rank, where lower values are better. I want larger bars to correspond to smaller values, so the "best" groups in the data receive more visual weight.

Reprex:

dat = data.frame("Group" = c(rep("Best",50), rep("Middle",50), rep("Worst",50) ), "Rank" = c(rnorm(n = 50, mean = 1.5, sd = 0.5), rnorm(n = 50, mean = 2.5, sd = 0.5), rnorm(n = 50, mean = 3.5, sd = 0.5) ) ) tibdat = as_tibble(dat) %>% group_by(Group) %>% summarise(Mean_Rank = mean(Rank,na.rm=T)) # creates simple rightside up bar graph ggplot(data = tibdat, mapping = aes(Group, Mean_Rank, fill = Group)) + geom_col() + scale_y_continuous(breaks = c(1:4), limits = c(1,4), oob = scales::squish) # my attempt below, simply reversing the breaks and limits ggplot(data = tibdat, mapping = aes(Group, Mean_Rank, fill = Group)) + geom_col() + scale_y_continuous(breaks = c(4:1), limits = c(4,1), oob = scales::squish) 

The graphing code at the end does succeed in flipping the axis, but the data disappears (the bars are not plotted).

Note that I do not want the graphs to originate from the top, which scale_y_reverse can achieve. I want the bars to originate from the bottom, at the y = 4 line (or below).

How is this achieved?

Edit: Added image below to show the original bar graph that works but is wrong.

Graph that works but is not ideal

1 Answer 1

1

I just transformed the labels. I don't know if that's what you searched.

ggplot(data = tibdat, mapping = aes(Group, Mean_Rank, fill = Group)) + geom_col() + scale_y_continuous(breaks = c(1:4), limits = c(1,4), oob = scales::squish, labels = function(x) 5 - x) 

With another trick in the aes argument I think you can arrive to the wanted result. Maybe someone better than me knows a clean way to do it.

ggplot(data = tibdat, mapping = aes(Group, 5 - Mean_Rank, fill = Group)) + geom_col() + scale_y_continuous(breaks = c(1:4), limits = c(1,4), oob = scales::squish, labels = function(x) 5 - x) 

And here is the result : plot_from_4

Sign up to request clarification or add additional context in comments.

3 Comments

This still leave the Worst group with the largest bar. I'm trying to make the bar for Best group the largest, spanning from 4 (at bottom) to 1.5 (near top). The Worst bar should be smallest, spanning from 4 to 3.5.
Oh ok I misunderstood a little. You can go with 5 - Mean_Rank in aes. I know it's kind of a chead trick but it seems to work. I edit my answer. Let me know if that's what you searched.
Yes this does work and will serve my purpose, thanks for your help! However it does make me uneasy to change the data itself rather than the axis/graph details.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.