[R] Change default order of colors & line types

Harris Butler c10harris.butler at gmail.com
Sat Feb 2 19:35:23 CET 2013


You can change colors by manually creating your own palette.  You can
start with the Set1 color palette from RColorBrewer, then move the
values.  You then use this new palette in your plot:

library(RColorBrewer)

numGroups <-5 #here is where you define the size of your color palette
customPalette <- brewer.pal(numGroups, "Set1") #create your color palette
customPalette <- append(customPalette, customPalette[1], after=4)
#insert the first color into the correct place on the list
customPalette <- customPalette[2:len(customPalette)] #drop the first color

To use this in your plot, instead of using
scale_color_brewer(palette="Set1"), you would use
scale_color_manual(values=customPalette).

Linetype will need to be handled a slightly different way.  Remove
linetype=method from your initial qplot command, and then add a manual
linetype scale that would look something like this:

+ scale_linetype_manual(breaks=c("m1", "m2", "m3", "m4", "m5"),
values=c(2,3,4,1,5))

where "m1", "m2", etc are the method names that you are grouping
everything by.  You should keep your geom_line() command the same.
Note that the values in scale_linetype_manual() are rearranged to put
1 in the 4th position.  I'm sure there is a more elegant way to do it,
but this way should work as long as you aren't creating lots of plots
with changing method names.


--
Cheers,

Harris Butler


On Fri, Feb 1, 2013 at 1:41 PM, Soyeon Kim <soyeon.sophia.kim at gmail.com> wrote:
> Dear R users,
>
> I'd like to change the default order of colors & line types.
> Especially I am using ggplot2 and using color Set1.
> In Set1, the default color order is red, blue, green, violet,.. ect.
> However, I want to put red in fourth (not first).
> Likewise, I want to change the order of default linetype. I want to
> put "solid" line in fourth.
> How can I do thses?
>
> R code to draw the graph is
> qplot(variable, power, data = m.powers, colour = method,
> linetype=method,  ylab = "Power") + geom_line(aes(group = method),
> ylim = c(0,1)) +
>   scale_colour_brewer(palette="Set1")
>
> Thank you,
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list