[R] Enquiries about time plots in R

Philipp Pagel p.pagel at wzw.tum.de
Wed Nov 12 17:06:47 CET 2008


> I am trying to do some time plots with R but it seems that it
> is not working.  The problem is that the time is not regular,
> for example, a small dataset can be seen below:

> Date              pH
> 1/02/1998      5.5
> 5/03/1998      5
> 8/12/1998      6
> 1/02/1999      6.1
> ...
> 08/10/2000    7.2


> I have used the function >plot(DATE, pH) and then >lines(pH),
> but the line does not join the points. 

lines() needs the same arguments as plot().

> I have used many combinations, e.g. >plot(DATE, pH) and then
> >lines(DATE, pH), where I get a plot with full of lines inside

You are on the right track here. You probably want something like 

plot(Date, ph, type="l")

or

plot(Date, ph, type="b")

R will plot in the order of appearance, so you may want to sort
the data.frame before plotting.

> x-axis I do not have the DATE. It says Index from 0 to 80. 

Have a look at the structure of your data. str() will do that for
you. I suspect that Date is a factor rather than a Date. Use
as.Date() to fix that.

> Do you know if there is a function where I can have in x-axis
> the DATE (irregular dates) and in y-axis the variable with a
> continuous line, showing how it changes through time?

So in summary you probably want something like this:

# make some example data
df <- data.frame(
	d=c('2008-10-01','2008-10-12','2008-10-5','2008-10-31'),
	y=c(12.5, 17, 13.6, 23.345)
	)

# convert to Date, sort and plot
df$d <- as.Date(df$d)
df <- df[order(df$d), ]
plot(df, type='l')

cu
	Philipp


-- 
Dr. Philipp Pagel
Lehrstuhl für Genomorientierte Bioinformatik
Technische Universität München
Wissenschaftszentrum Weihenstephan
85350 Freising, Germany
http://mips.gsf.de/staff/pagel



More information about the R-help mailing list