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!
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?)
To address the ordering: You probably want to fix the factor levels of
dt$Compilation
by calling sth likedt <- transform(dt, Compilation=factor(Compilation, levels=unique(Compilation)))
before plotting.And speaking of horizontal lines: Do you want
p = p + guides(size=FALSE)
?apply
breaks
in both scales(scale_color_manual
andscale_shape_manual
) . If we did just one, they wouldn't match, and ggplot would split them into two legends, rather than merging themOne such example is:
To reverse
also try