r - Combine guides for continuous fill (color) and alpha scales -


in ggplot2, making geom_tile plot both color , alpha vary same variable, make single guide shows colors way appear on plot instead of 2 separate guides.

library(ggplot2)  x <- seq(-10,10,0.1) data <- expand.grid(x=x,y=x) data$z <- with(data,y^2 * dnorm(sqrt(x^2 + y^2), 0, 3)) p <- ggplot(data) + geom_tile(aes(x=x,y=y, fill = z, alpha = z)) p <- p + scale_fill_continuous(low="blue", high="red") + scale_alpha_continuous(range=c(0.2,1.0)) plot(p) 

this produces figure 2 guides: 1 color , 1 alpha. have 1 guide on both color , alpha vary way in figure (so color shifts blue, fades out)

for figure, achieve similar effect varying saturation instead of alpha, real project in using this, overlaying layer on top of map, , want vary alpha map more visible smaller values of z-variable.

i don't think can combine continuous scales 1 legend, can combine discrete scales. example:

# create discrete version of z data$z.cut = cut(data$z, seq(min(data$z), max(data$z), length.out=10))  ggplot(data) +     geom_tile(aes(x=x, y=y, fill=z.cut, alpha=z.cut)) +   scale_fill_hue(h=c(-60, -120), c=100, l=50) +   scale_alpha_discrete(range=c(0.2,1)) 

you can of course cut z @ different, perhaps more convenient, values , change scale_fill_hue whatever color scale prefer.

enter image description here


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -