Book Image

R Graph Essentials

Book Image

R Graph Essentials

Overview of this book

Table of Contents (11 chapters)
R Graph Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating bar charts


Now, let's use qplot to produce a frequency bar chart; in this case, for the categorical variable TREATMENT. The heights of the bars give the counts of patients receiving each treatment. We choose a nice hue of brown from the Hexadecimal Color Chart. To create a bar chart, we use geom = "bar". Enter the following syntax:

qplot(TREATMENT, data = T, geom = "bar",  binwidth = 5, xlab = "HEIGHT (cm)", ylab = "FREQUENCY", fill = I("#CC6600"), color = I("blue"))

Here is our bar chart:

The following is a more complex example involving bar charts. We set up a new dataset relating to dinners purchased by two people at fast food outlets during one week. Enter the following syntax into R:

dinners = data.frame(person=c("Thomas", "Thomas", "Thomas", "James", "James"), meal = c("curry", "stew", "salad", "fish", "stir-fry"), price = c(15, 18, 12, 25, 13))

dinners

The output is as follows:

  person     meal  price
1 Thomas    curry    15
2 Thomas     stew    18
3 Thomas    salad    12...