6 1. Summarizing Data Sepal.Length 0 5 10 15 20 4 5 6 7 8 Histogram of iris$Sepal.Length iris$Sepal.Length 4 5 6 7 8 Figure 1.1. Comparing two histogram functions: histogram() on the left, and hist() on the right. the two histograms differ because they use slightly different algorithms for choosing the default break points. The user can, of course, override the default break points (using the breaks argument). There is a third scale, called the density scale, that is often used for the vertical axis. This scale is designed so that the area of each bar is equal to the proportion of data it represents. This is especially useful for histograms that have bins (as the intervals between break points are typically called in the context of histograms) of different widths. Figure 1.2 shows an example of such a histogram generated using the following code: iris-histo-density histogram(~Sepal.Length,data=iris,type="density", + breaks=c(4,5,5.5,6,6.5,7,8,10)) We will generally use the newer histogram() function because it has several nice features. One of these is the ability to split up a plot into subplots called panels. For example, we could build a separate panel for each species in the iris data set. Figure 1.2 suggests that part of the variation in sepal length is associated with the differences in species. Setosa are generally shorter, virginica longer, and versicolor intermediate. The right-hand plot in Figure 1.2 was created using iris-condition histogram(~Sepal.Length|Species,data=iris) If we only want to see the data from one species, we can select a subset of the data using the subset argument. iris-histo-subset histogram(~Sepal.Length|Species,data=iris, + subset=Species=="virginica") Sepal.Length 0.0 0.1 0.2 0.3 0.4 4 5 6 7 8 9 10 Sepal.Length 0 10 20 30 40 50 4 5 6 7 8 setosa 4 5 6 7 8 versicolor 4 5 6 7 8 virginica Figure 1.2. Left: A density histogram of sepal length using unequal bin widths. Right: A histogram of sepal length by species. Percent of Total Frequency 0 5 10 20 30 Density Percent of Total
Previous Page Next Page