Categories: TutorialsData

Choosing Styles of Various Graph Elements in R

4 min read

 

R Graph Cookbook

Detailed hands-on recipes for creating the most useful types of graphs in R – starting from the simplest versions to more advanced applications

  • Learn to draw any type of graph or visual data representation in R
  • Filled with practical tips and techniques for creating any type of graph you need; not just theoretical explanations
  • All examples are accompanied with the corresponding graph images, so you know what the results look like
  • Each recipe is independent and contains the complete explanation and code to perform the task as efficiently as possible

Choosing plotting point symbol styles and sizes

In this recipe, we will see how we can adjust the styling of plotting symbols, which is useful and necessary when we plot more than one set of points representing different groups of data on the same graph.

Getting ready

All you need to try out this recipe is to run R and type the recipe at the command prompt. You can also choose to save the recipe as a script so that you can use it again later on. We will also use the cityrain.csv example data file. Please read the file into R as follows:

rain<-read.csv(“cityrain.csv”)

 

The code file can be downloaded from here.

How to do it…

The plotting symbol and size can be set using the pch and cex arguments:

plot(rnorm(100),pch=19,cex=2)

 

 

How it works…

The pch argument stands for plotting character (symbol). It can take numerical values (usually between 0 and 25) as well as single character values. Each numerical value represents a different symbol. For example, 1 represents circles, 2 represents triangles, 3 represents plus signs, and so on. If we set the value of pch to a character such as “*” or “£” in inverted commas, then the data points are drawn as that character instead of the default circles.

The size of the plotting symbol is controlled by the cex argument, which takes numerical values starting at 0 giving the amount by which plotting symbols should be magnified relative to the default. Note that cex takes relative values (the default is 1). So, the absolute size may vary depending on the defaults of the graphic device in use. For example, the size of plotting symbols with the same cex value may be different for a graph saved as a PNG file versus a graph saved as a PDF.

There’s more…

The most common use of pch and cex is when we don’t want to use color to distinguish between different groups of data points. This is often the case in scientific journals which do not accept color images. For example, let’s plot the city rainfall data as a set of points instead of lines:

plot(rain$Tokyo,
ylim=c(0,250),
main=”Monthly Rainfall in major cities”,
xlab=”Month of Year”,
ylab=”Rainfall (mm)”,
pch=1)

points(rain$NewYork,pch=2)
points(rain$London,pch=3)
points(rain$Berlin,pch=4)

legend(“top”,
legend=c(“Tokyo”,”New York”,”London”,”Berlin”),
ncol=4,
cex=0.8,
bty=”n”,
pch=1:4)

 

Choosing line styles and width

Similar to plotting point symbols, R provides simple ways to adjust the style of lines in graphs.

Getting ready

All you need to try out this recipe is to run R and type the recipe at the command prompt. You can also choose to save the recipe as a script so that you can use it again later on. We will again use the cityrain.csv data file.

How to do it…

Line styles can be set by using the lty and lwd arguments (for line type and width respectively) in the plot(), lines(), and par() commands. Let’s take our rainfall example and apply different line styles keeping the color the same:

plot(rain$Tokyo,
ylim=c(0,250),
main=”Monthly Rainfall in major cities”,
xlab=”Month of Year”,
ylab=”Rainfall (mm)”,
type=”l”,
lty=1,
lwd=2)

lines(rain$NewYork,lty=2,lwd=2)
lines(rain$London,lty=3,lwd=2)
lines(rain$Berlin,lty=4,lwd=2)

legend(“top”,
legend=c(“Tokyo”,”New York”,”London”,”Berlin”),
ncol=4,
cex=0.8,
bty=”n”,
lty=1:4,
lwd=2)

 

 

How it works…

Both line type and width can be set with numerical values as shown in the previous example. Line type number values correspond to types of lines:

  • 0: blank
  • 1: solid (default)
  • 2: dashed
  • 3: dotted
  • 4: dotdash
  • 5: longdash
  • 6: twodash

We can also use the character strings instead of numbers, for example, lty=”dashed” instead of lty=2.

The line width argument lwd takes positive numerical values. The default value is 1. In the example we used a value of 2, thus making the lines thicker than default.

Packt

Share
Published by
Packt

Recent Posts

Top life hacks for prepping for your IT certification exam

I remember deciding to pursue my first IT certification, the CompTIA A+. I had signed…

3 years ago

Learn Transformers for Natural Language Processing with Denis Rothman

Key takeaways The transformer architecture has proved to be revolutionary in outperforming the classical RNN…

3 years ago

Learning Essential Linux Commands for Navigating the Shell Effectively

Once we learn how to deploy an Ubuntu server, how to manage users, and how…

3 years ago

Clean Coding in Python with Mariano Anaya

Key-takeaways:   Clean code isn’t just a nice thing to have or a luxury in software projects; it's a necessity. If we…

3 years ago

Exploring Forms in Angular – types, benefits and differences   

While developing a web application, or setting dynamic pages and meta tags we need to deal with…

3 years ago

Gain Practical Expertise with the Latest Edition of Software Architecture with C# 9 and .NET 5

Software architecture is one of the most discussed topics in the software industry today, and…

3 years ago