ggplot2可视化绘制在彼此之上绘制的点数:stat_bin2d或geom_tile或点大小? [英] ggplot2 visualizing counts of points plotted on top of each other: stat_bin2d or geom_tile or point size?
问题描述
我的问题很简单:我有一些x,y坐标点,它们位于由1x1方块组成的矩形网格内。这些点具有平均坐标,因此几个点被赋予相同的坐标(它们完全重叠)。可重复的例子:
$ p $ #生成假数据
y < - seq(from = 0.5,to = 9.5,by = 1)
x < - seq(从= 0.5到= 4.5,by = 1)
xnew <样本(x,100,替换= T)
新< (y,100,replace = T)
data < - data.frame(xnew,ynew)
#创建图表
ggplot(data,aes(x = y = ynew))+ geom_point()
我想表示特定位置的点的频率(x,y坐标,代表特定的正方形)。 stat_bin2d
是朝正确方向迈出的一步,但对于地图上的不同位置,这些箱子是莫名其妙的(对我来说),因此难以直观地看到分布。
我可以想象两种不同的解决方案:
<1>有没有办法让分档居中?有时候左下角是在这个点上,有时是右下角,等等:
另外,如果这些盒子足够大以便彼此接触,但是当我更改 binwidth = c(1,1)
在 stat_bin2d()
中,它实际上改变了计数,尽管箱不应该重叠,因为所有点至少1 b
$ b
或使用积分尺寸:
2)我宁愿点大小会反映浓度(这种方式在黑白方面会更好)。我尝试了geom_point():
pre $ g $ pggplot(data,aes(x = xnew,y = ynew))+ geom_point(aes (x = xnew,y = ynew,size = .. count ..))
eval中的错误(expr,envir,enclos):找不到对象'count'
然后,如果我添加`stat =bin,它会与赋值为y的值冲突。我看了这里:为什么不会wont ggplot2允许我为每个点设置一个大小?,但无法使其工作。
感谢您期待任何帮助。
ggplot(data,aes(x = xnew,y = ynew))+
geom_count( )
收益率:
My problem is simple: I have some points with x,y coordinates, which are positioned inside a rectangular grid made up of 1x1 squares. These points have averaged coordinates, so that several points are given the same coordinates (they overlap completely). Reproducible example:
# 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()
I want to represent the frequency of points at a particular location (x,y coordinate, representing a particular square). stat_bin2d
is a step in the right direction, but the bins are inexplicably (for me) placed at different spots on the map, making it difficult to see the distribution visually.
I can imagine two different solutions:
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:
Additionally, it would be ideal if the boxes would be big enough to touch each other, but when I change the binwidth=c(1,1)
in stat_bin2d()
, it actually changes the counts, although the bins should not overlap, since all points are at least 1 square away from each other.
or, using point sizes:
2) I would prefer points whose size would reflect the concentration (that way would be better in black and white, too). I tried with geom_point():
ggplot(data, aes(x=xnew, y=ynew))+geom_point(aes(x=xnew,y=ynew, size=..count..))
but I got
Error in eval(expr, envir, enclos) : object 'count' not found
and then, if I add `stat="bin", it conflicts with the assignment to y. I looked here:Why wont ggplot2 allow me to set a size for each individual point?, but was not able to make it work.
Thanks in anticipation for any help.
ggplot2 version 2.0.0 introduced geom_count() to do precisely this. With your data:
ggplot(data, aes(x=xnew,y=ynew)) +
geom_count()
Yields:
这篇关于ggplot2可视化绘制在彼此之上绘制的点数:stat_bin2d或geom_tile或点大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!