Skip to content

Simple time series questions

5 messages · gug, DKOD, Gabor Grothendieck

gug
#
I'm sure this is a really simple problem, but I've spent hours digging and I
keep running into roadblocks.

I'm trying to get a simple chart with three time series.  Similar to the
attached example 
http://www.nabble.com/file/p25398419/Excel%2Bchart%2Bexample.pdf
Excel+chart+example.pdf , something that was quite easy to do in Excel,
except that I need a log y-axis: something that R can do and Excel can't.

The data is in the attached CSV file 
http://www.nabble.com/file/p25398419/test%2Bchart%2Bdata.csv
test+chart+data.csv .  I can read it in OK, and create separate charts:

testdata<- read.table("C:\\Files\\test chart data.csv", head = T, sep =
",",na.strings = "na")
test_date = as.Date(testdata$Date,"%d-%m-%y")
test_data_model = testdata$Model
test_date_baseA = testdata$BaseDataA
test_date_baseB = testdata$BaseDataB
plot(test_date, test_data_model,type='l',log="y")
plot(test_date, test_data_baseA,type='l',log="y")
plot(test_date, test_data_baseB,type='l',log="y")
grid()

(Clearly at this point, each chart over-writes the previous one.)

Next I try to get them onto a single chart, sharing the same y-axis.  I'm
sure I haven't done this very elegantly, but here goes:

frame_model = data.frame(a=test_date,b=test_data_model)
frame_A = data.frame(a=test_date,b=test_data_baseA)
frame_B = data.frame(a=test_date,b=test_data_baseB)
ts_model = ts(frame_model$b)
ts_a = ts(frame_A$b)
ts_b = ts(frame_B$b)
ts.plot(ts_model,ts_a,ts_b,col=c("blue","red","green"),log="y")

The problem is that I no longer have the date along the y-axis.  How can I
get that back?

Finally, when I plot the separate time series, the grid() function geneates
a grid where the vertical lines are not lined up with the year tick marks. 
I interpreted the topic help as saying that by default they would match the
tick marks.  How can I achieve that?

Thanks for any suggestions,

Guy Green
#
This script worked for me. Be sure to put in your correct link.

  link <- "C:\\R_Home\\Charts & Graphs Blog\\R_Chart_Doc\\text_data.csv"
  testdata<- read.table(link, head = T, sep = ",",na.strings = "na")
  test_date = as.Date(testdata$Date,"%d-%m-%y") 

  plot(test_date, testdata$Model, type="l", log="y") 
  points(test_date, testdata$BaseDataA, type = "l", col = "red")
  points(test_date, testdata$BaseDataB, type = "l", col = "blue")

You add 2nd and 3rd series with points command

Hope this helps.

Kelly

http://chartsgraphs.wordpress.com http://chartsgraphs.wordpress.com
gug wrote:
http://chartsgraphs.wordperss.com http://chartsgraphs.wordperss.com
gug
#
Thanks - that works great.

Do you have any suggestions about the grid() problem - i.e. that the
vertical gridlines do not line up with the x-axis tickmarks (which are
years)?

I can't see on what basis the vertical gridlines are being positioned, but
it doesn't look good that they are not lined up with anything.

Thanks,

Guy
DKOD wrote:

  
    
#
Try this script. I converted test_date to numeric decimal year

link <- "C:\\R_Home\\Charts & Graphs Blog\\R_Chart_Doc\\text_data.csv"
  testdata<- read.table(link, head = T, sep = ",",na.strings = "na")
  test_date = as.Date(testdata$Date,"%d-%m-%y")

# Convert dates to decimal year   
  my_yr <- as.numeric(format(test_date,format="%Y"))
  my_mo <- as.numeric(format(test_date, format="%m"))
  dec_yr <- my_yr + (my_mo+0.5)/12

  plot(dec_yr, testdata$Model, type="l", log="y", xaxs="i", yaxs="i",
     axes=T, xlim = c(2003, 2008))
  points(dec_yr, testdata$BaseDataA, type = "l", col = "red")
  points(dec_yr, testdata$BaseDataB, type = "l", col = "blue")
  grid( col="grey",lty=1)
  box()

Kelly

http://chartsgraphs.wordpress.com http://chartsgraphs.wordpress.com
gug wrote:

  
    
#
Here it is using zoo and classic graphics.

1. Just paste this into your R session:

library(zoo)

URL <- "http://www.nabble.com/file/p25398419/test%2Bchart%2Bdata.csv"
z <- read.zoo(URL, header = TRUE, format = "%d-%m-%y", sep = ",")

cols <-  c("green", "red", "blue")
plot(z, screen = 1, col = cols)

# 2. Or try this fancier version using the same code
# replacing the plot statement above with all this

plot(z, screen = 1, col = cols, xaxt = "n")

legend("topleft", c("A", "B", "C"), col = cols, lty = 1)

# fancy X axis (code is from ?plot.zoo page)
ym <- as.yearmon(time(z))
mon <- as.numeric(format(ym, "%m"))
yy <- format(ym, "%y")
mm <- substring(month.abb[mon], 1, 1)
Axis(side = 1, at = time(z)[mon == 1], labels = yy[mon == 1], cex.axis = 0.7)
Axis(side = 1, at = time(z)[mon > 1], labels = mm[mon > 1], cex.axis =
0.5, tcl = -0.3)

abline(v = time(z)[mon == 1], col = grey(0.9))
abline(h = axTicks(2), col = grey(0.9))
On Fri, Sep 11, 2009 at 9:07 AM, DKOD <koday at processtrends.com> wrote: