3

I have a large data frame of several variables (around 50) with first column as date and second column id.

My data roughly look like this:

df <- data.frame(date = c("01-04-2001 00:00","01-04-2001 00:00","01-04-2001 00:00",
                      "01-05-2001 00:00","01-05-2001 00:00","01-05-2001 00:00",
                      "01-06-2001 00:00","01-06-2001 00:00","01-06-2001 00:00",
                      "01-07-2001 00:00","01-07-2001 00:00","01-07-2001 00:00"), 
             id = c(1,2,3,1,2,3,1,2,3,1,2,3), a = c(1,2,3,4,5,6,7,8,9,10,11,12), 
             b = c(2,2.5,3,3.2,4,4.6,5,5.6,8,8.9,10,10.6))

I want time series plots for all three ids separately in same graph of variables, a and b in different graphs.

I tried ggplot but it didn't work. Please help me

Steven Beaupré
  • 21,343
  • 7
  • 57
  • 77
Rajan
  • 453
  • 4
  • 22

3 Answers3

5

Do you mean something like this?

library(reshape)
library(lattice)

df2 <- melt(df, id.vars = c("date", "id"), measure.vars = c("a", "b"))

xyplot(value ~ date | variable, group = id, df2, t='l')

enter image description here

Addendum

# The following is from a comment by jbaums. 
# It will create a single plot/file for each variable of df2
png('plots%02d.png')
xyplot(value ~ date | variable, group = id, df2, t='l', layout=c(1, 1), 
       scales=list(alternating=FALSE, tck=1:0))
dev.off()

You can also add relation='free' to scales so that y-axis limits are calculated separately for each plot.

jbaums
  • 27,115
  • 5
  • 79
  • 119
  • Probs should `as.Date` first, in case the true dataset has missing dates. – jbaums Feb 27 '15 at 06:16
  • yeah, sort of this, but I want to plot it in different windows as I have more than 50 variables. Also want to put legend for different colors. – Rajan Feb 27 '15 at 06:16
  • @Rajan Please clarify. –  Feb 27 '15 at 06:19
  • @jbaums Yes, it should. –  Feb 27 '15 at 06:19
  • Since I have more than 50 variables and they have different maximum and minimum value. I want them as separate so that i can save each variable plot as separate png file – Rajan Feb 27 '15 at 06:31
2

Edit: After reading the comments, maybe you should try something like this:

library(tidyr)
df2 <- gather(df, variable, value, -date, -id)
vars <- unique(df2$variable)

library(ggplot2)
for (i in 1:length(vars)) {
  ggplot() + 
    geom_line(data = subset(df2, variable == vars[[i]]), 
              aes(date, value, group = id, color = factor(id))) +
    ylab(as.character(vars[[i]])) +
    ggsave(file = paste0(vars[[i]], ".png"))
}

This should save a PNG for each variable in your dataframe (and will change y label of every plot to variable name, as per your request)

Steven Beaupré
  • 21,343
  • 7
  • 57
  • 77
  • @ Steven thanks for helping me but .I have more than 50 variables. So its not possible to plot like this. So i want that it plot one after another so that i can save graph for each variable separately – Rajan Feb 27 '15 at 06:55
  • I want to change y label of every plot to variable name by ylab(colnames(df[i]) but its nor working. I am getting this error: "Error in +ylab(colnames(data)[i]) : invalid argument to unary operator". may you please me something – Rajan Feb 27 '15 at 10:00
  • 1
    @Rajan, might be better to access the vars variable since it's part of the for loop. Try `ylab(as.character(vars[[i]]))` – MichaelT Feb 27 '15 at 14:11
1

Here's how to do it in ggplot, using the tidyr package to get it in the right format:

library(ggplot2)
library(tidyr)
library(dplyr)

df <- data.frame(date = c("01-04-2001 00:00","01-04-2001 00:00","01-04-2001 00:00",
                      "01-05-2001 00:00","01-05-2001 00:00","01-05-2001 00:00",
                      "01-06-2001 00:00","01-06-2001 00:00","01-06-2001 00:00",
                      "01-07-2001 00:00","01-07-2001 00:00","01-07-2001 00:00"), 
             id = c(1,2,3,1,2,3,1,2,3,1,2,3), a = c(1,2,3,4,5,6,7,8,9,10,11,12), 
             b = c(2,2.5,3,3.2,4,4.6,5,5.6,8,8.9,10,10.6))

Then using dplyr's group_by and do functions, we can save multiple plots.

df %>%
  gather(variable, value, -date, -id) %>%
  mutate(id = factor(id)) %>%
  group_by(variable) %>%
  do(
    qplot(data = ., x = date, y = value, geom = "line", group = id, color = id, main = paste("variable =", .$variable)) +
          ggsave(filename = paste0(.$variable, ".png")
             )
    ) 
MichaelT
  • 73
  • 5