forked from tidyverse/ggplot2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeom_histogram.Rd
168 lines (138 loc) · 6.24 KB
/
geom_histogram.Rd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/geom-freqpoly.r, R/geom-histogram.r, R/stat-bin.r
\name{geom_freqpoly}
\alias{geom_freqpoly}
\alias{geom_histogram}
\alias{stat_bin}
\title{Histograms and frequency polygons.}
\usage{
geom_freqpoly(mapping = NULL, data = NULL, stat = "bin",
position = "identity", na.rm = FALSE, show.legend = NA,
inherit.aes = TRUE, ...)
geom_histogram(mapping = NULL, data = NULL, stat = "bin",
binwidth = NULL, bins = NULL, origin = NULL, right = FALSE,
position = "stack", na.rm = FALSE, show.legend = NA,
inherit.aes = TRUE, ...)
stat_bin(mapping = NULL, data = NULL, geom = "bar", position = "stack",
width = 0.9, drop = FALSE, right = FALSE, binwidth = NULL,
bins = NULL, origin = NULL, breaks = NULL, na.rm = FALSE,
show.legend = NA, inherit.aes = TRUE, ...)
}
\arguments{
\item{mapping}{Set of aesthetic mappings created by \code{\link{aes}} or
\code{\link{aes_}}. If specified and \code{inherit.aes = TRUE} (the
default), is combined with the default mapping at the top level of the
plot. You only need to supply \code{mapping} if there isn't a mapping
defined for the plot.}
\item{data}{A data frame. If specified, overrides the default data frame
defined at the top level of the plot.}
\item{position}{Position adjustment, either as a string, or the result of
a call to a position adjustment function.}
\item{na.rm}{If \code{FALSE} (the default), removes missing values with
a warning. If \code{TRUE} silently removes missing values.}
\item{show.legend}{logical. Should this layer be included in the legends?
\code{NA}, the default, includes if any aesthetics are mapped.
\code{FALSE} never includes, and \code{TRUE} always includes.}
\item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics,
rather than combining with them. This is most useful for helper functions
that define both data and aesthetics and shouldn't inherit behaviour from
the default plot specification, e.g. \code{\link{borders}}.}
\item{...}{other arguments passed on to \code{\link{layer}}. There are
three types of arguments you can use here:
\itemize{
\item Aesthetics: to set an aesthetic to a fixed value, like
\code{color = "red"} or \code{size = 3}.
\item Other arguments to the layer, for example you override the
default \code{stat} associated with the layer.
\item Other arguments passed on to the stat.
}}
\item{binwidth}{Bin width to use. Defaults to 1/\code{bins} of the range of
the data}
\item{bins}{Number of bins. Overridden by \code{binwidth} or \code{breaks}.
Defaults to 30}
\item{origin}{Origin of first bin}
\item{right}{If \code{TRUE}, right-closed, left-open, if \code{FALSE},
the default, right-open, left-closed.}
\item{geom, stat}{Use to override the default connection between
\code{geom_histogram}/\code{geom_freqpoly} and \code{stat_bin}.}
\item{width}{Width of bars when used with categorical data}
\item{drop}{If TRUE, remove all bins with zero counts}
\item{breaks}{Actual breaks to use. Overrides bin width, bin number and
origin}
}
\description{
Display a 1d distribution by dividing into bins and counting the number
of observations in each bin. Histograms use bars; frequency polygons use
lines.
\code{stat_bin} is suitable only for continuous x data. If your x data is
discrete, you probably want to use \code{\link{stat_count}}.
}
\details{
By default, \code{stat_bin} uses 30 bins - this is not a good default,
but the idea is to get you experimenting with different binwidths. You
may need to look at a few to uncover the full story behind your data.
}
\section{Aesthetics}{
\code{geom_histogram} uses the same aesthetics as \code{geom_bar};
\code{geom_freqpoly} uses the same aesthetics as \code{geom_line}.
}
\section{Computed variables}{
\describe{
\item{count}{number of points in bin}
\item{density}{density of points in bin, scaled to integrate to 1}
\item{ncount}{count, scaled to maximum of 1}
\item{ndensity}{density, scaled to maximum of 1}
}
}
\examples{
ggplot(diamonds, aes(carat)) +
geom_histogram()
ggplot(diamonds, aes(carat)) +
geom_histogram(binwidth = 0.01)
ggplot(diamonds, aes(carat)) +
geom_histogram(bins = 200)
# Rather than stacking histograms, it's easier to compare frequency
# polygons
ggplot(diamonds, aes(price, fill = cut)) +
geom_histogram(binwidth = 500)
ggplot(diamonds, aes(price, colour = cut)) +
geom_freqpoly(binwidth = 500)
# To make it easier to compare distributions with very different counts,
# put density on the y axis instead of the default count
ggplot(diamonds, aes(price, ..density.., colour = cut)) +
geom_freqpoly(binwidth = 500)
if (require("ggplot2movies")) {
# Often we don't want the height of the bar to represent the
# count of observations, but the sum of some other variable.
# For example, the following plot shows the number of movies
# in each rating.
m <- ggplot(movies, aes(rating))
m + geom_histogram(binwidth = 0.1)
# If, however, we want to see the number of votes cast in each
# category, we need to weight by the votes variable
m + geom_histogram(aes(weight = votes), binwidth = 0.1) + ylab("votes")
# For transformed scales, binwidth applies to the transformed data.
# The bins have constant width on the transformed scale.
m + geom_histogram() + scale_x_log10()
m + geom_histogram(binwidth = 0.05) + scale_x_log10()
# For transformed coordinate systems, the binwidth applies to the
# raw data. The bins have constant width on the original scale.
# Using log scales does not work here, because the first
# bar is anchored at zero, and so when transformed becomes negative
# infinity. This is not a problem when transforming the scales, because
# no observations have 0 ratings.
m + geom_histogram(origin = 0) + coord_trans(x = "log10")
# Use origin = 0, to make sure we don't take sqrt of negative values
m + geom_histogram(origin = 0) + coord_trans(x = "sqrt")
# You can also transform the y axis. Remember that the base of the bars
# has value 0, so log transformations are not appropriate
m <- ggplot(movies, aes(x = rating))
m + geom_histogram(binwidth = 0.5) + scale_y_sqrt()
}
rm(movies)
}
\seealso{
\code{\link{stat_count}}, which counts the number of cases at each x
posotion, without binning. It is suitable for both discrete and continuous
x data, whereas \link{stat_bin} is suitable only for continuous x data.
}