我正在尝试绘制两个变量,其中N=700K。问题是有太多的重叠,因此绘图主要是一个实心的黑色区块。有没有办法得到一个灰度“云”,其中绘图的暗度是一个区域中点数的函数?换句话说,我不想显示单独的点,而是想要一个“云”,一个区域中的点数越多,该区域就越暗。
发布于 2017-09-26 16:54:06
ggplot2中几个很好的选项概述
library(ggplot2)
x <- rnorm(n = 10000)
y <- rnorm(n = 10000, sd=2) + x
df <- data.frame(x, y)选项A:透明点
o1 <- ggplot(df, aes(x, y)) +
geom_point(alpha = 0.05)选项B:添加密度等值线
o2 <- ggplot(df, aes(x, y)) +
geom_point(alpha = 0.05) +
geom_density_2d()选项C:添加填充密度等值线
o3 <- ggplot(df, aes(x, y)) +
stat_density_2d(aes(fill = stat(level)), geom = 'polygon') +
scale_fill_viridis_c(name = "density") +
geom_point(shape = '.')选项D:密度热图
o4 <- ggplot(df, aes(x, y)) +
stat_density_2d(aes(fill = stat(density)), geom = 'raster', contour = FALSE) +
scale_fill_viridis_c() +
coord_cartesian(expand = FALSE) +
geom_point(shape = '.', col = 'white')选项E:六进制数
o5 <- ggplot(df, aes(x, y)) +
geom_hex() +
scale_fill_viridis_c() +
geom_point(shape = '.', col = 'white')选项F:地毯
o6 <- ggplot(df, aes(x, y)) +
geom_point(alpha = 0.1) +
geom_rug(alpha = 0.01)合并成一个图形:
cowplot::plot_grid(
o1, o2, o3, o4, o5, o6,
ncol = 2, labels = 'AUTO', align = 'v', axis = 'lr'
)

https://stackoverflow.com/questions/7714677
复制相似问题