0

I have the following data frame:

Author<-c("University","Office", "School","University","Office", "School","University","Office", "School")
Typ<-c("Text", "Text", "Text","Data", "Data","Data",  "List", "List", "List")
Number<-c("3","1","6","4","4","2","8","1","1")
df<-data.frame(Typ,Author,Number)

If I apply:

ggplot(df, aes(x=Author, y=Number, fill=Typ)) +
  geom_bar(stat='identity') + coord_flip()

then I get a stacked bar plot where the bars are orders in the order of the date frame, i.e. Text, Data, List, but the legend is in alphabethic order. Is there any (non brute force, ie. by hand) option such that I can rearrange the legend also in the "given" order of the df, i.e. in Text, Data, List?

(just to clarify - I have a bunch of data frames like that which are also bigger in the sense that the vectors "Typ" (which are also different in each data frame) have more entries whose order should not be changed and also displayed in the legend. I wrote a routine which plots all these data frames so I cannot change the legends manually - I am really looking for a routine friendly solution)

chris17
  • 27
  • 9
  • 1
    The legend is not necessarily ordered alphabetically, but it's ordered by the factor levels: `df$Typ`. One way to rearrange the legend is changing the order of the factor levels, e.g. `df$Typ <- factor(df$Typ, levels = rev(levels(df$Typ)))`. – lukeA Aug 04 '15 at 11:12
  • if you do not want to change directly your data, you can try ` ggplot(df, aes(x=Author,y=Number,fill=factor(Typ, levels=c('Text', 'Data', 'List')) )) + geom_bar(stat='identity') + coord_flip() ggplot(df, aes(x=Author,y=Number,fill=factor(Typ, levels=c('Text', 'Data', 'List')) + )) + geom_bar(stat='identity') + coord_flip()` – Mamoun Benghezal Aug 04 '15 at 11:16
  • Hi! Thanks for your answers - no, my real data frame is much bigger (much more "typ"s so manual setting of the levels is unfortunately no option. Unfortunately also the first approach changes the order of typ, which is not what I need. – chris17 Aug 04 '15 at 11:20
  • possible duplicate of [ggplot legends - change labels, order and title](http://stackoverflow.com/questions/12075037/ggplot-legends-change-labels-order-and-title) – Jaap Aug 04 '15 at 11:22
  • unfortunateyl also here I do not see how to apply this to my problem. Thanks nonetheless! – chris17 Aug 04 '15 at 11:51

1 Answers1

4

You could automatically set your levels according to the order how they appear in your data.frame:

df$Typ <- factor(df$Typ, levels = unique(df$Typ))
ggplot(df, aes(x=Author, y=Number, fill=Typ)) +
        geom_bar(stat='identity') + coord_flip()

In this way you change the order of your factor according to the order in df$Typ:

enter image description here

thothal
  • 16,690
  • 3
  • 36
  • 71
  • THAT'S IT! Thanks so much! – chris17 Aug 04 '15 at 12:00
  • @chris17 please mark the question as answered when you're satisfied with the given answer (you can do this by clicking the checkmark under the answer's vote counts). This stops people spending time on answering a question that has already been answered. – Heroka Aug 04 '15 at 12:31