Skip to Content
R Programming Fundamentals
book

R Programming Fundamentals

by Kaelen Medeiros
September 2018
Beginner
206 pages
4h 27m
English
Packt Publishing
Content preview from R Programming Fundamentals

Creating Histograms using ggplot2

In this section, we will create a histogram with ggplot2 and experiment with different binwidths to find the best representation of the data. Follow the steps below:

  1. Install the ggplot2 library and then load it:
install.packages("ggplot2")library(ggplot2)
  1. Load the msleep dataset, a built-in dataset that comes installed with ggplot2, using data("msleep").
  1. Create a histogram of the sleep_total variable from msleep. Do you get the binwidth error?
ggplot(msleep, aes(sleep_total)) + geom_histogram() 
  1. Try the same histogram, but with binwidth = 10. Does the histogram improve?
ggplot(msleep, aes(sleep_total)) + geom_histogram(binwidth = 10)
  1. Try the histogram one more time, now with binwidth = 1:
ggplot(msleep, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

R Programming Fundamentals

R Programming Fundamentals

Samik Sen, Marcin Dubel
Python for Programmers

Python for Programmers

Paul Deitel, Harvey Deitel

Publisher Resources

ISBN: 9781789612998Supplemental Content