I'm writing a function that creates ggplot's scatter plot with size of points representing the number of points with the same X and Y coordinates.
I have a function that works:
require(dplyr)
plot_size_bubbles <- function(x,y) {
dd = data.frame(x,y) %>%
group_by(x,y) %>%
summarise(n=n()) %>%
ungroup()
ggplot(dd, aes(x,y)) + geom_point(aes(size=n))
}
X = sample(1:3,10,replace = T)
Y = sample(1:3,10,replace = T)
plot_size_bubbles(X,Y)
I'd like to make it in ggplot's style as a custom geometry function inherited from geom_point. Maybe I can use some stat function, not sure. Basically I'd like to pass to ggplot a data frame, map x and y, and create this plot without prior calculating the points size. Like
ggplot(data.frame(X,Y), aes(X,Y)) + geom_sizebubble()
In addition it would be great to have x and y axes labels from the original data frame.
Hope it's possible and I'm just missing something.