theBotelho
theBotelho

Reputation: 324

Transform a time series matrix into ggplot format

How could I transform a time series matrix in the form of

      dDomingo  dSegunda     dTerça   dQuarta    dQuinta    dSexta    dSábado
[1,] -1.783558 0.5123224  0.5997068 0.4680476  0.6521374 0.6276698 -1.0763264
[2,] -1.730086 0.8552685  0.6426731 0.6377859  0.3995325 0.2944567 -1.0996309
[3,] -1.503333 0.9450167 -0.4125656 0.8446433  0.6839187 0.5397113 -1.0973919
[4,] -1.720261 0.3044134  0.6722075 0.5313041  0.6411365 0.7105043 -1.1393050
[5,] -1.234701 0.9189446  0.9670409 0.8713827 -1.1208727 0.3465987 -0.7483936

Into this

           V1     V2
[1,] -1.783558 dDomingo        
[2,]  0.5123224 dSegunda 
[3,]  0.5997068 dTerça 
[4,]  0.4680476 dQuarta
[5,]  0.6521374 dQuinta 
[6,]  0.6276698 dSexta 
[7,] -1.0763264 dSábado
[8,] -1.730086  dDomingo

... and so on until

[35,] -0.7483936  dSábado

Notice that the new data format I would like has only two columns. The first one will contain the variables values from the last matrix. The second column will contain the respective column where the variable values was.

The time series matrix data is the following:

structure(c(-1.78355763661757, -1.73008589314791, -1.50333251627705, 
-1.72026078275706, -1.23470060199216, 0.512322374645035, 0.855268450002498, 
0.945016691336727, 0.304413384239457, 0.91894460806156, 0.599706795271625, 
0.642673149573306, -0.412565627467277, 0.672207513790216, 0.967040903075167, 
0.468047601527563, 0.637785901287577, 0.844643267672228, 0.531304065337568, 
0.871382716325882, 0.652137447647579, 0.39953254735831, 0.683918734842367, 
0.64113649695194, -1.12087270379329, 0.627669809872133, 0.294456709215146, 
0.539711347678816, 0.710504348497859, 0.346598697399641, -1.07632639234637, 
-1.09963086428893, -1.09739189778582, -1.13930502605998, -0.748393619076803
), .Dim = c(5L, 7L), .Dimnames = list(NULL, c("dDomingo", "dSegunda", 
"dTerça", "dQuarta", "dQuinta", "dSexta", "dSábado")))

Upvotes: 0

Views: 1311

Answers (3)

G. Grothendieck
G. Grothendieck

Reputation: 269905

Using m in the Note at the end this base code

stack(as.data.frame(m))

or this

library(zoo)
fortify.zoo(zoo(m), melt = TRUE)

will create a two column data frame with 35 rows; however, note that autoplot.zoo will plot a multivariate zoo time series using ggplot2 without requiring any explicit transformation. Remove facet = NULL if you want each series in a separate panel. See ?autoplot.zoo and ?fortify.zoo for more info and examples.

library(ggplot2)
library(zoo)

autoplot(zoo(m), facet = NULL) + ggtitle("My Series") + xlab("")

screenshot

Note

We assume that the input m is:

m <- structure(c(-1.78355763661757, -1.73008589314791, -1.50333251627705, 
-1.72026078275706, -1.23470060199216, 0.512322374645035, 0.855268450002498, 
0.945016691336727, 0.304413384239457, 0.91894460806156, 0.599706795271625, 
0.642673149573306, -0.412565627467277, 0.672207513790216, 0.967040903075167, 
0.468047601527563, 0.637785901287577, 0.844643267672228, 0.531304065337568, 
0.871382716325882, 0.652137447647579, 0.39953254735831, 0.683918734842367, 
0.64113649695194, -1.12087270379329, 0.627669809872133, 0.294456709215146, 
0.539711347678816, 0.710504348497859, 0.346598697399641, -1.07632639234637, 
-1.09963086428893, -1.09739189778582, -1.13930502605998, -0.748393619076803
), .Dim = c(5L, 7L), .Dimnames = list(NULL, c("dDomingo", "dSegunda", 
"dTerça", "dQuarta", "dQuinta", "dSexta", "dSábado")))

Upvotes: 2

Tung
Tung

Reputation: 28401

You need to convert the matrix to data frame first. Then use tidyr::gather to convert to long format

mat1 <- structure(c(-1.78355763661757, -1.73008589314791, -1.50333251627705, 
  -1.72026078275706, -1.23470060199216, 0.512322374645035, 0.855268450002498, 
  0.945016691336727, 0.304413384239457, 0.91894460806156, 0.599706795271625, 
  0.642673149573306, -0.412565627467277, 0.672207513790216, 0.967040903075167, 
  0.468047601527563, 0.637785901287577, 0.844643267672228, 0.531304065337568, 
  0.871382716325882, 0.652137447647579, 0.39953254735831, 0.683918734842367, 
  0.64113649695194, -1.12087270379329, 0.627669809872133, 0.294456709215146, 
  0.539711347678816, 0.710504348497859, 0.346598697399641, -1.07632639234637, 
  -1.09963086428893, -1.09739189778582, -1.13930502605998, -0.748393619076803
  ), .Dim = c(5L, 7L), .Dimnames = list(NULL, c("dDomingo", "dSegunda", 
  "dTerça", "dQuarta", "dQuinta", "dSexta", "dSábado")))

df <- data.frame(mat1)

library(tidyr)
df %>% 
  gather(key, value)

#>         key      value
#> 1  dDomingo -1.7835576
#> 2  dDomingo -1.7300859
#> 3  dDomingo -1.5033325
#> 4  dDomingo -1.7202608
#> 5  dDomingo -1.2347006
#> 6  dSegunda  0.5123224
#> 7  dSegunda  0.8552685
#> 8  dSegunda  0.9450167
#> 35  dSábado -0.7483936

# rename to whatever you want
df %>% 
  gather(key = "V1", value = "V2")

#>          V1         V2
#> 1  dDomingo -1.7835576
#> 2  dDomingo -1.7300859
#> 3  dDomingo -1.5033325
#> 4  dDomingo -1.7202608
#> 5  dDomingo -1.2347006
#> 6  dSegunda  0.5123224
#> 7  dSegunda  0.8552685
#> 8  dSegunda  0.9450167
#> 35  dSábado -0.7483936

Edit: to go back to the wide format, use tidyr::spread

library(dplyr)

df_long %>% 
  group_by(key) %>% 
  mutate(rowid = row_number()) %>% 
  spread(key, value) %>% 
  select(-rowid)

  dDomingo dSegunda dTerça dQuarta dQuinta dSexta dSábado
     <dbl>    <dbl>  <dbl>   <dbl>   <dbl>  <dbl>   <dbl>
1    -1.78    0.512  0.600   0.468   0.652  0.628  -1.08 
2    -1.73    0.855  0.643   0.638   0.400  0.294  -1.10 
3    -1.50    0.945 -0.413   0.845   0.684  0.540  -1.10 
4    -1.72    0.304  0.672   0.531   0.641  0.711  -1.14 
5    -1.23    0.919  0.967   0.871  -1.12   0.347  -0.748  

Created on 2018-04-15 by the reprex package (v0.2.0).

Upvotes: 2

Andris Prokofjevs
Andris Prokofjevs

Reputation: 42

t(your structure here)

t(structure(c(-1.78355763661757, -1.73008589314791, -1.50333251627705, 
-1.72026078275706, -1.23470060199216, 0.512322374645035, 0.855268450002498, 
0.945016691336727, 0.304413384239457, 0.91894460806156, 0.599706795271625, 
0.642673149573306, -0.412565627467277, 0.672207513790216, 0.967040903075167, 
0.468047601527563, 0.637785901287577, 0.844643267672228, 0.531304065337568, 
0.871382716325882, 0.652137447647579, 0.39953254735831, 0.683918734842367, 
0.64113649695194, -1.12087270379329, 0.627669809872133, 0.294456709215146, 
0.539711347678816, 0.710504348497859, 0.346598697399641, -1.07632639234637, 
-1.09963086428893, -1.09739189778582, -1.13930502605998, -0.748393619076803
), .Dim = c(5L, 7L), .Dimnames = list(NULL, c("dDomingo", "dSegunda", 
"dTerça", "dQuarta", "dQuinta", "dSexta", "dSábado"))))

output

              [,1]       [,2]       [,3]       [,4]       [,5]
dDomingo -1.7835576 -1.7300859 -1.5033325 -1.7202608 -1.2347006
dSegunda  0.5123224  0.8552685  0.9450167  0.3044134  0.9189446
dTerça    0.5997068  0.6426731 -0.4125656  0.6722075  0.9670409
dQuarta   0.4680476  0.6377859  0.8446433  0.5313041  0.8713827
dQuinta   0.6521374  0.3995325  0.6839187  0.6411365 -1.1208727
dSexta    0.6276698  0.2944567  0.5397113  0.7105043  0.3465987
dSábado  -1.0763264 -1.0996309 -1.0973919 -1.1393050 -0.7483936

Upvotes: -1

Related Questions