0

Last July, Alastair asked a well-framed question about ordering the legends in ggplot2.

Controlling ggplot2 legend display order

I have a related but distinct question about adjusting the order of a legend comprised of lines created with stat_summary. In other words, I am not currently on comparing values drawn from a single variable or two variables as was the case in previous examples. Rather, I am graphing calculated means.

At present the legend generated is in alphabetical order: Any Source, Classmate, Major, and so on. I would like to reorder the legend so that the items are ordered based on decreasing the intercept values in the hopes of making the graph easier to read. The preferred order would be: Any Source, Classmate, Professor, Stu. Worker @ Support Center, Teaching Assistant, Staff, Major, Other Faculty Member.

pmf0 <- ggplot(All2011QsGPASource.long, aes(x=ClassYear)) 
pmf1 <- pmf0 + stat_summary(aes(y=FamSuppAny,colour="Any Source"),fun.y=mean,geom="line", lwd=2)
pmf2 <- pmf1 + stat_summary(aes(y=FamClassmAny,colour="Classmate"),fun.y=mean,geom="line", lwd=2)
pmf3 <- pmf2 + stat_summary(aes(y=FamProfAny,colour="Professor"),fun.y=mean,geom="line", lwd=2)
pmf4 <- pmf3 + stat_summary(aes(y=FamStuWkrAny,colour="Stu. Worker @ Support Center"),fun.y=mean,geom="line", lwd=2)
pmf5 <- pmf4 + stat_summary(aes(y=FamTAAny,colour="Teaching Assistant"),fun.y=mean,geom="line", lwd=2)
pmf6 <- pmf5 + stat_summary(aes(y=FamStaffAny,colour="Staff"),fun.y=mean,geom="line", lwd=2)
pmf7 <- pmf6 + stat_summary(aes(y=FamMajorAny,colour="Major"),fun.y=mean,geom="line", lwd=2)
pmf8 <- pmf7 + stat_summary(aes(y=FamOthFacAny,colour="Other Faculty Member"),fun.y=mean,geom="line", lwd=2)
pmf9 <- pmf8 + theme(aspect.ratio=1) + ylab("Percentage") + xlab("Year Survey Taken") 
pmf10 <- pmf9 + scale_x_continuous(breaks=c(1,2,3,4),labels=c("First Year","Sophomore","Junior","Senior")) + xlab("Class Year") + ylab("Percentage") + scale_y_continuous(labels=percent)
pmf11 <- pmf10 + scale_colour_discrete("Specific Source of Support") + labs(title="Sources of Support Over Time - Familiar Assignment")
print(pmf11)

I am currently using ggplot2 0.9.3.1, R version 3.0.1, in Mac OS X 10.7.5 and am grateful to @Kohske Takahashi for his impressive work on guide_legend. At present I can see an option for reversing the existing order of a legend but am hoping to reorder the legend in a different manner.

Thank you in advance for your help!

Andrea

Community
  • 1
  • 1
  • Good lord, why have you used so many `stat_summary` calls, rather than summarizing your data outside of ggplot and using a variable to represent the different colors? – joran Jul 24 '13 at 21:06
  • At this point it seems easiest to just reorder the legend if that is possible. But, sure, another approach would be to do the calculations outside of ggplot and generate an alternative data frame. That is likely what I would do were reordering not an option. – A Nixon Jul 24 '13 at 21:30
  • Well, the reason I'm confused is that (1) the question you linked to is about ordering _multiple distinct_ legends, not the levels within one legend, and (2) the way you control legend order is by encoding that information in a factor and altering the level order there. – joran Jul 24 '13 at 21:36
  • That is one way to do it. I was trying to tie the conversation to previous discussions that appeared to lead to the new guide_legend feature in the new version of ggplot and am hopeful that someone more familiar with that work can give me a sense of whether it is possible to reorder the legend using that approach. Thanks! – A Nixon Jul 24 '13 at 22:17
  • I don't think you can, since it needs to be specified only once per aesthetic, in your case color. I _am_ trying to help you here, because regardless of how you _want_ to do this, you simply shouldn't be taking this approach. It will be much better to summarise your data outside of ggplot and simply use factor levels to control the ordering. (I'd be happy to help more with an actual answer, but you have failed to provide a reproducible example; I don't have access to your data frame.) – joran Jul 24 '13 at 22:26

0 Answers0