Adding nested observations to a dataframe

I think it'll be easier to do it in a slightly different way. One approach is:

library(tidyverse)

a = c(2,4,6)
b = c(10,12,14)

params = expand.grid(a = a, b = b) %>% as_tibble()

gen_den = function(a,b,...){ 
  x = seq(0,1,0.1)
  den = dbeta(x = x, shape1 = a, shape2 = b)
  return(tibble(x = x, y = den))
}

params %>%
  dplyr::mutate(data = pmap(., gen_den)) %>%
  dplyr::mutate(label = paste0("a = ", a, ", b = ", b)) %>%
  dplyr::select(data, label) %>%
  tidyr::unnest() %>%
  ggplot(., aes(x = x, y = y, color = label)) + 
    geom_line()

This would produce a graph with all of the variations on the same plot, which I guess is what you are going for.

5 Likes