Sum unique values. Useful for overplotting on scatterplots.


stat_sum(mapping = NULL, data = NULL, geom = "point", position = "identity", ...)


The aesthetic mapping, usually constructed with aes or aes_string. Only needs to be set at the layer level if you are overriding the plot defaults.
A layer specific dataset - only needed if you want to override the plot defaults.
The geometric object to use display the data
The position adjustment to use for overlappling points on this layer
other arguments passed on to layer. This can include aesthetics whose values you want to set, not map. See layer for more details.


a data.frame with additional columns nnumber of observations at position proppercent of points in that panel at that position


Sum unique values. Useful for overplotting on scatterplots.


stat_sum understands the following aesthetics (required aesthetics are in bold):

  • x
  • y
  • size


d <- ggplot(diamonds, aes(x = cut, y = clarity)) # By default, all categorical variables in the plot form grouping # variables, and the default behavior in stat_sum is to show the # proportion. Specifying stat_sum with no group identifier leads to # a plot which is not meaningful: d + stat_sum()

# To correct this problem and achieve a more desirable plot, we need # to specify which group the proportion is to be calculated over. # There are several ways to do this: # by overall proportion d + stat_sum(aes(group = 1))

d + stat_sum(aes(group = 1)) + scale_size(range = c(3, 10))

d + stat_sum(aes(group = 1)) + scale_area(range = c(3, 10))
scale_area is deprecated. Use scale_size_area instead. Note that the behavior of scale_size_area is slightly different: by default it makes the area proportional to the numeric value. (Deprecated; last used in version 0.9.2)

# by cut d + stat_sum(aes(group = cut))

d + stat_sum(aes(group = cut, colour = cut))

# by clarity d + stat_sum(aes(group = clarity))

d + stat_sum(aes(group = clarity, colour = cut))

# Instead of proportions, can also use sums d + stat_sum(aes(size = ..n..))

# Can also weight by another variable d + stat_sum(aes(group = 1, weight = price))

d + stat_sum(aes(group = 1, weight = price, size = ..n..))

# Or using qplot qplot(cut, clarity, data = diamonds)

qplot(cut, clarity, data = diamonds, stat = "sum", group = 1)

See also

ggfluctuation for a fluctuation diagram,