如何在R中自定义plot()中的xlab名称



我正在尝试创建一个函数来绘制变量的散点图,如下所示:

plotting = function(x,y){
   plot(x, y,
        main= "PM10 and Electricity use",
        ylab= "",
        xlab= "",
        col= "blue", pch = 19, cex = 1, lty = "solid", lwd = 2)
}
y = PM10
x = Total_E*population
plotting(x,y)

(注:PM10、Total_E、population均为数字向量)

是否可以将xlab, ylab改为变量的名称,将ylab改为"PM10",将xlab改为"Total_E*population"甚至"Total_E times population"?

您正在寻找非标准评估。这是通过substitutedeparse实现的. ...

plotting <- function(x, y) {
  plot(x, y, main = "PM10 and Electricity use",
    ylab = deparse(substitute(y)), 
    xlab = deparse(substitute(x))
  )
}

最新更新