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

Passing parameter values to titles and labels


In the next example, we pass parameter values to the title and the axis labels and create the labels using the paste() command. This technique can be useful for creating titles and labelling automatically from within an R program (usually called a script). Let's create a set of values first using the following commands:

k <- 9
min <- 3
max <- 25
name <- "Mary"

Before we start, try the following code:

paste(name, "'s Mark", sep = "")

You will get the following output:

[1] "Mary's Mark"

The content of the variable name (Mary) was pasted together with the text 's Mark. Each element of the text is separated by commas, while the argument sep = "" ensures that there are no breaks between the variable and the text. That's the way the paste() function works.

Let's start again and enhance the plot. Let's create our plot using colors taken from the Hexadecimal Color Chart. The commands to be used are as follows:

plot(X, Y, type="o", col="#669966", xlab = paste(name, "'s Mark", sep = ""), ylab = paste("Marks from ", min, " to ", max,  sep = "")) 

Now let's create a title:

title(main = paste("Plot ", k, " for ", name, sep = ""), font.main = 2, col.main = "#CC6600")

The following is our graph, with the appropriate labels and title:

The title() command is one way of creating a title. However, by using the main argument, you can also create a title within the plot() command (as shown in the next example). In the following example, we pass the same parameter values to the title and the axis labels. Enter the following syntax on the command line:

plot(X, Y, type = "o", col = "red", main = paste("Plot ", k, " for ", name, sep = ""), pch = 16, cex = 1.4, font.main = 2, col.main = "blue", xlab = paste(name, "'s Mark", sep = ""), ylab = paste("Marks from ", min, " to ", max,  sep = "")) 

As in the previous example, the cex parameter controls the symbol size (the default value is 1). The resulting graph is as follows:

Indeed, we have the correct axis labels and title. You can check out the parameters pch and lty for yourselves.

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}