r - Create a bivariate color gradient legend using lattice for an spplot overlaying polygons with alpha -
r - Create a bivariate color gradient legend using lattice for an spplot overlaying polygons with alpha -
i've created map overlaying polygons using spplot , alpha value of fill set 10/255 areas more polygons overlapping have more saturated color. polygons set 2 different colors (blue , red) based on binary variable in attribute table. thus, while color saturation depends on number of polygons overlapping, color depends on ratio of bluish , reddish classes of polygons.
there is, of course, no easy built-in legend need create 1 scratch. there nice solution in base of operations graphics found here. came not-so-good hack in ggplot based on this post kohske. similar question posted here , did best give solutions, couldn't come solid answer. need same myself, utilize r , utilize grid
graphics.
this ggplot hack came with
variable_a <- 100 # max of variable variable_b <- 100 x <- melt(outer(1:variable_a, 1:variable_b)) # set info frame plot p <- ggplot(x) + theme_classic() + scale_alpha(range=c(0,0.5), guide="none") + geom_tile(aes(x=var1, y=var2, fill="variable_a", col.regions="red", alpha=var1)) + geom_tile(aes(x=var1, y=var2, fill="variable_b", col.regions="blue", alpha=var2)) + scale_x_continuous(limits = c(0, variable_a), expand = c(0, 0)) + scale_y_continuous(limits = c(0, variable_b), expand = c(0, 0)) + xlab("variable_a") + ylab("variable_b") + guides(fill=false) p
which gives this:
this doesn't work purposes 2 reasons. 1) because alpha value varies, sec color plotted (blue in case) overwhelms first 1 alpha values higher. right legend should have bluish , reddish mixed evenly along 1:1 diagonal. in addition, colors don't correspond map colors. 2) don't know how overlay ggplot
object on lattice
map created spplot
. tried create grob using ggplotgrob(p)
, still couldn't figure out how add together grob spplot
map.
the ideal solution create similar figure using lattice
graphics. think using tiles right solution, best have alpha values remain constant , vary number of tiles plotted going left right (for red) , bottom top (for blue). thus, colors , saturation should match map (i think...).
any help much appreciated!
how mapping angle color, , alpha sum of 2 variables -- want?
d <- expand.grid(x=1:100, y=1:100) ggplot(d, aes(x, y, fill=atan(y/x), alpha=x+y)) + geom_tile() + scale_fill_gradient(high="red", low="blue")+ theme(legend.position="none", panel.background=element_blank())
r ggplot2 alpha lattice sp
Comments
Post a Comment