简体   繁体   English

关于函数内的ggplot标题

[英]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. 我已经在ggtitle语法中添加了“ _______”,因为我想要我调用的任何变量的名称。

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.

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM