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 Plot of Two-Dimensional Dat...

Making a Density Plot of Two-Dimensional Data

Problem

You want to plot the density of two-dimensional (2D) data.

Solution

Use stat_density2d(). This makes a 2D kernel density estimate from the data. First we’ll plot the density contour along with the data points (Figure 6-33, left):

# The base plot
p <- ggplot(faithful, aes(x=eruptions, y=waiting))

p + geom_point() + stat_density2d()

It’s also possible to map the height of the density curve to the color of the contour lines, by using ..level.. (Figure 6-33, right):

# Contour lines, with "height" mapped to color
p + stat_density2d(aes(colour=..level..))
Left: points and density contour; right: with ..level.. mapped to colour
Figure 6-33. Left: points and density contour; right: with ..level.. mapped to color

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

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

  

Start a Free 10-Day Trial


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