user169605
user169605

Reputation: 141

Variable in title for ggplot.Predict

I would like to use ggplot.Predict (from the rms package) in a function to produce graphs automatically:

library(rms)

ddist <- datadist(iris); options(datadist="ddist")

mod <- ols(Sepal.Length ~ Petal.Length, data=iris)

p.plot <- function(pred, ttl) {
          ggplot(pred, 
          addlayer=labs(title=ttl))
}

plot.title <- "Sepal length"

p.plot(Predict(mod), plot.title) 

This results in an "Error in labs(title = ttl) : object 'ttl' not found" The following however works without problems.

ggplot(Predict(mod), addlayer=labs(title=plot.title)) 

Upvotes: 0

Views: 168

Answers (1)

divibisan
divibisan

Reputation: 12165

Answered by MrFlick in the comments:

Rather than using addlayer=, try just adding + labs(title=ttl) after the ggplot() call:

p.plot <- function(pred, ttl) {
          ggplot(pred) +
              labs(title=ttl)
}

Upvotes: 1

Related Questions