1% Generated by roxygen2: do not edit by hand
2% Please edit documentation in R/geom-count.r, R/stat-sum.r
3\name{geom_count}
4\alias{geom_count}
5\alias{stat_sum}
6\title{Count overlapping points}
7\usage{
8geom_count(
9  mapping = NULL,
10  data = NULL,
11  stat = "sum",
12  position = "identity",
13  ...,
14  na.rm = FALSE,
15  show.legend = NA,
16  inherit.aes = TRUE
17)
18
19stat_sum(
20  mapping = NULL,
21  data = NULL,
22  geom = "point",
23  position = "identity",
24  ...,
25  na.rm = FALSE,
26  show.legend = NA,
27  inherit.aes = TRUE
28)
29}
30\arguments{
31\item{mapping}{Set of aesthetic mappings created by \code{\link[=aes]{aes()}} or
32\code{\link[=aes_]{aes_()}}. If specified and \code{inherit.aes = TRUE} (the
33default), it is combined with the default mapping at the top level of the
34plot. You must supply \code{mapping} if there is no plot mapping.}
35
36\item{data}{The data to be displayed in this layer. There are three
37options:
38
39If \code{NULL}, the default, the data is inherited from the plot
40data as specified in the call to \code{\link[=ggplot]{ggplot()}}.
41
42A \code{data.frame}, or other object, will override the plot
43data. All objects will be fortified to produce a data frame. See
44\code{\link[=fortify]{fortify()}} for which variables will be created.
45
46A \code{function} will be called with a single argument,
47the plot data. The return value must be a \code{data.frame}, and
48will be used as the layer data. A \code{function} can be created
49from a \code{formula} (e.g. \code{~ head(.x, 10)}).}
50
51\item{position}{Position adjustment, either as a string, or the result of
52a call to a position adjustment function.}
53
54\item{...}{Other arguments passed on to \code{\link[=layer]{layer()}}. These are
55often aesthetics, used to set an aesthetic to a fixed value, like
56\code{colour = "red"} or \code{size = 3}. They may also be parameters
57to the paired geom/stat.}
58
59\item{na.rm}{If \code{FALSE}, the default, missing values are removed with
60a warning. If \code{TRUE}, missing values are silently removed.}
61
62\item{show.legend}{logical. Should this layer be included in the legends?
63\code{NA}, the default, includes if any aesthetics are mapped.
64\code{FALSE} never includes, and \code{TRUE} always includes.
65It can also be a named logical vector to finely select the aesthetics to
66display.}
67
68\item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics,
69rather than combining with them. This is most useful for helper functions
70that define both data and aesthetics and shouldn't inherit behaviour from
71the default plot specification, e.g. \code{\link[=borders]{borders()}}.}
72
73\item{geom, stat}{Use to override the default connection between
74\code{geom_count()} and \code{stat_sum()}.}
75}
76\description{
77This is a variant \code{\link[=geom_point]{geom_point()}} that counts the number of
78observations at each location, then maps the count to point area. It
79useful when you have discrete data and overplotting.
80}
81\section{Aesthetics}{
82
83\code{geom_point()} understands the following aesthetics (required aesthetics are in bold):
84\itemize{
85\item \strong{\code{x}}
86\item \strong{\code{y}}
87\item \code{alpha}
88\item \code{colour}
89\item \code{fill}
90\item \code{group}
91\item \code{shape}
92\item \code{size}
93\item \code{stroke}
94}
95Learn more about setting these aesthetics in \code{vignette("ggplot2-specs")}.
96}
97
98\section{Computed variables}{
99
100\describe{
101\item{n}{number of observations at position}
102\item{prop}{percent of points in that panel at that position}
103}
104}
105
106\examples{
107ggplot(mpg, aes(cty, hwy)) +
108 geom_point()
109
110ggplot(mpg, aes(cty, hwy)) +
111 geom_count()
112
113# Best used in conjunction with scale_size_area which ensures that
114# counts of zero would be given size 0. Doesn't make much different
115# here because the smallest count is already close to 0.
116ggplot(mpg, aes(cty, hwy)) +
117 geom_count() +
118 scale_size_area()
119
120# Display proportions instead of counts -------------------------------------
121# By default, all categorical variables in the plot form the groups.
122# Specifying geom_count without a group identifier leads to a plot which is
123# not useful:
124d <- ggplot(diamonds, aes(x = cut, y = clarity))
125d + geom_count(aes(size = after_stat(prop)))
126# To correct this problem and achieve a more desirable plot, we need
127# to specify which group the proportion is to be calculated over.
128d + geom_count(aes(size = after_stat(prop), group = 1)) +
129  scale_size_area(max_size = 10)
130
131# Or group by x/y variables to have rows/columns sum to 1.
132d + geom_count(aes(size = after_stat(prop), group = cut)) +
133  scale_size_area(max_size = 10)
134d + geom_count(aes(size = after_stat(prop), group = clarity)) +
135  scale_size_area(max_size = 10)
136}
137\seealso{
138For continuous \code{x} and \code{y}, use \code{\link[=geom_bin2d]{geom_bin2d()}}.
139}
140