**50%**off this eBook here

### R Graph Cookbook — Save 50%

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

With more than two million users worldwide, R is one of the most popular open source projects. It is a free and robust statistical programming environment with very powerful graphical capabilities. Analyzing and visualizing data with R is a necessary skill for anyone doing any kind of statistical analysis.

In the previous article by **Hrishi V. Mittal**, author of the book R Graph Cookbook, we saw how to adjust some key parameters in R.

In this article we will cover:

- Choosing plotting point symbol styles and sizes
- Choosing line styles and width
- Choosing box styles
- How to adjust axis annotations and tick marks
- How to format log axes
- Setting graph margins and dimensions

## R Graph Cookbook

Read more about this book |

*(For more resources on R, see here.)*

# 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.

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

Read more about this book |

*(For more resources on R, see here.)*

# Choosing box styles

The styles of various boxes drawn in a graph such as the one around the plotting region and the legend can be adjusted in a similar way to the line styles we saw in the last recipe.

## 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.

## How to do it...

Let's say we want to make an L-shaped box around a graph, such that the default top and right borders are not drawn. We can do so using the *bty* argument in the *par()* command:

par(bty="l")

plot(rnorm(100))

## How it works...

The *bty* argument stands for box type and takes single characters in inverted commas as values. The resulting box resembles the corresponding upper case letter. For example, the default value is *o*, thus giving a box with all four edges. Other possible values are *l*, *7*, *c*, *u*, and *]*. If we do not wish to draw a box at all, we can set *bty* to *n*.

*Note that setting bty to n doesn’t suppress the drawing of axes. If we wish to suppress those too then we would also have to set xaxt and yaxt to n. Alternatively, we can simply set the axes argument to FALSE in the plot() function call.*

## There's more...

Box styles can be controlled in a finer way using the *box()* command. In addition to the *lty* and *lwd* arguments, we can also specify where the box should be drawn using the *which* parameter, *which* can take values of *plot*, *figure*, *inner*, and *outer*.

Let's say we want to draw a graph with an L-shaped box for the plot area and a full box around the figure including the axis annotations and titles, then we can do:

par(oma=c(1,1,1,1))

plot(rnorm(100),bty="l")

box(which="figure")

Note that we had to first set the outer margins by setting the oma argument with the *par()* function. We will learn more about this argument later in this article. If we did not set the outer margins, the box around the figure would be right at the edge of the plot and get cut off because the default margins are set to zero.

# Adjusting axis annotations and tick marks

The default axis settings are often not adequate to deal with all kinds of data. For example, we may wish to change the number of tick marks along an axis or change the orientation of the annotations if they are too long to fit horizontally. In this recipe we will cover some settings which can be used to customize axes as per our requirements.

## 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.

## How to do it...

We can set the *xaxp* and *yaxp* arguments with the *par()* command to specify co-ordinates of the extreme tick marks and the number of intervals between tick marks in the form *c(min,max,n)*.

plot(rnorm(100),xaxp=c(0,100,10))

## How it works...

When *xaxp* or *yaxp* is not specified, R automatically calculates the number of tick marks and their values. By default, R extends the axis limits by adding 4% at each end and then draws an axis which fits within the extended range. This means that even if we set the axis limits using *xlim* or *ylim*, the graph corners don’t exactly correspond with those values. To make sure they do, we need to change the axis style using the *xaxs* argument, which takes one of two possible values: *r* (regular or default) and *i* (internal). We need to set *xaxs* to *i*.

A vector of the form *c(x1, x2, n)* giving the co-ordinates of the extreme tick marks and the number of intervals between tick marks.

## There's more...

To change the orientation of axis value annotations, we need to set the *las* argument of the *par()* command. It takes one of four possible numeric values:

- 0: always parallel to the axis (default)
- 1: always horizontal
- 2: always perpendicular to the axis
- 3: always vertical

We can also use the *axis()* command to make a custom axis by specifying a number of arguments. The basic arguments are:

*side*which takes numeric values (1=below, 2=left, 3=above and 4=right)*at*which takes a vector of co-ordinates where tick marks are to be drawn*labels*which takes a vector of tick mark annotations

We can separately set the line width for the axis lines and the tick marks by passing the *lwd* and *lwd.ticks* arguments respectively. Similarly colors can be set using the *col* and *col.ticks* arguments.

# Formatting log axes

In scientific analysis, we often need to represent data on a logarithmic scale. In this recipe, we will see how we can do this easily in R.

## 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.

## How to do it...

The simplest way to make an axes logarithmic is to use the *log* argument in the *plot()* command:

plot(10^c(1:5),log="y",type="b")

## How it works...

The *log* argument takes character values specifying which axes should be logarithmic: *x* for X axis only, *y* for Y axis only, and *xy* or *yx* for both axes.

## There's more...

We can also set scales to be logarithmic by setting the *xlog* and *ylog* arguments to *TRUE* with the *par()* command. This can be handy if we wish to have the same setting for multiple plots as *par()* applies the settings to all subsequent plots on the same device.

Note that R will not create the plot if our data contains zero or negative values.

# Setting graph margins and dimensions

In this recipe we will learn how to adjust graph margins and dimensions.

## Getting ready

## How to do it...

We can use the *fin* and *pin* arguments of the *par()* command to set the figure region and plot dimensions:

par(fin=c(6,6),

pin=c(4,4))

We can use the *mai* and *omi* arguments to adjust the inner and outer margins respectively:

par(mai=c(1,1,1,1),

omi=c(0.1,0.1,0.1,0.1))

## How it works...

All the previous arguments accept values in inches as a pair of width and height values. The default values for *fin* and *pin* are approximately 7x7 and 5.75x5.15. We have to be careful not to specify bigger values for *pin* than *fin* or we would get an error.

Adjusting *fin* and *pin* is one way of setting the figure margins containing the axis annotations and labels. Another way is to use the *mai* or *mar* arguments. In the example, we used *mai* which takes a vector value in inches, whereas *mar* takes a vector of numerical values in terms of number of lines of margins. It is better to use *mar* or *mai* because they adjust the figure margins irrespective of the figure or plot size.

We can also set an outer margin which is set to zero by default. This margin is useful if we wish to contain the entire graph including axis labels within a box as we saw in an earlier recipe. Like figure margins, outer margins can be set in inches with *omi* or in number of lines of text using *oma*.

R Graphics by Paul Murrell is an excellent reference with visual explanations of how margins work in R.

This talk by Paul Murrell also contains figures from the book explaining the same concepts.

# Summary

This article took a look at choosing plotting point symbol styles and sizes, choosing line styles and width, and choosing box styles. We also saw how to adjust axis annotations and tick marks, how to format log axes and setting graph margins and dimensions.

**Further resources on this subject:**

- Creating Line Graphs in R [article]
- Graphical Capabilities of R [article]
- Adjusting Key Parameters in R [article]
- Organizing, Clarifying and Communicating the R Data Analyses [article]
- Customizing Graphics and Creating a Bar Chart and Scatterplot in R [article]

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

## About the Author :

## Hrishi V. Mittal

Hrishi Mittal has been working with R for a few years in different capacities. He was introduced to the exciting world of data analysis with R when he was working as Senior Air Quality Scientist at King’s College London, where he used R extensively to analyze large amounts of air pollution and traffic data for informing the London Mayor’s Air Quality Strategy. He has experience in various other programming languages, but prefers R for data analysis and visualization. He is actively involved in various R mailing lists, forums and the development of some R packages.

In early 2010, he started Pretty Graph Limited (http://www.prettygraph.com), a software company specializing in web-based data visualization products. The company’s flagship product Pretty Graph uses R as the backend engine for helping researchers and businesses visualize and analyze data. The goal is to bring the power of R to a wider audience by providing a modern graphical user interface which can be accessed by anyone and from anywhere simply using a web browser.

## Post new comment