Graphics Reference
In-Depth Information
If you just want to get a quick look at some data that isn't in a data frame, you can get the same
result by passing in NULL for the data frame and giving ggplot() a vector of values. This would
have the same result as the previous code:
# Store the values in a simple vector
w <- faithful$waiting
ggplot( NULL
NULL , aes(x = w)) + geom_histogram()
By default, the data is grouped into 30 bins. This may be too fine or too coarse for your data.
You can change the size of the bins by using binwidth , or you can divide the range of the data
into a specific number of bins. The default colors—a dark fill without an outline—can make it
difficult to see which bar corresponds to which value, so we'll also change the colors, as shown
in Figure 6-2 .
# Set the width of each bin to 5
ggplot(faithful, aes(x = waiting)) +
geom_histogram(binwidth = 5 , fill = "white" , colour = "black" )
# Divide the x range into 15 bins
binsize <- diff(range(faithful$waiting)) / 15
ggplot(faithful, aes(x = waiting)) +
geom_histogram(binwidth = binsize, fill = "white" , colour = "black" )
Figure 6-2. Left: histogram with binwidth=5, and with different colors; right: with 15 bins
Sometimes the appearance of the histogram will be very dependent on the width of the bins and
where exactly the boundaries between bins are. In Figure 6-3 , we'll use a bin width of 8. In the
version on the left, we'll use the origin parameter to put boundaries at 31, 39, 47, etc., while in
the version on the right, we'll shift it over by 4, putting boundaries at 35, 43, 51, etc.:
Search WWH ::




Custom Search