【发布时间】:2014-07-27 13:02:35
【问题描述】:
我想用 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()
第一个图显示data,第二个图data2:
我现在想要两个情节的组合。下图是我想要得到的。我使用桌面上的图像编辑程序通过将两个图像作为图层相乘来生成它。
我尝试将一个数据集绘制在另一个数据集之上,但这不会将两个图层相乘,并且第二种颜色会覆盖第一个。
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
@Roland 在他的回答中教我如何在同一个图中绘制两个数据集。虽然这很好,但仍然存在一个问题:图像取决于您将数据提供给绘图的顺序。
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 首先出现,然后是数据集 data 又名“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))
我需要一个不依赖于数据集顺序的解决方案。
【问题讨论】:
-
不幸的是:"Currently there can be only one scale per plot (for everything except x and y)."。而且,由于那是来自 Hadley,你很可能真的被困在生成两个图并使用 ImageMagick 转换。
-
感谢您向我指出此声明。那我就用 ImageMagick 的方式。
-
重新排列影响最终颜色的堆叠顺序,还有这个线程:mail-archive.com/r-help@r-project.org/msg84014.html 最好的解决方案是自己计算堆栈的颜色并绘制它们 - 这是一个示例,虽然不在 ggplot2 中:stackoverflow.com/questions/13867782/…
-
根据订单获得的不同结果也可能与混入背景的白色有关。
-
您也可以尝试使用 MASS::kde2d 自己计算密度,在堆叠时计算适当的 rgb 值,并使用 qplot(x, y, data=mydata, fill=rgb, geom="raster") + scale_fill_identity()