R 用alpha通道覆盖两个ggplot2统计密度2D图

R 用alpha通道覆盖两个ggplot2统计密度2D图,r,plot,ggplot2,R,Plot,Ggplot2,我想用alpha通道覆盖两个ggplot2绘图,结果图像显示两个数据集。这是我的测试数据: data = read.table(text="P1 -1 0 4\nP2 0 0 2\nP3 2 1 8\nP4 -2 -2 6\nP5 0.5 2 12") data2 = read.table(text="Q1 1 1 3\nQ2 1 -1 2\nQ3 -1 1 8") colnames(data) = c("name","x","y","score") colnames(data2) = c("n

我想用alpha通道覆盖两个
ggplot2
绘图,结果图像显示两个数据集。这是我的测试数据:

data = read.table(text="P1 -1 0 4\nP2 0 0 2\nP3 2 1 8\nP4 -2 -2 6\nP5 0.5 2 12")
data2 = read.table(text="Q1 1 1 3\nQ2 1 -1 2\nQ3 -1 1 8")
colnames(data) = c("name","x","y","score")
colnames(data2) = c("name","x","y","score")
下面是我如何绘制这些数据的:

ggplot(data, aes(x=x,y=y)) + 
  stat_density2d(data=data,geom="tile", aes(fill = ..density..,alpha=..density..), contour=FALSE) + 
  theme(legend.position="none") + scale_fill_gradient (low = "#FFFFFF", high = "#FF0000") + 
  xlim(-3,3) + ylim(-3,3) + 
  geom_point()

ggplot(data2, aes(x=x,y=y)) + 
  stat_density2d(data=data2,geom="tile", aes(fill = ..density..,alpha=..density..), contour=FALSE) + 
  theme(legend.position="none") + 
  scale_fill_gradient (low = "#FFFFFF", high = "#00FF00") + 
  xlim(-3,3) + ylim(-3,3) + 
  geom_point()
第一个图显示数据,第二个图显示数据2:

我现在想要两个情节的组合。下图是我想要得到的。我在桌面上用我的图像编辑程序制作了它,将两幅图像乘以图层

我试图在另一个数据集上绘制一个数据集,但这不会使两个层都相乘,第二个颜色会覆盖第一个

ggplot(data, aes(x=x,y=y)) + 
  stat_density2d(data=data,geom="tile", aes(fill = ..density..,alpha=..density..), contour=FALSE) + 
  theme(legend.position="none") + scale_fill_gradient (low = "#FFFFFF", high = "#FF0000") + 
  xlim(-3,3) + ylim(-3,3) + 
  stat_density2d(data=data2,geom="tile", aes(fill = ..density..,alpha=..density..), contour=FALSE) + 
  scale_fill_gradient (low = "#FFFFFF", high = "#00FF00")

此外,我还收到这样的警告:“填充”的比例已经存在。为“填充”添加另一个比例,该比例将替换现有比例

有没有办法在R中做到这一点?或者是否有其他方法(使用其他函数,如smoothScatter)获得此结果或类似结果?作为一种解决方法,我认为在服务器上使用ImageMagick会得到类似的结果,但我更喜欢在R中完成这一切

更新1

两层的乘法是通过ImageMagick这样做的

composite -compose multiply data-red.png data-green.png im-multiply.png
这将产生与上面所示相同的结果

更新2

@罗兰在他的回答中教我如何在同一个图中绘制两个数据集。虽然这很好,但仍然存在一个问题:图像取决于向绘图提供数据的顺序

ggplot(rbind(data.frame(data, group="a"), data.frame(data2, group="b")), aes(x=x,y=y)) + 
  stat_density2d(geom="tile", aes(fill = group, alpha=..density..), contour=FALSE) + 
  scale_fill_manual(values=c("a"="#FF0000", "b"="#00FF00")) + 
  geom_point() + 
  theme_minimal() + 
  xlim(-3.3, 3.3) + ylim(-3.3, 3.3) +
  coord_cartesian(xlim = c(-3.2, 3.2), ylim = c(-3.2, 3.2))
给出以下结果:

当交换两个数据集的顺序时(现在先是数据集“b”又名data2,然后是数据集数据又名“a”),会得到类似的结果,但现在红色占主导地位,因为它会在稍后绘制,因此会覆盖绿色数据

ggplot(rbind(data.frame(data2, group="a"), data.frame(data, group="b")), aes(x=x,y=y)) + 
  stat_density2d(geom="tile", aes(fill = group, alpha=..density..), contour=FALSE) + 
  scale_fill_manual(values=c("b"="#FF0000", "a"="#00FF00")) +
  geom_point() + theme_minimal() + 
  xlim(-3.3, 3.3) + ylim(-3.3, 3.3) + 
  coord_cartesian(xlim = c(-3.2, 3.2), ylim = c(-3.2, 3.2))


我需要一个不依赖于数据集顺序的解决方案。

您应该在同一比例上绘制两种密度:

ggplot(rbind(data.frame(data, group="a"), data.frame(data2, group="b")), 
       aes(x=x,y=y)) + 
  stat_density2d(geom="tile", aes(fill = group, alpha=..density..), 
                 contour=FALSE) + 
  scale_fill_manual(values=c("a"="#FF0000", "b"="#00FF00")) +
  geom_point() +
  theme_minimal() +
  xlim(-3.3, 3.3) + ylim(-3.3, 3.3) +
  coord_cartesian(xlim = c(-3.2, 3.2), ylim = c(-3.2, 3.2))


否则,您将显示数据的扭曲图片。

这里的解决方案与@Roland完全相同,只是我建议使用controur line。这使您能够理解重叠。 我看不出geom_tile和你的“乘法”思想能让你体会到这一点。也许,若你们用蓝色和红色表示无重叠区域,用“加权”紫色表示重叠区域。但我想你必须在绘图前的前一步计算它

ggplot(rbind(data.frame(data, group="a"), data.frame(data2, group="b")), 
       aes(x=x,y=y)) + 
  stat_density2d(geom="density2d", aes(color = group,alpha=..level..),
                 size=2,
                 contour=TRUE) + 
  #scale_color_manual(values=c("a"="#FF0000", "b"="#00FF00")) +
  geom_point() +
  theme_minimal() +
  xlim(-3.3, 3.3) + ylim(-3.3, 3.3) +
  coord_cartesian(xlim = c(-3.2, 3.2), ylim = c(-3.2, 3.2))


不幸的是:。而且,由于这是哈德利的作品,你很可能真的被困在了生成两个情节并用ImageMagick转换的过程中。谢谢你给我指出这段话。然后我将使用ImageMagick的方法。关于影响最终颜色的堆叠顺序,这里也有一条线索:最好的解决方案是自己计算堆栈的颜色并绘制这些颜色-下面是一个示例,尽管不在ggplot2中:根据顺序得到的不同结果也可能与混合在背景中的白色有关。您也可以尝试使用MASS::kde2d自己计算密度,在堆叠时计算适当的rgb值,并使用类似qplot的方法绘制(x,y,data=mydata,fill=rgb,geom=“光栅”)+比例填充标识()这对我的尝试是一个巨大的改进。谢谢。这两层有没有可能成倍增加?在你的代码中,后一个数据集在视觉上占主导地位,因此绿色比红色更亮。如果我交换数据和数据2,红色会更亮。a组的局部密度最大值高于gr组oup b。绘图确实反映了这一点。我不知道“乘以”是什么意思。但当a组(以红色绘制)的最大值更高时,为什么绿色(b组)更亮?如果您更改代码中的数据和数据2:
rbind(data.frame(data2,group=“a”)、data.frame(data,group=“b”)
值=c(“b”=“=“b”=“\FF0000”、“a”=“\3500ff00”)
–然后红色比绿色亮。乘以我指的是图像编辑中两层的混合模式。这种模式是对称的,所以执行顺序无关紧要。抱歉,我在编写过程中混淆了这一点。b组的最大值更高。为什么b组的最大值应该更高?事实上,您的解决方案取决于将数据馈送到
ggplot
的顺序后来打印的数据有点覆盖了前者。我想你指的是
scale\u color\u manual
,而不是
scale\u fill\u manual
@WilliamZhang。是的,谢谢。实际上我复制/粘贴了罗兰的解决方案,并做了最小的更改。我现在更正了我的帖子。谢谢,这是一个有趣的视觉方法。虽然它工作得很好对于我展示的测试数据,我不确定这是否适用于我的真实数据。尽管如此+1。