Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?)

like image 877
fridde Avatar asked Jan 28 '14 10:01

fridde


People also ask

How do I change the order of items in legend ggplot2?

You can use the following syntax to change the order of the items in a ggplot2 legend: scale_fill_discrete(breaks=c('item4', 'item2', 'item1', 'item3', ...) The following example shows how to use this syntax in practice.

How do I change the legend position in R?

You can place the legend literally anywhere. To put it around the chart, use the legend. position option and specify top , right , bottom , or left . To put it inside the plot area, specify a vector of length 2, both values going between 0 and 1 and giving the x and y coordinates.

How do you reverse the order of legend in R?

R. To Reverse the order of Legend, we have to add guides() and guide_legend() functions to the geom_point() function. Inside guides() function, we take the parameter color, which will call guide_legend() guide function as value.


1 Answers

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)))
like image 184
Prasanna Nandakumar Avatar answered Sep 22 '22 01:09

Prasanna Nandakumar