ggplot2: Order in legend alphabetically instead of following the order of appearance in the dataset

12,502

Solution 1

apply breaks in both scales(scale_color_manual and scale_shape_manual) . If we did just one, they wouldn't match, and ggplot would split them into two legends, rather than merging them

One such example is:

> library(ggplot2)
> ggplot(mtcars, aes(wt, mpg, shape=factor(cyl))) + geom_point() + theme_bw()
> library(ggplot2)
> bp <- ggplot(data=PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot()
> bp

enter image description here

bp + scale_fill_discrete(breaks=c("trt1","ctrl","trt2"))

enter image description here

To reverse

bp + scale_fill_discrete(breaks = rev(levels(PlantGrowth$group)))

also try

bp + scale_fill_discrete(breaks = unique(levels(PlantGrowth$group)))

Solution 2

To address the ordering: You probably want to fix the factor levels of dt$Compilation by calling sth like dt <- transform(dt, Compilation=factor(Compilation, levels=unique(Compilation)))before plotting.

And speaking of horizontal lines: Do you want p = p + guides(size=FALSE)?

Share:
12,502
fridde
Author by

fridde

Updated on June 24, 2022

Comments

  • fridde
    fridde almost 2 years

    I am producing a ggplot2 line plot from AESRD 2013 - SCO Bitumen - 7y.csv in this folder. The file is automatically created on a website according to my wishes. These are several time series showing some production values, each named according to the column "Compilation". So I grouped according to "Compilation".

    See this excerpt of my code in the file plotter.r available in the same folder (see above).

    # "dt" is the dataframe derived from the csv file. 
    # "thinned" is some vector of x-values that tells where to draw the special symbols.
    
    p = ggplot(dt, aes(Date, Value, colour= Compilation, group = Compilation, size = plotParameter), guide=FALSE)
    p = p + geom_point(data=dt[thinned,],aes(as.Date(Date), Value, colour= Compilation, shape = Compilation), size = 5)
    p = p + scale_shape_manual(values = seq(0,20))
    p = p + geom_line(guide = FALSE)
    p = p + scale_colour_manual(values=cbPalette) #cbPalette is already defined
    p = p + scale_size(range=c(0.5, 2), guide=FALSE)
    p = p + scale_y_continuous(labels = comma)
    p = p + ylab("Barrels per day") + xlab("")
    p = p + theme(legend.text = element_text(size = 8, hjust = 5, vjust= -5)) 
    plot(p)
    

    Here comes the nasty thing: The legend reorders my compilations alphabetically! enter image description here

    I have purposely designed my csv-file so that each compilation shows up in a certain logical order (the most important series first, then following in order of some performance parameter). So the right order of the legend would simply be according to unique(dt$Compilation).

    My steps until now have been to introduce the column Order into the csv-file and experiment with that (unsuccessfully), and to change my code in all kinds of ways. With no success.

    Of course, I have googled and checked most available threads on Stackoverflow. I have encountered factorization and reordering, but there is no "logical" order for my compilations except for the order they appear in the dataset. *Sigh*

    Can anyone point me on where to insert what?

    (Bonus point: How do I get rid of those horizontal lines in the symbol legend?)

  • Prasanna Nandakumar
    Prasanna Nandakumar about 10 years
    also explore bp+ opts(legend.key = ) for legend themes
  • a different ben
    a different ben about 10 years
    For order of appearance of items in group, the last line here should read: bp + scale_fill_discrete(breaks = unique(PlantGrowth$group))?