ggplot2 visualizing counts of points plotted on to

2020-04-14 03:55发布

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: as in here

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.

标签: r ggplot2
2条回答
放我归山
2楼-- · 2020-04-14 04:22
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))

enter image description here

查看更多
迷人小祖宗
3楼-- · 2020-04-14 04:30

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: geom_count chart

查看更多
登录 后发表回答