CephBirk
CephBirk

Reputation: 6720

Create balloon plot with ggplot2: use ..count.. to adjust size of geom_point?

I want to essentially create a balloon plot with ggplot2 where the size of the points are the frequency of data at a given coordinate.

Given the data.frame d:

d = structure(list(value.x = structure(c(2L, 2L, 3L, 2L, 3L, 2L, 2L, 2L, 3L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L), .Label = c("Not at all Knowledgeable", "Somewhat Knowledgeable", "Very Knowledgeable"), class = c("ordered", "factor")), value.y = structure(c(5L, 5L, 3L, 5L, 5L, 5L, 5L, 5L, 4L, 4L, 5L, 4L, 4L, 4L, 5L, 4L, 5L, 5L, 4L, 4L), .Label = c("Much less knowledgeable", "Less knowledgeable", "Same as before workshop", "More knowledgeable", "Much more knowledgeable"), class = c("ordered", "factor"))), .Names = c("value.x", "value.y"), row.names = c(NA, 20L), class = "data.frame")

I want to do something like:

ggplot(d,aes(value.x,value.y,size=..count..))+geom_point()

where the data points are proportional to how many times data occur, but I cannot figure out how to properly set the size of the points for what I want.

Importantly, I would like to avoid creating a new column in d just for counts of data as has been done with other datasets (e.g. http://www.r-bloggers.com/balloon-plot-using-ggplot2/). This seems messy and I would like to utilize ggplot2's power if I can.

Upvotes: 2

Views: 2044

Answers (1)

CephBirk
CephBirk

Reputation: 6720

Per @BenBolker's suggestion, I found a solution using stat_sum():

ggplot(d, aes(value.x, value.y, size = ..n..)) + stat_sum()

Upvotes: 1

Related Questions