purrr + ggplot: How to add titles

Hi

I have a dataset that I want to go through printing multiple graphs: https://pastebin.com/QwF1mb9w

I'm using purrr to map my graph function to each nested grouping and can therefore print out all my graphs, but I would like to title each one by what they were grouped by so I can tell them apart

Thanks

my_graphs <- df %>% 
  pivot_longer(names_to = "name", values_to = "value", cols = viability_percent:vcd_10_5_cells_m_l) %>%
  group_by(grouping, name) %>% 
  nest() %>% 
  mutate(graphs = map(
    data,
    function(.x) {
      ggplot(.x, aes(actual_duration_of_subculture_hrs, value, group = run)) +
        geom_point() +
        geom_line()
      }
  ))

my_graphs$graphs %>% 
  map(print)

I was able to answer my own question using map2, for future reference:

my_graphs <- df %>% 
  pivot_longer(names_to = "name", values_to = "value", cols = viability_percent:vcd_10_5_cells_m_l) %>%
  mutate(graph_title = paste(grouping, ":", name)) %>% 
  group_by(graph_title) %>% 
  nest() %>% 
  mutate(graphs = map2(
    data, graph_title,
    ~ {
      ggplot(.x, aes(actual_duration_of_subculture_hrs, value, group = run)) +
        geom_point() +
        geom_line() +
        ggtitle(.y)
      } 
  ))

my_graphs$graphs %>% 
  map(print)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.