Search code examples
rggplot2reshape2facet-grid

Why is the Facet_Grid Output Different from Its Data


I have these time series data which I plotted as follows

## simulate ARIMA(1, 0, 0)
set.seed(289805)
x1 <- arima.sim(n = 10, model = list(ar = 0.8, order = c(1, 0, 0)), sd = 1)
set.seed(671086)
x2 <- arima.sim(n = 10, model = list(ar = 0.9, order = c(1, 0, 0)), sd = 1)
set.seed(799837)
x3 <- arima.sim(n = 10, model = list(ar = 0.95, order = c(1, 0, 0)), sd = 1)
set.seed(289805)
x4 <- arima.sim(n = 10, model = list(ar = 0.8, order = c(1, 0, 0)), sd = 3)
set.seed(671086)
x5 <- arima.sim(n = 10, model = list(ar = 0.9, order = c(1, 0, 0)), sd = 3)
set.seed(799837)
x6 <- arima.sim(n = 10, model = list(ar = 0.95, order = c(1, 0, 0)), sd = 3)
set.seed(289805)
x7 <- arima.sim(n = 10, model = list(ar = 0.8, order = c(1, 0, 0)), sd = 5)
set.seed(671086)
x8 <- arima.sim(n = 10, model = list(ar = 0.9, order = c(1, 0, 0)), sd = 5)
set.seed(799837)
x9 <- arima.sim(n = 10, model = list(ar = 0.95, order = c(1, 0, 0)), sd = 5)
set.seed(289805)
x10 <- arima.sim(n = 10, model = list(ar = 0.8, order = c(1, 0, 0)), sd = 10)
set.seed(671086)
x11 <- arima.sim(n = 10, model = list(ar = 0.9, order = c(1, 0, 0)), sd = 10)
set.seed(799837)
x12 <- arima.sim(n = 10, model = list(ar = 0.95, order = c(1, 0, 0)), sd = 10)
xx <- 1:10


# ggplot for x1
plot1 <- ggplot2::ggplot(NULL, aes(y = x1, x = xx)) +  ggplot2::geom_line(color = "#F2AA4CFF") + ggplot2::geom_point(color = "#101820FF") + xlab('lb') + ylab('RMSE') +  ggplot2::theme_bw()+ ggplot2::scale_y_continuous(expand = c(0.0, 0.00))

# ggplot for x2
plot2 <- ggplot2::ggplot(NULL, aes(y = x2, x = xx)) +  ggplot2::geom_line(color = "#F2AA4CFF") + ggplot2::geom_point(color = "#101820FF") + xlab('lb') + ylab('RMSE') +  ggplot2::theme_bw()+ ggplot2::scale_y_continuous(expand = c(0.0, 0.00))

# ggplot for x3
plot3 <- ggplot2::ggplot(NULL, aes(y = x3, x = xx)) +  ggplot2::geom_line(color = "#F2AA4CFF") + ggplot2::geom_point(color = "#101820FF") + xlab('lb') + ylab('RMSE') +  ggplot2::theme_bw()+ ggplot2::scale_y_continuous(expand = c(0.0, 0.00))

# ggplot for x4
plot4 <- ggplot2::ggplot(NULL, aes(y = x4, x = xx)) +  ggplot2::geom_line(color = "#F2AA4CFF") + ggplot2::geom_point(color = "#101820FF") + xlab('lb') + ylab('RMSE') +  ggplot2::theme_bw()+ ggplot2::scale_y_continuous(expand = c(0.0, 0.00))

# ggplot for x5
plot5 <- ggplot2::ggplot(NULL, aes(y = x5, x = xx)) +  ggplot2::geom_line(color = "#F2AA4CFF") + ggplot2::geom_point(color = "#101820FF") + xlab('lb') + ylab('RMSE') +  ggplot2::theme_bw()+ ggplot2::scale_y_continuous(expand = c(0.0, 0.00))

# ggplot for x6
plot6 <- ggplot2::ggplot(NULL, aes(y = x6, x = xx)) +  ggplot2::geom_line(color = "#F2AA4CFF") + ggplot2::geom_point(color = "#101820FF") + xlab('lb') + ylab('RMSE') +  ggplot2::theme_bw()+ ggplot2::scale_y_continuous(expand = c(0.0, 0.00))

# ggplot for x7
plot7 <- ggplot2::ggplot(NULL, aes(y = x7, x = xx)) +  ggplot2::geom_line(color = "#F2AA4CFF") + ggplot2::geom_point(color = "#101820FF") + xlab('lb') + ylab('RMSE') +  ggplot2::theme_bw()+ ggplot2::scale_y_continuous(expand = c(0.0, 0.00))

# ggplot for x8
plot8 <- ggplot2::ggplot(NULL, aes(y = x8, x = xx)) +  ggplot2::geom_line(color = "#F2AA4CFF") + ggplot2::geom_point(color = "#101820FF") + xlab('lb') + ylab('RMSE') +  ggplot2::theme_bw()+ ggplot2::scale_y_continuous(expand = c(0.0, 0.00))

# ggplot for x9
plot9 <- ggplot2::ggplot(NULL, aes(y = x9, x = xx)) +  ggplot2::geom_line(color = "#F2AA4CFF") + ggplot2::geom_point(color = "#101820FF") + xlab('lb') + ylab('RMSE') +  ggplot2::theme_bw()+ ggplot2::scale_y_continuous(expand = c(0.0, 0.00))

# ggplot for x10
plot10 <- ggplot2::ggplot(NULL, aes(y = x10, x = xx)) +  ggplot2::geom_line(color = "#F2AA4CFF") + ggplot2::geom_point(color = "#101820FF") + xlab('lb') + ylab('RMSE') +  ggplot2::theme_bw()+ ggplot2::scale_y_continuous(expand = c(0.0, 0.00))

# ggplot for x11
plot11 <- ggplot2::ggplot(NULL, aes(y = x11, x = xx)) +  ggplot2::geom_line(color = "#F2AA4CFF") + ggplot2::geom_point(color = "#101820FF") + xlab('lb') + ylab('RMSE') +  ggplot2::theme_bw()+ ggplot2::scale_y_continuous(expand = c(0.0, 0.00))


# ggplot for x12
plot12 <- ggplot2::ggplot(NULL, aes(y = x12, x = xx)) +  ggplot2::geom_line(color = "#F2AA4CFF") + ggplot2::geom_point(color = "#101820FF") + xlab('lb') + ylab('RMSE') +  ggplot2::theme_bw()+ ggplot2::scale_y_continuous(expand = c(0.0, 0.00))


# plot in a 3 by 5 grid by using plot_layout
plot1 + plot2 + plot3 + plot4 + plot5 + plot6 + plot7 + plot8 + plot9 + plot10 + plot11 + plot12 + patchwork::plot_layout(ncol = 3, byrow = TRUE)

Grid Arranged Plot

I now want to give it a face-lift with facet_grid which I tried as follows

df <- data.frame(xx, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12)
reshapp <- reshape2::melt(df, id = "xx")

NEWDAT <- data.frame(y = reshapp$value, x = reshapp$xx, sd = rep(c(1,3,5,10), each = 3), phi = rep(c(.8, .9, .95), 4))

ggplot(NEWDAT, aes(x = x, y = y)) +  geom_line() +  geom_point() + facet_grid(sd ~ phi,  scales = "free_y") +

theme_bw() + ggplot2::scale_y_continuous(expand = c(0.0, 0.00))

Facet_Grid Plot

WHAT I WANT

How do I make the Facet_Grid Plot to be what Grid_Arranged Plot is?

POINTER

One can see that the lowest data point in the first series is -0.2505125 while the highest is 4.6956397. In the Facet-Grid Plot the lowest is -20+ and the highest is 30+.


Solution

  • As I read the comments, I can see that you are going to delete this question. However, I would like to ask you to take a moment to look at my solution. I think it is much simpler and more transparent.

    We will start with a simple tibble in which the parameters of your calculations will be.

    library(tidyverse)
    
    df = tibble(
      seed = rep(c(289805, 671086, 799837), 4),
      sd = rep(c(1, 3, 5, 10), each=3),
      ar = rep(c(.8, .9, .95), 4)
    )
    

    output

    # A tibble: 12 x 3
         seed    sd    ar
        <dbl> <dbl> <dbl>
     1 289805     1  0.8 
     2 671086     1  0.9 
     3 799837     1  0.95
     4 289805     3  0.8 
     5 671086     3  0.9 
     6 799837     3  0.95
     7 289805     5  0.8 
     8 671086     5  0.9 
     9 799837     5  0.95
    10 289805    10  0.8 
    11 671086    10  0.9 
    12 799837    10  0.95
    

    Now we are preparing one simple farima function.

    farima = function(seed, n, ar, order, sd){ 
      set.seed(seed)
      model = arima.sim(n=n, model=list(ar = ar, order = order), sd = sd)
      tibble(
        name = paste("sd -", sd, "ar -", ar),
        x = model %>% as.numeric(), 
        lb = 1:length(x)
        )
    }
    

    All you have to do afterwards is one simple mutation!

    df %>% nest_by(seed, ar, sd) %>% 
      mutate(data = list(farima(seed, 10, ar, c(1, 0, 0), sd))) %>% 
      unnest(data) %>% 
      ggplot(aes(lb, x))+
      geom_line(color = "#F2AA4CFF")+
      geom_point(color = "#101820FF")+
      theme_bw()+
      scale_y_continuous(expand = c(0, 0))+
      facet_wrap(.~name, ncol = 3, scales = "free")
    

    enter image description here

    But you can also do it a bit differently.

    df %>% nest_by(seed, ar, sd) %>% 
      mutate(data = list(farima(seed, 10, ar, c(1, 0, 0), sd))) %>% 
      unnest(data) %>% 
      ggplot(aes(lb, x))+
      geom_line(color = "#F2AA4CFF")+
      geom_point(color = "#101820FF")+
      theme_bw()+
      scale_y_continuous(expand = c(0, 0))+
      facet_grid(vars(sd), vars(ar), scales = "free")
    

    enter image description here

    I hope you like my solution and instead of deleting this question you will mark it as useful.

    Small update for @Daniel James

    You have to understand well what happens when you use the nest_by function. I'll show you step by step.

    df %>% nest_by(seed, ar, sd) %>% #Step 1
      mutate(data = list(farima(seed, 10, ar, c(1, 0, 0), sd))) %>% #Step 2
      unnest(data) %>% #Step 3
      ggplot(aes(lb, x))+ #Step 4
      geom_line(color = "#F2AA4CFF")+
      geom_point(color = "#101820FF")+
      theme_bw()+
      scale_y_continuous(expand = c(0, 0))+
      facet_wrap(.~name, ncol = 3, scales = "free")
    

    After the first step, you get a shattered tibble, however, not by variables but by rows. The date variable appears here, which in this case is basically empty (it contains the zero value).

    # A tibble: 12 x 4
    # Rowwise:  seed, ar, sd
         seed    ar    sd               data
        <dbl> <dbl> <dbl> <list<tibble[,0]>>
     1 289805  0.8      1            [1 x 0]
     2 289805  0.8      3            [1 x 0]
     3 289805  0.8      5            [1 x 0]
     4 289805  0.8     10            [1 x 0]
     5 671086  0.9      1            [1 x 0]
     6 671086  0.9      3            [1 x 0]
     7 671086  0.9      5            [1 x 0]
     8 671086  0.9     10            [1 x 0]
     9 799837  0.95     1            [1 x 0]
    10 799837  0.95     3            [1 x 0]
    11 799837  0.95     5            [1 x 0]
    12 799837  0.95    10            [1 x 0]
    

    If you do the mutation now, it will apply to the values in each row.

    So let's see what we have after step two.

    # A tibble: 12 x 4
    # Rowwise:  seed, ar, sd
         seed    ar    sd data             
        <dbl> <dbl> <dbl> <list>           
     1 289805  0.8      1 <tibble [10 x 3]>
     2 289805  0.8      3 <tibble [10 x 3]>
     3 289805  0.8      5 <tibble [10 x 3]>
     4 289805  0.8     10 <tibble [10 x 3]>
     5 671086  0.9      1 <tibble [10 x 3]>
     6 671086  0.9      3 <tibble [10 x 3]>
     7 671086  0.9      5 <tibble [10 x 3]>
     8 671086  0.9     10 <tibble [10 x 3]>
     9 799837  0.95     1 <tibble [10 x 3]>
    10 799837  0.95     3 <tibble [10 x 3]>
    11 799837  0.95     5 <tibble [10 x 3]>
    12 799837  0.95    10 <tibble [10 x 3]>
    

    Note that our farima function used in the mutate function for the arguments seed, ar and sd received single values on each row! Also pay attention to use the list function in which you will wrap our farima function.

    Now all we have to do is solve our data variable. We'll do this in step 3.

    # A tibble: 120 x 6
    # Groups:   seed, ar, sd [12]
         seed    ar    sd name                 x    lb
        <dbl> <dbl> <dbl> <chr>            <dbl> <int>
     1 289805   0.8     1 sd - 1 ar - 0.8 -0.251     1
     2 289805   0.8     1 sd - 1 ar - 0.8  0.886     2
     3 289805   0.8     1 sd - 1 ar - 0.8  0.198     3
     4 289805   0.8     1 sd - 1 ar - 0.8  1.54      4
     5 289805   0.8     1 sd - 1 ar - 0.8  4.70      5
     6 289805   0.8     1 sd - 1 ar - 0.8  4.44      6
     7 289805   0.8     1 sd - 1 ar - 0.8  3.02      7
     8 289805   0.8     1 sd - 1 ar - 0.8  1.81      8
     9 289805   0.8     1 sd - 1 ar - 0.8  1.17      9
    10 289805   0.8     1 sd - 1 ar - 0.8  1.16     10
    # ... with 110 more rows
    

    Everything that happens in step 4 and beyond is just creating the appropriate charts.