R ggplot2 displays multiple time series in a plot

After you have successfully (with your help) in constructing meta variables in single (single variable) graphs, I try to create a panel with time series of different variables in my data in one panel, for example, as an example of the ggplot2 web page . I tried to reproduce this example (the last graph at the bottom of the page) with my data, but without success

My data is spread over several years, but I attach only 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 am trying to use qplot as shown in the above example, but it might be better to use ggplot and set the aesthetics. Then I could also adjust the axis.

Thanks in advance

+7
source share
1 answer

The problem is twofold. First of all, there is no date object in the molten data.frame , which gives you an error message. Secondly, your FECHA.H_SOLAR is a factor that FECHA.H_SOLAR it difficult to correctly print dates. 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 this helps

+6
source

All Articles