Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Scatterplot/bubble chart with dot size based on observation number

I know this question has been somewhat answered here, here and here.

In all of these examples the dot/bubble size is based on a third factor such as size.

However, in my data, I only have 2 variables, xval and yval.

library("ggplot2")
xval <- c("0","0.5","0.25","0","0")
yval <- c("1","0.5","0.25","0.25","1")
df.test <- data.frame(xval,yval)
df.test
p <- ggplot(df.test, aes(x = xval, y = yval)) + geom_point()
p

Here is df.test

  xval yval
1    0    1
2  0.5  0.5
3 0.25 0.25
4    0 0.25
5    0    1

and here is p

enter image description here

What I would like is each dot/bubble size to depend on the number of occurrence of the observations of this coordinate. For instance, (0,1) would be to be twice as big as the others dots. I would like to avoid adding a 3rd column to my data frame, and leaving R doing it automatically.

I don't know if this can be done without having to play too much with the data... Any insight would be much appreciated :)

like image 647
tlorin Avatar asked Dec 04 '25 21:12

tlorin


1 Answers

Use geom_count()

xval <- c("0","0.5","0.25","0","0")
yval <- c("1","0.5","0.25","0.25","1")
df.test <- data.frame(xval,yval)
df.test

ggplot(df.test, aes(x = xval, y = yval)) + geom_count()

enter image description here

like image 175
Warner Avatar answered Dec 06 '25 12:12

Warner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!