r - Assigning Individual Colors to a Gradient Color Scheme in ggplot2 -
i'm trying find best way display stacked area chart in ggplot2 in r. issue amount of variables i'm trying display while keeping chart readable. i've got 17 different variables need keep present in chart have few in particular i'd single out. code stands this:
full <- ggplot(data=df, aes(x=year, y=value) + geom_area(aes(fill=variable)) + theme(legend.position='right') + theme(legend.title=element_blank())
i thought add achieve effect of giving entire graph gradient theme singling out couple variables separate fill color entirely.
full + scale_fill_grey() + scale_fill_manual(values=c('xval1'='red', 'xval2'='blue'))
if knows how i'd extremely grateful. alternately, if can think of better way entirely display data open hearing suggestions.
since not give example of data, impossible give detailed advice. can show in example how can achieve goal (if correctly understood goal...).
i first create som data , plot in post:
df <- data.frame(year=as.factor(rep(2000:2005,each=9)), value=runif(6*9,0,10), variable=as.factor(rep(1:9,times=6))) full <- ggplot(data=df, aes(x=year, y=value)) + geom_area(aes(fill=variable,group=variable)) + theme(legend.title=element_blank())
there no need theme(legend.position='right')
since default. note added aesthetics group
geom_area
. if missing, ggplot
not know values belong same curve , produces empty plot.
now create scale manually in 2 steps. first, create scale of grays 9 levels, because have 9 levels in df$variable
. , modify few of these levels:
my_scale <- gray.colors(n=9,start=0.2,end=0.8) my_scale[c(4,7,8)] <- c("red", rgb(0,0.8,0.6), "#ff7f00") full + scale_fill_manual(values=my_scale)
as can see, possible use named colours, hexcodes, or rgb
(which returns hexcode).
instead of picking colours yourself, make use of colourbrewer, overs excellent palettes. gray.colors
above, 1 can use command brewer.pal
create palette:
pal <- brewer.pal(9,"set1") my_scale <- gray.colors(n=9,start=0.2,end=0.8) my_scale[c(4,7,8)] <- pal[1:3] full + scale_fill_manual(values=my_scale)
the arguments brewer.pal
number of colours want , name of palette. can browse various palettes on http://colorbrewer2.org/.
Comments
Post a Comment