Sunday 15 March 2015

r - ggplot2: geom_smooth colour with scale_colour_brewer -



r - ggplot2: geom_smooth colour with scale_colour_brewer -

i have created simple graph using geom_smooth:

test.dat <- data.frame(matrix(c(runif(20, 0, 20)), 10, 2)) names(test.dat) <- c("x", "y") test.plot <- ggplot(test.dat, aes(x,y)) + geom_point() + geom_smooth(method="loess")

the line produced default blue. can alter manually given colour adding this:

test.plot + geom_smooth(method="loess", colour="black")

however, base of operations colour selection on palette have been using series of plots. normally, achieved adding respective line:

scale_colour_brewer(type = "div", palette = "spectral")

yet, not have effect (nor using scale_fill_brewer instead). have found give-and-take on behaviour of geom_smooth colour here, no apparent prepare particular problem. suggestions?

you can use:

test.plot + geom_smooth(aes(colour=true), method="loess") + scale_colour_brewer(type = "div", palette = "spectral")

the key thing utilize colour within mapping argument geom_smooth (the aes part). doesn't matter value use, has distinct whatever other values you're using specify other line colors in graph.

here fuller example:

set.seed(1) test.dat <- data.frame( x=runif(20), y=runif(20), z=sample(letters[1:2], 20, rep=t) ) ggplot(test.dat, aes(x, y, colour=z)) + geom_smooth(aes(colour="d"), method="loess") + geom_point() + geom_line() + scale_colour_brewer(type = "div", palette = "spectral")

notice how can geom_smooth participate in colour scale adding new distinct value ('d') existing colour values ('a', 'b'). ggplot collects values assigned colour aesthetic mapping (in case, unique(c(test.dat$z, "d"))), , allocates colors color scale them.

r colors ggplot2 colorbrewer

No comments:

Post a Comment