使用晶格创建一个双变量颜色渐变图例,以一个散点图覆盖带有Alpha的多边形 [英] Create a bivariate color gradient legend using lattice for an spplot overlaying polygons with alpha

查看:101
本文介绍了使用晶格创建一个双变量颜色渐变图例,以一个散点图覆盖带有Alpha的多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过使用spplot覆盖多边形并将填充的alpha值设置为10/255来创建地图,以使多边形重叠的区域具有更饱和的颜色.根据属性表中的二进制变量,将多边形设置为两种不同的颜色(蓝色和红色).因此,虽然颜色饱和度取决于重叠的多边形数量,但是颜色取决于多边形的蓝色和红色类别的比率.

I've created a map by overlaying polygons using spplot and with the alpha value of the fill set to 10/255 so that areas with more polygons overlapping have a more saturated color. The polygons are set to two different colors (blue and red) based on a binary variable in the attribute table. Thus, while the color saturation depends on the number of polygons overlapping, the color depends on the ratio of the blue and red classes of polygons.

当然,没有简单的内置图例,因此我需要从头开始创建一个图例.在此处中找到的基本图形中有一个很好的解决方案.我还基于在这里发布了一个类似的问题,我尽力提供了一些解决方案,但实际上无法提出一个可靠的答案.现在我需要为自己做同样的事情,但是我特别想使用R并还要使用grid图形.

There is, of course, no easy built-in legend for this so I need to create one from scratch. There is a nice solution to this in base graphics found here. I also came up with a not-so-good hack to do this in ggplot based on this post from kohske. A similar question was posted here and I did my best to give some solutions, but couldn't really come up with a solid answer. Now I need to do the same for myself, but I specifically would like to use R and also use grid graphics.

这是我想出的ggplot骇客

This is the ggplot hack I came up with

Variable_A <- 100 # max of variable
Variable_B <- 100

x <- melt(outer(1:Variable_A, 1:Variable_B)) # set up the data frame to plot from

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

哪个给这个:

出于我的目的,这不起作用,原因有两个. 1)由于Alpha值变化,当Alpha值变高时,绘制的第二种颜色(在这种情况下为蓝色)会淹没第一种颜色.正确的图例应沿1:1对角线均匀混合蓝色和红色.此外,这些颜色与地图颜色并不完全对应. 2)我不知道如何在使用spplot创建的lattice地图上覆盖ggplot对象.我尝试使用ggplotGrob(p)创建一个grob,但仍然不知道如何将grob添加到spplot地图.

This doesn't work for my purposes for two reasons. 1) Because the alpha value varies, the second color plotted (blue in this case) overwhelms the first one as the alpha values get higher. The correct legend should have blue and red mixed evenly along the 1:1 diagonal. In addition, the colors don't really properly correspond to the map colors. 2) I don't know how to overlay a ggplot object on the lattice map created with spplot. I tried to create a grob using ggplotGrob(p), but still couldn't figure out how to add the grob to the spplot map.

理想的解决方案是使用lattice图形创建相似的图形.我认为使用图块可能是正确的解决方案,但是最好的方法是使alpha值保持恒定,并从左到右(对于红色)和从下到上(对于蓝色)改变绘制的图块数量.因此,颜色和饱和度应与地图正确匹配(我认为...).

The ideal solution would be to create a similar figure using lattice graphics. I think that using tiles is probably the right solution, but what would be best is to have the alpha values stay constant and vary the number of tiles plotted going from left to right (for red) and bottom to top (for blue). Thus, the colors and saturation should properly match the map (I think...).

非常感谢您的帮助!

推荐答案

如何将角度映射到颜色,将alpha映射到两个变量的总和?这是否符合您的要求?

How about mapping the angle to color, and alpha to the sum of the two variables -- does this do what you 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())

这篇关于使用晶格创建一个双变量颜色渐变图例,以一个散点图覆盖带有Alpha的多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆