1

I have prepared a data frame ordered according to date and time as.POSIXlt:

             Date_Time A1    B2 C1    E2
46 24/06/2012 12:20:00 NA 5.515 20    NA
47 24/06/2012 13:20:00 41    NA NA 3.519
48 25/06/2012 14:00:00 NA    NA NA    NA
49 25/06/2012 14:20:00 30    NA 30    NA
50 27/06/2012 15:20:00 NA    71 NA    NA
51 28/06/2012 18:00:00 11    NA 55    11
... ...

As you can see it contains a lot of 'NA' values. Is it possible to plot all data in a single plot with the 'x-axis' showing month-year and different colors assigned for different data (e.g. green for A1, blue for C1)? I tried 'ggplot2' package which removes all missing values. Trying 'zoo' package doesn't allow x-axis to be plotted as month-year. Any suggestion?

1 Answer 1

2

This should not be problematic for any of R's plotting packages. I'm not a ggplot user, but here's a base R example using a slightly modified version of your data. Note that using as.POSIXct is preferable to using as.POSIXlt within data.frame objects:

dat <- read.csv(text="Date_Time,A1,B2,C1,E2
24/06/2012 12:20:00,NA,5.515,20,NA
24/06/2012 13:20:00,41,NA,NA,3.519
25/06/2012 14:00:00,NA,NA,NA,NA
25/07/2012 14:20:00,30,NA,30,NA
27/08/2012 15:20:00,NA,71,NA,NA
28/09/2012 18:00:00,11,NA,55,11")

dat$Date_Time <- as.POSIXct(dat$Date_Time,format="%d/%m/%Y %H:%M:%S")

matplot(
  dat[,1],
  dat[2:5],
  type="o",
  pch=19,
  col=c("green","red","blue","black"),
  xaxt="n",
  xlab="Year-Month",
  ylab="Value"
)

Add a POSIXct represented axis with a chosen format:

axis.POSIXct(side=1,x=dat$Date_Time,format="%Y-%m")

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

thanks, but say you have time-series data for more than 50 variables, how would you give the legend?
@ToNoY - well, if you were doing this with 50 variables, that would be totally unreadable and a legend wouldn't help at all. Adding a legend is pretty straight-forward otherwise though - see ?legend and the examples at the end of the help page.
There's another problem, due to NA values, I can't plot it as a line which is very important to see the trend when you have at least a number of different variables. Even if I use 'matlines' after plot with the same data, it doesn't seem to work! Any solution??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.