0

I have a nested tibble that stores data, a grouping variable and a plot illustrating the data.

Consider the following example:

library(dplyr)
library(tibble)
library(ggplot2)

test <- tibble(x=c(1,2), p=list(qplot(1), qplot(2)))

Column p are the plots, column x is the grouping variable. I would like to provide a title to each plot, based on the value of x in the respective row.

I tried using dplyr's mutate function:

mutate(test, p=p+labs(title=paste("Group:", x)))

However, this specific code fails with Error: non-numeric argument to binary operator.

How can I add another layer to the stored ggplots within the tibble?

Vertho
  • 200
  • 1
  • 7
  • 1
    Try `test$p <- lapply(1:length(test$p), function(k) test$p[[k]]+labs(title=paste("Group:", test$x[k])))` – Marco Sandri Nov 16 '17 at 18:08
  • That worked. I tried using lapply on test$p before, but couldn't get it to run. lapply using 1:length does the job. Thank you! – Vertho Nov 16 '17 at 18:13

0 Answers0