Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

6. Summarized Data Distributions > Making a Density Curve

Making a Density Curve

Problem

You want to make a kernel density curve.

Solution

Use geom_density() and map a continuous variable to x (Figure 6-7):

ggplot(faithful, aes(x=waiting)) + geom_density()

If you don’t like the lines along the side and bottom, you can use geom_line(stat="density") (see Figure 6-7, right):

# The expand_limits() increases the y range to include the value 0
ggplot(faithful, aes(x=waiting)) + geom_line(stat="density") +
    expand_limits(y=0)
Left: a kernel density estimate curve with geom_density(); right: with geom_line()
Figure 6-7. Left: a kernel density estimate curve with geom_density(); right: with geom_line()

Discussion

Like geom_histogram(), geom_density() requires just one column from a data frame. For this example, we’ll use the faithful data set, which contains data about the Old Faithful geyser in two columns: eruptions, which is the length of each eruption, and waiting, which is the length of time to the next eruption. We’ll only use the waiting column in this example:


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint