stat_sum

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

Sum unique values. Useful for overplotting on scatterplots

This page describes stat_sum, see layer and qplot for how to create a complete plot from individual components.

What do you think of the documentation? Please let me know by filling out this short online survey.

Aesthetics

The following aesthetics can be used with stat_sum. Aesthetics are mapped to variables in the data with the aes function: stat_sum(aes(x = var)). Note that you do not need quotes around the variable name.

Scales control how the variable is mapped to the aesthetic and are listed after each aesthetic.

Aesthetic Default Related scales
xrequiredcontinuous, date, datetime, discrete
yrequiredcontinuous, date, datetime, discrete
size..prop..identity, manual, size

Layers are divided into groups by the group aesthetic. By default this is set to the interaction of all categorical variables present in the plot.

New variables produced by the statistic

To use these variables in an aesthetic mapping, you need to surrond them with .., like aes(x = ..output..). This tells ggplot that the variable isn't the original dataset, but has been created by the statistic.

Parameters

Parameters control the appearance of the stat. In addition to the parameters listed below (if any), any aesthetic can be used as a parameter, in which case it will override any aesthetic mapping.

Returns

This function returns a layer object.

See also

Examples

> d <- ggplot(diamonds, aes(x = cut, y = clarity)) 
> # Need to control which group proportion calculated over 
> # Overall proportion 
> d + stat_sum(aes(group = 1)) 
  
> d + stat_sum(aes(group = 1)) + scale_size(to = c(3, 10)) 
  
> d + stat_sum(aes(group = 1)) + scale_area(to = c(3, 10)) 
  
> # by cut 
> d + stat_sum(aes(group = cut)) 
  
> d + stat_sum(aes(group = cut, colour = cut)) 
Error: replacement element 1 has 5 rows, need 6
  
> # by clarity 
> d + stat_sum(aes(group = clarity)) 
  
> d + stat_sum(aes(group = clarity, colour = cut)) 
Error: replacement element 1 has 5 rows, need 6
  
>  
> # 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) 
  

What do you think of the documentation? Please let me know by filling out this short online survey.