Creating histograms with qplot

Now, let's learn how to create a histogram using geom = "histogram" and control the bin width using the argument binwidth. In this example, we will create a histogram of the heights of the patients and select a bin width of 10 cm. The syntax is as follows:

qplot(HEIGHT, geom = "histogram",  ylab = "COUNT", xlab = "HEIGHT", binwidth = 10)

Here is our histogram:

Creating histograms with qplot

Our histogram has a rather chunky appearance, and we may wish to change the color and other attributes of the default histogram. By the way, note what happens if we use the syntax color = I("blue"):

qplot(HEIGHT, geom = "histogram",  ylab = "COUNT", xlab = "HEIGHT", binwidth = 10, color = I("blue"), fill = I("wheat"))

Now the histogram looks like this:

Creating histograms with qplot

We get blue outlines for the bars and the axes. Remember that, for histograms, the color argument controls the color of the histogram outlines, while the argument fill controls the color of the bars.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset