1

This question is similar to r - data frame of lists into a data frame but no answer was really sufficient.

I have a df of time series data, comparing modeled to gage data. See below for a sub-section of the data:

Date        GageFlow  base0      1a       1b       2a        2b       3a       
2003-10-01     78.6 72.43154 64.55318 64.55318 64.55318 64.55318 64.55318 
2003-10-02     88.6 74.94789 68.48789 68.48789 68.48789 68.48789 68.48789 
2003-10-03     96.6 75.08756 69.75530 69.75530 69.75530 69.75530 69.75530 
2003-10-04     93.1 74.45323 66.67482 66.67482 66.67482 66.67482 66.67482 
2003-10-05     90.2 72.89045 65.24120 65.24120 65.24120 65.24120 65.24120 
2003-10-06     96.0 73.89078 67.88296 67.88296 67.88296 67.88296 67.88296 

The str of my data is:

'data.frame':   3653 obs. of  11 variables:
 $ Date    : Date, format: "2003-10-01" "2003-10-02" ...
 $ GageFlow: num  78.6 88.6 96.6 93.1 90.2 96 96 97.8 98.7 99.8 ...
 $ base0   :'data.frame':   3653 obs. of  1 variable:
  ..$ base0: num  72.4 74.9 75.1 74.5 72.9 ...

I would like to use ggplot to make a plot of the modeled scenarios with geom_lines of (base0, 1a, 1b, etc.) and GageFlow, with the x-axis as the timeseries (x=Date) provided. The problem is that when I use melt to try and get this in a tall format, I receive an error:

Error: Can't melt data.frames with non-atomic 'measure' columns In addition: Warning message: attributes are not identical across measure variables; they will be dropped

Do I actually need to restructure my df or is there some way to melt, or use another function to produce such a plot. Of course I could do this the long way, but I'm trying to become more efficient.

Thanks!

Community
  • 1
  • 1
LauraR
  • 181
  • 1
  • 2
  • 12
  • 2
    Can you edit with the result of `dput(head(df))`? Structure is important here, and that's the best way to preserve it. – alistaire Jun 02 '16 at 22:07
  • 3
    The problem is that the class of variable `base0` is data.frame. If you change it into numeric vector, it should work fine. – JasonWang Jun 02 '16 at 22:18

1 Answers1

6

To solve the problem, you have to transform the base0 variable.

str(df)
## 'data.frame':    6 obs. of  8 variables:
##  $ Date    : Date, format: "2003-10-01" ...
##  $ GageFlow: num  78.6 88.6 96.6 93.1 90.2 96
##  $ base0   :'data.frame':    6 obs. of  1 variable:
##   ..$ df$base0: num  72.4 74.9 75.1 74.5 72.9 ...

# Alternatively, you can use df$base0 <- df$base0[, 1]
# if you only have few columns which classes are data.frame
df[] <- lapply(df, unlist)

str(df)
## 'data.frame':    6 obs. of  8 variables:
##  $ Date    : Date, format: "2003-10-01" ...
##  $ GageFlow: num  78.6 88.6 96.6 93.1 90.2 96
##  $ base0   : num  72.4 74.9 75.1 74.5 72.9 ...

df <- melt(df, "Date")
ggplot(df, aes(x=Date, y=value, color=variable)) + geom_line()

enter image description here

JasonWang
  • 2,414
  • 11
  • 12