[R] qplot, stacked area, own colourscheme

hadley wickham h.wickham at gmail.com
Wed Sep 24 15:47:27 CEST 2008


On Wed, Sep 24, 2008 at 8:39 AM, Albin Blaschka
<albin.blaschka at gmail.com> wrote:
>
> Dear list,
>
> I would like to apply my own colours to a stacked area plot, done with
> qplot, but I have not succeeded...
>
> What do I have so far (I am dealing with the development of cover of
> specific groups of plants):
>
>
> library(ggplot2)
> library(RODBC)
> channel <- odbcConnect("myusername", case="tolower")
> sql <- "select trial, cover_of, dateofsurvey, cover from mytable"
> mydata <- (sqlQuery(channel, sql))
>
> What I get is the following dataframe (simplified, there are more
> categories, not real data):
>
>        trial   cover_of                dateofsurvey    cover
> 1        ZU-316 Cover dead material     2004-09-16    0
> 2        ZU-316 Cover grasses           2004-09-16   16
> 3        ZU-316 Cover herbs             2004-09-16   14
> 4        ZU-316 Cover legumes           2004-09-16   10
> 5        ZU-316 Cover open soil         2004-09-16   30
> 6        ZU-316 Cover dead material     2005-09-18    5
> 7        ZU-316 Cover grasses           2005-09-18   26
> 8        ZU-316 Cover herbs             2005-09-18   14
> 9        ZU-316 Cover legumes           2005-09-18   15
> 10       ZU-316 Cover open soil         2005-09-18   20
> 11       ZU-316 Cover dead material     2006-09-06    7
> 12       ZU-316 Cover grasses           2006-09-06   36
> 13       ZU-316 Cover herbs             2006-09-06   20
> 14       ZU-316 Cover legumes           2006-09-06   15
> 15       ZU-316 Cover open soil         2006-09-06   20
> .
> .
> .
>
> With the following command I get a nice graph (development of cover over the
> years):
>
> qplot(dateofsurvey, cover, data=mydata, geom="area", fill=cover_of,
>        main = "Development of cover, ZU-316",
>        ylab="projective Cover[%]", xlab = "Year")
>
> As I have those categories (cover grasses, cover herbs...) I would like to
> assign special colours to each (for example "cover grasses" => dark green,
> "cover open soil" => brown ...), but I did not get it so far.
> I tried several variations with area_geom, scale_manual but without
> success...
> For example (which does not work, it just shows the standard colourscheme):
>
> mycolorscheme <- c("khaki", "darkgreen",
>                   "lightgreen", "yellow", "saddlebrown")
> mycolors <- rep(mycolorscheme,3)
>
> myplot <- qplot(dateofsurvey, wert, data=mydata, geom="area",
>                fill=cover_of, main = "Development of cover, ZU-316",
>                ylab="projective Cover[%]", xlab = "Year")
> myplot + geom_area(colour = mycolours)

You want to modify the scale (the thing that converts data values to
colours), not the geom:

+ scale_fill_manual(values = mycolorscheme)

(And it's fill you want to change, not colour)

This will also update the legend.

Hadley


-- 
http://had.co.nz/



More information about the R-help mailing list