13

I have noticed that the default for filling the bars in a histogram created using plot is the reverse alphabetical, while the legend in ordered alphabetically. I there any way to get both to order alphabetically? Problem is apparent in example plot below. Bonus question: how I change the left to right bar order from alphabetical to decreasing count total? Thanks

df <- data.frame(
  Site=c("A05","R17","R01","A05","R17","R01"),
  Group=c("Fungia","Fungia","Acro","Acro","Porites","Porites"),
  Count=c(6,8,6,7,2,9),
  Total=c(13,10,15,13,10,15)
)

  Site   Group Count Total
1  A05  Fungia     6    13
2  R17  Fungia     8    10
3  R01    Acro     6    15
4  A05    Acro     7    13
5  R17 Porites     2    10
6  R01 Porites     9    15

qplot(df$Site,data=df,weight=df$Count,geom="histogram", fill=df$Group, ylim = c(0,16)) + 
  xlab("Sites") + 
  ylab("Counts") + 
  scale_fill_hue(h=c(0,360), l=70, c=70,name = "Emergent Groups")

enter image description here

I am trying to order the counts from high to low and the fill colours so that they match the alphabetic ordering of the legend. I have been trying to adjust it for a few hours with the tips from the initial posts but without sucess. Any help on this would be most appreciated!!!

Andrie
  • 176,377
  • 47
  • 447
  • 496
Elizabeth
  • 6,391
  • 17
  • 62
  • 90
  • Maybe [this question](http://stackoverflow.com/questions/8186436/order-stacked-bar-graph-in-ggplot) helps? – ROLO Jul 23 '12 at 14:38
  • 1
    In addition to ROLO's suggestion see [this question](http://stackoverflow.com/questions/5208679/order-bars-in-ggplot2-bar-graph) and R cookbook's [legend information](http://wiki.stdout.org/rcookbook/Graphs/Legends%20(ggplot2)/). Together you chould be able to do what you want. – Tyler Rinker Jul 23 '12 at 14:47
  • Thanks guys. I updated the question with the actual data and code. Any ideas? I just can't seem to get it to order in the way I want. – Elizabeth Jul 23 '12 at 18:43

1 Answers1

18

If you just want the color order match, you can just reverse the legend: the color orders will match, but the legend will be in reverse alphabetical order:

qplot(df$Site,data=df,weight=df$Count,geom="histogram", fill=df$Group, ylim = c(0,16)) + 
  xlab("Sites") + 
  ylab("Counts") + 
  scale_fill_hue(h=c(0,360), l=70, c=70,name = "Emergent Groups") +
  guides(fill = guide_legend(reverse = TRUE))

enter image description here

To get the alphabetical order back, precede the above code by a reordering of the Group factor:

# reorder the groups
df$Group <- factor(df$Group , 
                   levels=levels(df$Group)[order(levels(df$Group), decreasing = TRUE)])

qplot(df$Site,data=df,weight=df$Count,geom="histogram", fill=df$Group, ylim = c(0,16)) + 
  xlab("Sites") + 
  ylab("Counts") + 
  scale_fill_hue(h=c(0,360), l=70, c=70,name = "Emergent Groups") +
  guides(fill = guide_legend(reverse = TRUE))

enter image description here

For the bonus (ordering the bars by decreasing total count), reorder the factor order of the Site variable:

# reorder the sites
df$Site <- factor(df$Site, 
                  levels = levels(df$Site)[order(aggregate(Count ~ Site, data = df, sum)$Count, 
                                                 decreasing = TRUE)])
# reorder the groups
df$Group <- factor(df$Group , 
                   levels=levels(df$Group)[order(levels(df$Group), decreasing = TRUE)])

qplot(df$Site,data=df,weight=df$Count,geom="histogram", fill=df$Group, ylim = c(0,16)) + 
  xlab("Sites") + 
  ylab("Counts") + 
  scale_fill_hue(h=c(0,360), l=70, c=70,name = "Emergent Groups") +
  guides(fill = guide_legend(reverse = TRUE))

enter image description here

nassimhddd
  • 8,340
  • 1
  • 29
  • 44
  • Your first line of code alters the data in a way that seems dangerous and unintended. Check `df` both before and after running `levels(df$Group) <- ...` to see what I mean. – bdemarest Jul 25 '12 at 18:40
  • Nice solution with reversing the guide. But is it really necessary to change the levels of the data? It seems to work without this step. – Andrie Jul 25 '12 at 21:04
  • @Andrie: I have decomposed the steps so we can see what each does. I didn't find any way to reorder the positions in the actual chart without reordering the factors... – nassimhddd Jul 26 '12 at 07:55
  • @bdemarest Thank you for your step by step answer. It was very helpful. What was tripping me up was that is creating my initial data.frame I had included stringsAsFactors=FALSE which of course was causing problems. Thanks again! – Elizabeth Jul 26 '12 at 09:58
  • @bdemarest On a side note, do you know of any particularly pretty adjustment to the fill colour? The current colour scheme is rather gross. I know it involves adjusting the parameters in: scale_fill_hue(h=c(0,360), l=70, c=70,name = "Emergent Groups") but have yet to find a parameter set that results in an attractive combination of colours – Elizabeth Jul 26 '12 at 10:10
  • 1
    @Elizabeth To get other colors just add something like: + scale_fill_brewer(palette="Set1") or: + scale_fill_brewer(palette="Spectral") – nassimhddd Jul 26 '12 at 10:20