1

I am trying to plot a bar plot with study names on the x-axis and a total score on some measurement on the y-axis. In order to make the graph more readable, I flipped the coordinate systems so that the study names are presented on the left now. However, now I can't change the order of the bars anymore. I want it to be order in alphabetical order going from a-z rather than the other way around (which is the case in my plot).

As you can see in the picture below, the bars start with Wright (2018) and end with Alford (2012). I would like to have it the other way around.

enter image description here The code looks as follows:

 data.quality %>%
  ggplot(aes(x = study, y = score)) + 
  geom_bar(stat = "identity", fill = "#66c2a5") +
  coord_flip() +
  scale_y_continuous(name = "Total quality score", limits = c(0, 7),
                     breaks = 0:7) +
  scale_x_discrete(name = element_blank()) +
  theme(axis.ticks.y = element_blank())

I have already tried to use reorder():

ggplot(aes(x = study, y = reorder(score, -study)))

But that doesn't work because study is a string.

Thank you, Matthias

Matthias
  • 171
  • 11
  • 1
    Can you upload some reproducible code? See here:https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610. In short, try `dput(data.quality)` and upload the output; it should look something like `structure()` – TheSciGuy May 15 '19 at 15:03

1 Answers1

1

Have you tried just turning study into a factor and reversing its order before plotting it? I don't have a reproducible example to test this out but it might work, since coord_flip reverses the (now reversed) order.

library(tidyverse)

data.quality %>%
  mutate(study = fct_rev(study)) %>% 
  ggplot(aes(x = study, y = score)) + 
  geom_bar(stat = "identity", fill = "#66c2a5") +
  coord_flip() +
  scale_y_continuous(name = "Total quality score", limits = c(0, 7),
                     breaks = 0:7) +
  scale_x_discrete(name = element_blank()) +
  theme(axis.ticks.y = element_blank())
Benjamin
  • 876
  • 4
  • 8