tags:

views:

263

answers:

1

Given the following ggplot2 chart:

ggplot(my_data, aes(colour=my_factor) +   
                geom_point(aes(x=prior, y=current)) +   
                facet_grid(gender ~ age)

I would like to make the size of the points be proportional to the count of my_factor for that prior/current combination.

ggplot(my_data, aes(colour=my_factor, 
                size=<something-here>(my_factor)) +   
                geom_point(aes(x=prior, y=current)) + 
                facet_grid(gender ~ age)

Any ideas?

== Edit ==

Here's a very trivial example based on mpg dataset. Let's define "great_hwy" as hwy > 35, and "great_cty" as cty > 25:

mpg$great_hwy[mpg$hwy > 35]  <-1
mpg$great_hwy[mpg$hwy <= 35] <-0
mpg$great_hwy <- factor(mpg$great_hwy)

mpg$great_cty[mpg$cty > 25]  <- 1
mpg$great_cty[mpg$cty <= 25] <- 0
mpg$great_cty <- factor(mpg$great_cty)

If we plot great_hwy vs. great_cty, it won't tell us much:

ggplot(mpg) + geom_point(aes(x=great_cty, y=great_hwy))

How could I make the data points bigger in size depending on the number of x/y points? Hope this clears it up, but let me know otherwise.

+10  A: 
Harlan
That's a great solution. Thanks!
Shane
Exactly what I was looking for. Looks like *most* cars are not great in terms of city and highway mileage ;)
hgimenez
You might also want to check out this page, just to make sure that the size of the points is what you think it is (radius? area?): http://had.co.nz/ggplot2/scale_size.html I think having proportional areas is traditionally preferred to proportional radii.
Matt Parker
Yes, but ggplot2 doesn't do that because it only works for points - not (e.g.) lines or text. scale_area is strongly recommended for points!
hadley