3

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 :)

Community
  • 1
  • 1
tlorin
  • 1,100
  • 6
  • 17
  • 30
  • Why would (0,1) be twice as big as the others if it's the 5th observation? Do you want the size to be different for each observation? – Warner Aug 02 '16 at 14:03
  • @Warner see my edit :) (0,1) occurs twice, and all the others once, so the dot would be twice bigger. – tlorin Aug 02 '16 at 14:07

1 Answers1

10

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

Warner
  • 1,353
  • 9
  • 23