R ggplot2显示相互叠加的点的计数:stat_bin2d或geom_tile或点大小?
我的问题很简单:我有一些x,y坐标的点,它们位于由1x1个正方形组成的矩形网格内。这些点具有平均坐标,因此多个点具有相同的坐标(它们完全重叠)。可复制示例:R ggplot2显示相互叠加的点的计数:stat_bin2d或geom_tile或点大小?,r,ggplot2,R,Ggplot2,我的问题很简单:我有一些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 <
# 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()
但是我有
Error in eval(expr, envir, enclos) : object 'count' not found
然后,如果我添加`stat=“bin”,它与y的赋值冲突。我看了看这里,但没能让它工作
谢谢你的帮助
data2ggplot2 2.0.0版的引入正是为了做到这一点。使用您的数据:
data2 <- aggregate(data$x,by=list(x=data$x,y=data$y),length)
names(data2)[3] <- "count"
ggplot(data2, aes(x=x,y=y)) + geom_point(aes(size=count))
ggplot(data, aes(x=xnew,y=ynew)) +
geom_count()
收益率:
太好了,@Roland,谢谢!这就解决了点的问题。如果有人有想法,我也想知道另一种方法。
ggplot(data, aes(x=xnew,y=ynew)) +
geom_count()