2

I'm trying to visualize the different backgrunds of our students, the bars represent different high school programs. I write:

ggplot(test, aes(x=fct_infreq(gymnasiegrov))) + geom_bar()

And I get:

enter image description here

As you can see the names get very cluttered, so I write:

ggplot(test, aes(x=fct_infreq(gymnasiegrov))) + geom_bar()+coord_flip()

And get:

enter image description here

This "does" look much better, but for optimal effect I would like to show the frequencies in descending order from the top, like my first plot but rotated clockwise.

Is there any way I can make this happen?

Magnus
  • 728
  • 4
  • 17

2 Answers2

3

I think you can use scale_x_discrete(limits = rev(levels(test$fct_infreq(gymnasiegrov)))).

Where test is your data frame and fct_infreq(gymnasiegrov) is your discrete variable.

Please let me know if it worked :)

Edit: To be more clear add it to ggplot(test, aes(x=fct_infreq(gymnasiegrov))) + geom_bar()+coord_flip() so that you get ggplot(test, aes(x=fct_infreq(gymnasiegrov))) + geom_bar()+coord_flip() + scale_x_discrete(limits = rev(levels(test$fct_infreq(gymnasiegrov))))

Elias
  • 726
  • 8
  • 20
  • 1
    I write ggplot(test, aes(x=rev(fct_infreq(gymnasiegrov)))) + geom_bar()+coord_flip()+scale_x_discrete(limits = rev(levels(test$fct_infreq(gymnasiegrov)))) and the program tells me "Error in levels(test$fct_infreq(gymnasiegrov)) : attempt to apply non-function" – Magnus Oct 31 '19 at 12:53
  • 2
    If this returns an error, you could try `scale_x_discrete(limits = rev(levels(fct_infreq(test$gymnasiegrov))))` – teunbrand Oct 31 '19 at 12:54
0

Use ggplot(test, aes(x=reoder(fct_infreq(gymnasiegrov)),Count)) + geom_bar()+coord_flip()

kevinn-12
  • 13
  • 5