I am using this to print plots to a PDF within a for loop, which is working fine:
print(ggplot(subdata3, aes(x = Year, y = value, colour = Stat)) 
+ geom_line() + expand_limits(y=c(0,100)) 
+ ggtitle(paste0(as.character(ScenName),":\n", as.character(k))) 
+ ylab(paste0(j, " (", units, ")")))
Now, I need to assign each ggplot to a variable 'p', which will then be stored in a list and used by multiplot to arrange multiple plots on a page.
This is my attempt to assign the plot to a variable:
p <- ggplot(subdata3, aes(x = Year, y = value, colour = Stat)) 
+ geom_line() + expand_limits(y=c(0,100)) 
+ ggtitle(paste0(as.character(ScenName),":\n", as.character(k))) 
+ ylab(paste0(j, " (", units, ")"))
The only changes I made were to remove print() and make the variable assignment. After which, I receive this error:
Error in +ggtitle(paste0(as.character(ScenName), ":\n", as.character(k))) : 
  invalid argument to unary operator
I have tried many adjustments to the various sets of () in attempts find a solution, however, nothing seems to work.
Any thoughts?
You've got your +s in the wrong place. Try this:
p <- ggplot(subdata3, aes(x = Year, y = value, colour = Stat)) +
  geom_line() + expand_limits(y=c(0,100)) +
  ggtitle(paste0(as.character(ScenName),":\n", as.character(k))) +
  ylab(paste0(j, " (", units, ")"))
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With