Kinjal Kachi
Kinjal Kachi

Reputation: 131

About ggplot titles inside a function

I have created a function below,

create_plot <- function(variable) {
 return(qplot(data = white_wine, x = variable ,color =I('black'),fill = 
        qual_factor ))+
      ggtitle('Distribution of quality with respect to _______')}

Below I am calling the function with two different variables:

create_plot(white_wine$total.sulfur.dioxide)

create_plot(white_wine$density)

I have put "_______" in ggtitle syntax , because I want the name of any variable that I call.

For example ,

'enter code hereDistribution of quality with respect to total.sulfur.dioxide' when 
     white_wine$total.sulfur.dioxide is called, and ,'Distribution of quality with respect to density' when white_wine$density is called.

Upvotes: 1

Views: 1412

Answers (1)

FlorianGD
FlorianGD

Reputation: 2436

Maybe you could use another argument in your function call ?

create_plot <- function(variable, title) {
 return(qplot(data = white_wine, x = variable ,color =I('black'),fill = 
        qual_factor ))+
      ggtitle(paste0('Distribution of quality with respect to ', title))}

create_plot(white_wine$total.sulfur.dioxide, 'total sulfure dioxine')

create_plot(white_wine$density, 'density')

Upvotes: 2

Related Questions