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.