Question

I have data which is like this

 Date,     VID, C1,  C2, C3
 2/1/2014, V01, ON,  OFF, OFF 
 2/2/2014, V01, ON,  OFF, OFF 
 2/3/2014, V01, ON,  OFF, OFF 
 2/4/2014, V01, ON,  OFF, OFF 
 2/1/2014, V02, OFF, ON,  OFF 
 2/3/2014, V02, OFF, OFF, ON 
 2/4/2014, V02, ON,  ON,  OFF 
 2/5/2014, V02, ON,  OFF, ON 

There are many VID's And C1, C2,C3 are Categorical. Here is how I plot this & this is very time consuming so wanted to know if i can plot all time series for one VID ib one graph and how do that for all VID's ?

library(sqldf)
library(zoo)

#I execute this code for each variable and get separate plot
#I would like to plot all C1,C2,C3 for each vehicle in single plot
#and plot for each vehicle 
vehicle_V01 = sqldf("select * from df where Vehicle='V01'")
vehicle_V01$C1_numeric = as.numeric(vehicle_V01$C1)
vehicle_V01$C2_numeric = as.numeric(vehicle_V01$C2)
vehicle_V01$C3_numeric = as.numeric(vehicle_V01$C3)
ts = zoo(vehicle_V01$C1_numeric ,vehicle_V01$Date)
plot(ts)

This is very time costuming, any idea how to do it efficiently?

Was it helpful?

Solution

I would use ggplot2 and reshape2 to plot and reshape the data.

library(reshape2)
library(ggplot2)
df=melt(df,id.vars=c('Date','VID'))
df$value=as.numeric(vehicle$value)#ON = 2, OFF = 1

ggplot(df,aes(x=Date,y=value))+geom_line()+facet_grid('variable+VID~.')

The Date value was changed on the last 3 lines to be one less, but the output graphic looks like what you see below. Is this what you're looking for? (Also, I didn't use the date values you used, but you can use them when you run the code or just change the axis labels to them).

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top