Home > Blockchain >  Facet_grid: Error in Factor() Object not Found
Facet_grid: Error in Factor() Object not Found

Time:11-15

I want to ggplot with facet_grid on this my type of data that contains a column with a [list of vectors][1] just the same way as this this image only that the 0.8 will be (0.4, 0.4), 0.9 will be (0.45, 0.45) and 0,95 will be (0.35, 0.6). like this.

CodePudding user response:

If you are open to a complete tidyverse workflow, you could use:

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

df %>% 
  pivot_longer(-xx) %>% 
  mutate(id = as.numeric(gsub("x", "", name))) %>% 
  arrange(id, xx) %>% 
  select(-id) %>% 
  mutate(sd = rep(rep(c(sd = 1, sd = 3, sd = 5, sd = 10), each = 10), each = 3),
         phi = rep(rep(list(c(0.4, 0.4), c(0.45, 0.45), c(0.35, 0.6)), each = 10), 4)) %>% 
  mutate(sd = factor(sd, levels = sd, labels = paste("sd =", sd)),
         phi = factor(phi, levels = phi, labels = gsub("c", "", paste("\U03D5 =", phi)))) %>%   
  ggplot(aes(x = xx, y = value))  
  geom_line()   
  geom_point()  
  scale_y_continuous(expand = c(0.0, 0.00))  
  labs(x = "Time", y = "Series")  
  facet_grid(sd ~ phi, scales = "free_y")   
  theme_bw()

This produces

enter image description here

  • Related