Search code examples
rggplot2facet-grid

How to Use Facet Grid For My Type of Data


I have this 12-time series generated data which I have plotted each as a time plot using ggplot2. I want to arrange the 12 of the plots into 1 single plot to make it a 3D using facet_grid where the column name is colname <- c("0.8", "0.9", "0.95") and the row name is rowname <- c("sd = 1", "sd = 3", "sd = 5", "sd = 10") and the arrangement will be a 4 by 3 layout.

## 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)

ggplot

I want it to be like this

enter image description here .

EDIT

May be there is need for its data frame version

df <- data.frame(xx, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12)

Solution

  • In order to use ggplot, you will need data frames. One way to deal with your problem could be for example:

    edit after you've specified that you struggle with the layout, here one way. As you're calculating the time series fairly manually, you will need to manually stipulate the sd/CI information by adding it to your data frame. Then you can use that information in the formula syntax within facet_grid

    ## This requires an empty environment
    ## first make a list of all objects in the environment with the pattern x[number]
    ## mget retrieves all those objects
    ## the subsetting operator is to bring it into the right order
    ls_ts <- mget(ls(pattern = "x[0-9]+"))[paste0("x", 1:length(ls(pattern = "x[0-9]+")))]
    
    newdat <-
      data.frame(
        y = unlist(lapply(ls_ts, as.data.frame)),
        x = xx, sd = rep(rep(c(1, 3, 5, 10), each = 10), each = 3),
        CI = rep(rep(c(.8, .9, .95), each = 10), 4)
      )
    
    ggplot(newdat, aes(x, y)) +
      geom_line() +
      geom_point() +
      labs(x = "lb", y = "RMSE") +
      theme_bw() +
      scale_y_continuous(expand = c(0, 0)) +
      facet_grid(sd ~ CI, scales = "free_y")
    

    Created on 2021-11-07 by the reprex package (v2.0.1)