Dr. Mark Gardener

Data Analysis Publications Courses About

On this page...

Line Plots

Plot types

Time series

Custom axes

Home > Data Analysis > Using R - Graphs 3 (Line plots and custom axes)

Using R for statistical analyses - Graphs 3

This page is intended to be a help in getting to grips with the powerful statistical program called R. It is not intended as a course in statistics (see here for details about those). If you have an analysis to perform I hope that you will be able to find the commands you need here and copy/paste them into R to get going.

On this page learn how to create line plots and add custom axes for graphs.

What is R? | Topic Navigation Index| R Tips, Tricks and Hints | MonogRaphs | Go to 1st Topic

I run courses in using R; these may be held at various locations:

If you are interested then see our Courses page or contact us for details.

My publications about R and Data Science

See my books about R and Data Science on my Publications page

I have more projects in hand - visit my Publications page from time to time. You might also like my random essays on selected R topics in MonogRaphs. See also my Writer's Bloc page, details about my latest writing project including R scripts developed for the book.

Skip directly to the 1st topic

R is Open Source
R is Free

Get R at the R Project Page

What is R?

R is an open-source (GPL) statistical environment modeled after S and S-Plus. The S language was developed in the late 1980s at AT&T labs. The R project was started by Robert Gentleman and Ross Ihaka (hence the name, R) of the Statistics Department of the University of Auckland in 1995. It has quickly gained a widespread audience. It is currently maintained by the R core-development team, a hard-working, international team of volunteer developers. The R project web page is the main site for information on R. At this site are directions for obtaining the software, accompanying packages and other sources of documentation.

R is a powerful statistical program but it is first and foremost a programming language. Many routines have been written for R by people all over the world and made freely available from the R project website as "packages". However, the basic installation (for Linux, Windows or Mac) contains a powerful set of tools for most purposes.

Because R is a programming language it can seem a bit daunting; you have to type in commands to get it to work. However, it does have a Graphical User Interface (GUI) to make things easier. You can also copy and paste text from other applications into it (e.g. word processors). So, if you have a library of these commands it is easy to pop in the ones you need for the task at hand. That is the purpose of this web page; to provide a library of basic commands that the user can copy and paste into R to perform a variety of statistical analyses.


Navigation index


Getting started with R:

What is R?
Data files
Inputting data
Seeing your data in R
What data are loaded?
Removing data sets
Help and Documentation


More about manipulating data and entering data without using a spreadsheet:

Making Data
Combine command
Types of Data
Entering data with scan()
Multiple variables
More types of data
Variables within data
Transposing data
Making text columns
Missing values
Stacking data
Selecting columns
Naming columns
Unstacking data

Help and Documentation

A short section on how to find more help with R


Basic Statistics

Some statistical tests:

Basic stats

Variance unequal
Variance Equal
Paired t-test
T-test Step by Step

Two sample test
Paired test
U-test Step by Step

Paired tests
T-test: see T-test
Wilcoxon: see U-test

Chi Squared
Yates Correction for 2x2 matrix
Chi-Squared Step by Step

Goodness of Fit test
Goodness of Fit Step by Step

Non-Parametric stats

Stats on multiple samples when you have non-parametric data.

Kruskal Wallis test
Kruskal-Wallis Stacked
Kruskal Post-Hoc test
Studentized Range Q
Selecting sub-sets
Friedman test
Friedman post-hoc
Rank data ANOVA



Getting started with correlation and a basic graph:

Correlation and Significance tests
Graphing the Correlation
Correlation step by step


Multiple regression analysis:

Multiple Regression
Linear regression models
Regression coefficients
Beta coefficients
R squared
Graphing the regression
Regression step by step


Analysis of variance:

ANOVA analysis of variance
Simple Post-hoc test
ANOVA Models
ANOVA Step by Step



Getting started with graphs, some basic types:

Bar charts
Stacked bars
Frequency plots
Horizontal bars


Box-whisker plots
Single sample
Horizontal plot


More graphical methods:

Scatter plot

Stem-Leaf plots

Pie charts


More advanced graphical methods:

Line Plots
Plot types
Time series
Custom axes


Navigation Index

Line plots

Previously you learnt about bar charts (incl. histograms), box-whisker plots and scatter graphs. However, there may be occasions when you wish to display data as a line, perhaps to show a time series. There is no specific lineplot command in R so you must use other graph types and coerce the program to produce your line.


Navigation Index


Use type ="b" to produce a plot with both points and lines e.g. plot(x, y, type= "b")

Plot types

If you produce a plot you generally get a series of points. The default symbol for the points is an open circle but you can alter it using the pch= n parameter (see the section on scatter plots). Actually the points are only one sort of plot type that you can achieve in R (the default). You can use the parameter type = 'type" to create other plots.

Command Plot type
type = "p" Produces points.
type = "l" Produces line segments.
type = "b" Produces points joined by line segments.
type = "o" Similar to "b" but the points are overlaid onto the line.
type = "n" Produces a graph with nothing it it! This can be used to create a graph frame that you add lines to later.

So for example you can type: plot (x, y, type = "b") to produce a simple line plot with added points.


Navigation Index

Time Series

Rather than an series of x, y data you may have a single time series. Here is an example of data to illustrate.

> vostok


Here you have mean monthly temperatures for an Antarctic research station. The file was read in using the standard read.csv() command and contains two columns; month is a factor and temp is a numeric variable (see the section on data types for more information).

If you attempt to plot the whole variable e.g. plot(temp ~ month) you get a horrid mess (try it and see). This is because the month is a factor and cannot be represented on an x,y scatter plot.

However, if you plot the temperature alone you get the beginnings of something sensible:

> plot(temp)

So far so good. There appear to be a series of points and they are in the correct order. You can easily join the dots to make a line plot by adding (type= "b") to the plot command (see the section on plot types). Notice how R has used default labels for the axes, temp for the y-axis is taken from the values in the variable but index is used for the x-axis because you have no reference (you only plotted a single variable).

What you need to do next is to alter the x-axis to reflect your month variable.

The axis() command can be used to make custom axes


Navigation Index

Custom axes

When you look at the time series plot produced above you see that the x-axis needs a bit of work. Since the plot was made from a single variable (temp) there are no values for x and R substitutes a numeric index.

You need to scrap the current axes and start again with your own. It is simple to produce a plot with no axes, merely add (axes= F) to the plot command like so:

> plot(temp, axes= F)

However, R appends default labels to the axes so you need to get rid of those too:

> plot(temp, axes= F, xlab= "", ylab= "")

That does the job. You are going to add axis labels of course so could have specified them now but I used the "" double (double) quotes to illustrate how to produce blank ones (setting xlab= F produces a label FALSE so you have to use "").

To add an axis you use the axis() command. Axis 1 is the bottom of the plot (i.e. the x-axis), axis 2 is the left side of the plot (the y-axis). You can also specify the top (3) and the right side (4) if you wish. In it's simplest form axis(n) adds in the axis specified with it's default parameters. This won't do here because the default x-axis contains only index information. You need to tell R where to find the labels associated with the axis.

To generate an axis you need to specify the length of it and the labels to be used. Here is what you need for the temperature example:

> axis(1, at = 1:length(temp), labels = month)


Oops, that doesn't look right. The 1 specified the bottom (x) axis so that is okay, the at= part specified the length (this is right, there are 12) but the months ar not displayed, you get numerical values instead.

The problem is that the month variable is not plain text but is regarded as a factor. The types of data are covered in the section on manipulating data. You have three sensible ways of altering the month column into a text variable.

You can read the original CSV data file in with an extra command to regard the month column as text (see the section on reading text from CSV files). To do that you would append as.is = 1 to the command (assuming that the month column was the first one) so:

> vostok = read.csv(file.choose(), as.is = 1)

You could read the month column as row names instead:

> vostok = read.csv(file.choose(), row.names= 1)

As a last resort you could create a new vector of text and type the names. This is rather tedious but if there were only a few it might be worth considering. See the section on manipulating data for ways to do this.

Assuming that you chose the option of setting the column as text using the as.is parameter you can now re-run our axis command so:

> axis(1, at = 1:length(temp), labels = month)

If you had set the month column to be row names you would modify the axis() command slightly:

> axis(1, at = 1:length(temp), labels = row.names(vostok))

Now you need to add in the y-axis and the axis labels. You could also add a title and perhaps the whole thing would look better if the dots were joined up to make a lineplot (which was after all the point of the exercise). Here is the whole series of commands from start to finish.

> vostok= read.csv(file.choose(), as.is= 1)
> attach(vostok)
> plot(temp, axes=F, xlab="", ylab= "", type= "b")
> axis(1, at = 1:length(temp), labels = month)
> axis(2)
> title(main= "Time Series", font.main=4, xlab= "Month", ylab= "Mean Temp C")
> box()
> detach(vostok)

The box() command merely adds a border around the plot. This looks a lot better. It is possible to alter the plot character and the colour of the lines, see the section on scatter plots for more information.

Data Analysis Home Back to Graphs 2 (Scatter, Stem, Pie) | MonogRaphs | R Tips & Tricks
Main Homepage