我的问题很简单:我有一些带有 x,y 坐标的点,它们位于由 1x1 正方形组成的矩形网格内。这些点具有平均坐标,因此多个点被赋予相同的坐标(它们完全重叠)。可重现的例子:
# generate fake data
y <- seq(from=0.5, to=9.5, by=1)
x <- seq(from=0.5, to=4.5, by=1)
xnew <- sample(x,100,replace=T)
ynew <- sample(y,100,replace=T)
data <- data.frame(xnew,ynew)
# create chart
ggplot(data, aes(x=xnew, y=ynew)) + geom_point()
我想表示特定位置(x,y 坐标,表示特定正方形)处的点的频率。stat_bin2d
是朝着正确方向迈出的一步,但垃圾箱(对我来说)莫名其妙地放置在地图上的不同位置,使得很难直观地看到分布情况。
我可以想象两种不同的解决方案:
1) Is there a way to center the bins on the points? Sometimes the lower left-hand corner is at the point, sometimes the lower right-hand corner, etc. like this:
此外,如果盒子足够大以能够相互接触,那将是理想的,但是当我改变binwidth=c(1,1)
in stat_bin2d()
,它实际上改变了计数,尽管箱不应重叠,因为所有点彼此之间至少相距 1 个平方。
或者,使用磅值:
2)我更喜欢其大小能够反映浓度的点(这样在黑白中也更好)。我尝试使用 geom_point():
ggplot(data, aes(x=xnew, y=ynew))+geom_point(aes(x=xnew,y=ynew, size=..count..))
但我得到了
Error in eval(expr, envir, enclos) : object 'count' not found
然后,如果我添加 `stat="bin",它会与 y 的分配发生冲突。我看这里:为什么 ggplot2 不允许我为每个单独的点设置大小?,但无法使其发挥作用。
感谢您的帮助。