I'd like to ra-arrange the bars of my ggplot bar chart - and there is quite a number of similar entries here on stackoverflow (e.g. here).
However, the question I have is: can you do this with only one variable (the one used for the bar graph) by telling ggplot not to sort alphabetically by labels but sort by take the count of identical labels as the value of interest.
In my case, I have survey data on the question of which political party champions a certain issue/ is the most competent in a given issue area.
respondent-id competence
1 "Party A"
2 "Party A"
3 "Party B"
4 "Party B"
5 "Party B"
6 "Party C"
What ggplot would do now is a bar chart with the 2nd-highest first (party A), highest second (party B) and lowest last (party C). But how do I tell ggplot to take the count into account (2:3:1 --> place party B first)?
I tried several ways as suggested here, but that didn't solve the problem: most of them included a position-variable which would tell ggplot "assign party B first place". I also tried to reorder()
simply by "competence", with no success. Finally, I could assign different prefixes to the parties ("1_party_B", "2_...") but that would really tedious work.
ggplot(MyData, aes(x=competence,y=(..count..))) + geom_bar()
Also, I have a NA-bar in my bar chart, and MyData[,c("competence")]
doesn't seem to do the trick. But that's another story.
Thanks in advance!
table()
– Sowmya S. Manian