SunWuKung
SunWuKung

Reputation: 557

pass a list as arguments to ggplot

I would like to pass a list as arguments to ggplot. The solutions I found so far usually use do(), but I don't know how to use that here. In the example below I would like ggplot to take the name and limits argument from the variable.

library(ggplot2)
p<-ggplot(mtcars, aes(x=wt, y=mpg)) + 
  geom_point()

p

yscale<-list(limits=c(0,40), name='A Y scale name')
p<-p+scale_y_continuous(yscale) #how to change this line?
p

Upvotes: 1

Views: 425

Answers (1)

Tur
Tur

Reputation: 614

Use do.call:

yscale<-list(limits=c(0,40), name='A Y scale name')
p <- p + 
  do.call(scale_y_continuous, yscale)

Upvotes: 2

Related Questions