Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R ggplot2 plot several time series in a plot

After succeeding (with your help) in plotting meteo variables in single (one variable) graphs I'm trying to produce a panel with time series of the different variables in my data in a single panel, like the one in ggplot2 webpage example. I have tried to reproduce that example (the last graph at bottom of the page) with my data but without success

My data span for several years but I attach just a month. You can see the output of dput(datos) at http://ubuntuone.com/42j1RqUmNmxUuCppW4gElX

and this is the code I'm trying

datos=read.csv("paterna.dat",sep=";",header=T,na.strings="-99.9")
dm=melt(datos,id="FECHA.H_SOLAR")

datos$PRECIP[is.na(datos$PRECIP)]=0
dm=melt(datos,id="FECHA.H_SOLAR")
qplot(date, value, data = dm, geom = "line", group = variable) +  facet_grid(variable ~ ., scale = "free_y") 
Error: geom_line requires the following missing aesthetics: x
Además: Mensajes de aviso perdidos
1: In min(x) : ningún argumento finito para min; retornando Inf
2: In max(x) : ningun argumento finito para max; retornando -Inf

I try qplot as it appears in the cited example but maybe it's better to use ggplot and set the aesthetics. Then I could also customize axes.

Thanks in advance

like image 334
pacomet Avatar asked Oct 19 '11 11:10

pacomet


1 Answers

the issue is twofold. First of all there's no date object within the melted data.frame, which gives you the error message. Second, your FECHA.H_SOLAR is a factor which would make hard plotting the dates correctely. So here is my solution:

datos <- source("http://ubuntuone.com/42j1RqUmNmxUuCppW4gElX")[[1]]
library(reshape2)
library(ggplot2)

datos$PRECIP[is.na(datos$PRECIP)] <- 0
dm <- melt(datos,id="FECHA.H_SOLAR")
# change FECHA.H_SOLAR to POSIXct so you get your dates right
dm$Fecha <- as.POSIXct(dm$FECHA.H_SOLAR, "%y/%m/%d %H:%M:%S", tz = "")
qplot(Fecha, value, data = dm, geom = "line", group = variable) +
  facet_grid(variable ~ ., scale = "free_y")

enter image description here

Hope it Helps

like image 171
Luciano Selzer Avatar answered Oct 04 '22 16:10

Luciano Selzer