[R] Enquiries about time plots in R

1 view
Skip to first unread message

Lathouri, Maria

unread,
Nov 12, 2008, 10:40:07 AM11/12/08
to r-h...@r-project.org
Dear all,

My name is Maria Lathouri and I am currently doing a PhD at Imperial College of London about time series of environmental variables in surface waters. 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. 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 or just >plot(pH) and then >lines(pH) where the line connects the points but in x-axis I do not have the DATE. It says Index from 0 to 80.

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?

I know that your time is valuable but I would appreciate it if you could help me.

Thank you very much in advance for your time and help.

I am looking forward to hearing from you.

Sincerely yours
Lathouri Maria

[[alternative HTML version deleted]]

______________________________________________
R-h...@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Gabor Grothendieck

unread,
Nov 12, 2008, 10:51:08 AM11/12/08
to Lathouri, Maria, r-h...@r-project.org
Try this (in reality you would use the commented line
in place of the subsequent line) and then read the three
zoo vignettes and R News 4/1.

library(zoo)
library(chron)
Lines <- "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"

# z <- read.zoo("myfile.dat", header = TRUE, format = "%m/%d/%Y")
z <- read.zoo(textConnection(Lines), header = TRUE, format = "%m/%d/%Y")
plot(z)


On Wed, Nov 12, 2008 at 10:40 AM, Lathouri, Maria
<m.lath...@imperial.ac.uk> wrote:
> Dear all,
>
> My name is Maria Lathouri and I am currently doing a PhD at Imperial College of London about time series of environmental variables in surface waters. 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. 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 or just >plot(pH) and then >lines(pH) where the line connects the points but in x-axis I do not have the DATE. It says Index from 0 to 80.
>
> 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?

______________________________________________

Philipp Pagel

unread,
Nov 12, 2008, 11:06:47 AM11/12/08
to r-h...@r-project.org
> 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

Reply all
Reply to author
Forward
0 new messages