I'm currently working on a project that involves creating plots very similar to examples in Hadley's ggplot2 0.9.0 page regarding stat_density2d().
我目前正在开发一个项目,涉及创建非常类似于Hadley的ggplot2 0.9.0页面中有关stat_density2d()的示例。
library(ggplot2)
dsmall <- diamonds[sample(nrow(diamonds), 1000), ]
d <- ggplot(dsmall, aes(carat, price)) + xlim(1,3)
d + stat_density2d(geom="tile", aes(fill = ..density..), contour = FALSE)
last_plot() + scale_fill_gradient(limits=c(1e-5,8e-4))
Now, what I am struggling with is a way to essentially turn alpha off (alpha=0) for all tiles not in the fill-range. So every grey tile seen in the image, the alpha should be set to 0. This would make the image a lot nicer, especially when overlaying on top of a map for example.
现在,我正在努力的是一种基本上为所有不在填充范围内的瓷砖关闭alpha(alpha = 0)的方法。因此,在图像中看到的每个灰色图块,alpha都应该设置为0.这将使图像更好,尤其是当覆盖在地图上时。
If anyone has any suggestions, this would be greatly appreciated.
如果有人有任何建议,我将不胜感激。
2 个解决方案
#1
9
This seems to work:
这似乎有效:
d + stat_density2d(geom="tile",
aes(fill = ..density..,
alpha=cut(..density..,breaks=c(0,1e-5,Inf))),
contour = FALSE)+
scale_alpha_manual(values=c(0,1),guide="none")
#2
11
Another possibility, just using ifelse
instead of cut
.
另一种可能性,只是使用ifelse而不是切割。
d + stat_density2d(geom="tile",
aes(fill = ..density.., alpha = ifelse(..density.. < 1e-5, 0, 1)),
contour = FALSE) +
scale_alpha_continuous(range = c(0, 1), guide = "none")
#1
9
This seems to work:
这似乎有效:
d + stat_density2d(geom="tile",
aes(fill = ..density..,
alpha=cut(..density..,breaks=c(0,1e-5,Inf))),
contour = FALSE)+
scale_alpha_manual(values=c(0,1),guide="none")
#2
11
Another possibility, just using ifelse
instead of cut
.
另一种可能性,只是使用ifelse而不是切割。
d + stat_density2d(geom="tile",
aes(fill = ..density.., alpha = ifelse(..density.. < 1e-5, 0, 1)),
contour = FALSE) +
scale_alpha_continuous(range = c(0, 1), guide = "none")