I am building a plot and totally happy with the default color order defined by ggplot, for example:
library(ggplot2)
data(mtcars)
mtcars$brand <- gsub(' .*','',row.names(mtcars))
ggplot(mtcars,aes(x=mpg, y=hp, col=brand)) +
geom_point()
Now imagine I want to change the order of colors in the legend (in my example, according to the frequency of levels - but could be any other custom order). The only way I know is to manually define the order of levels:
df.freq <- as.data.frame(table(mtcars$brand))
neworder <- order(-df.freq$Freq)
mtcars$brand <- factor(mtcars$brand, levels=df.freq$Var1[neworder])
It makes the desired order but changes the colors as well.
What if I absolutely want to keep the original colors?
For now I only came to an awkward workaround with manually re-creating the default palette, then reordering it and feeding these colors to scale_color_manual()
:
gg_color_hue <- function(n) { # code from https://stackoverflow.com/questions/8197559/
hues = seq(15, 375, length = n + 1)
hcl(h = hues, l = 65, c = 100)[1:n]
}
newcolors <- gg_color_hue(length(levels(mtcars$brand)))
newcolors <- newcolors[neworder]
ggplot(mtcars,aes(x=mpg, y=hp, col=brand)) +
geom_point()+
scale_color_manual(values = newcolors)
It does the job but I'm wondering if there's any cleaner way...