Search icon
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
R Graph Essentials

You're reading from  R Graph Essentials

Product type Book
Published in Sep 2014
Publisher
ISBN-13 9781783554553
Pages 190 pages
Edition 1st Edition
Languages

Table of Contents (11) Chapters

R Graph Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Base Graphics in R – One Step at a Time 2. Advanced Functions in Base Graphics 3. Mastering the qplot Function 4. Creating Graphs with ggplot 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...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}