2015-08-31 26 views
0

我試圖創建一個函數來繪製的變量散點圖,類似如下:如何在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,人口是數字的所有向量)

那是可能的將xlab,ylab改爲變量的名稱,比如說ylab到「PM10」,xlab改爲「Total_E * population」,甚至是「Total_E times population」?

回答

0

您正在尋找非標準評估。這是通過substitutedeparse完成的。 ...

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

太好了,非常感謝! – Chumoon

+0

編輯爲繪圖函數添加缺少的元素。 – misspelled

+0

@Chumoon你應該接受答案,如果它幫助你獎勵拼錯的應得分:) –

相關問題