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

R provides many options


Often, R provides several ways to achieve what you want. Let's set up 50 values from -pi to +pi and graph a sine function. We use the seq() command to set up this sequence. Note that R understands the constant Pi, whose value can be obtained using the following command:

pi

The following output is obtained:

[1] 3.141593

Now, we create horizontal and vertical axis points for plotting:

x <- seq(-pi, pi, length = 50)    
y <- sin(x)  
                     
plot(x, y, pch = 17, cex = 0.7, col = "darkgreen") 

Then, we add a line that connects the points:

lines(x, y, col = "darkgreen")  

Let's take a look at the resulting graph:

Now try the following approach, using 1000 axis values in order to create a smooth-looking graph:

x <- seq(-pi, pi, length = 1000)
y <- sin(x)

plot(x, y, type = "l")

The output is as follows:

The argument type = "l" produces connecting lines, but here we have so many points that the graph appears smooth. Other options include the argument type = "o", which produces symbols joined by straight lines, and type = "p", which produces points.

You have been reading a chapter from
R Graph Essentials
Published in: Sep 2014 Publisher: ISBN-13: 9781783554553
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}