关于函数内的ggplot标题

时间:2017-09-19 14:03:11

标签: r ggplot2

我在下面创建了一个函数,

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

下面我用两个不同的变量调用函数:

create_plot(white_wine$total.sulfur.dioxide)

create_plot(white_wine$density)

我在ggtitle语法中加了“_______”,因为我想要调用的任何变量的名称。

例如,

'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.

1 个答案:

答案 0 :(得分:2)

也许你可以在函数调用中使用另一个参数?

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')