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

3. Bar Graphs > Making a Bar Graph of Counts

Making a Bar Graph of Counts

Problem

Your data has one row representing each case, and you want plot counts of the cases.

Solution

Use geom_bar() without mapping anything to y (Figure 3-7):

ggplot(diamonds, aes(x=cut)) + geom_bar()
# Equivalent to using geom_bar(stat="bin")
Bar graph of counts
Figure 3-7. Bar graph of counts

Discussion

The diamonds data set has 53,940 rows, each of which represents information about one diamond:

diamonds

       carat       cut color clarity depth table price    x    y    z
 1      0.23     Ideal     E     SI2  61.5    55   326 3.95 3.98 2.43
 2      0.21   Premium     E     SI1  59.8    61   326 3.89 3.84 2.31
 3      0.23      Good     E     VS1  56.9    65   327 4.05 4.07 2.31
  ...
 53939  0.86   Premium     H     SI2  61.0    58  2757 6.15 6.12 3.74
 53940  0.75     Ideal     D     SI2  62.2    55  2757 5.83 5.87 3.64

  

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