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 Basic Box Plot

Making a Basic Box Plot

Problem

You want to make a box (or box-and-whiskers) plot.

Solution

Use geom_boxplot(), mapping a continuous variable to y and a discrete variable to x (Figure 6-15):

library(MASS) # For the data set

ggplot(birthwt, aes(x=factor(race), y=bwt)) + geom_boxplot()
# Use factor() to convert numeric variable to discrete
A box plot
Figure 6-15. A box plot

Discussion

For this example, we used the birthwt data set from the MASS library. It contains data about birth weights and a number of risk factors for low birth weight:

birthwt

low age lwt race smoke ptl ht ui ftv  bwt
  0  19 182    2     0   0  0  1   0 2523
  0  33 155    3     0   0  0  0   3 2551
  0  20 105    1     1   0  0  0   1 2557
...

  

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