2016-05-31 105 views
0

我是R的新手,一直試圖弄清楚這一點。基本上我有一個數據框,和各種變量。我正在嘗試編寫一個函數,使我能夠爲我所擁有的許多不同的y變量提供自定義圖形模板。我想下面的下面的代碼,但我遇到了這個錯誤:爲ggplot編寫函數

1: In eval(expr, envir, enclos) : NAs introduced by coercion

2: In aes_string(xvar[max(which(complete.cases(yvar)))], yvar[max(which(complete.cases(yvar)))], : NAs introduced by coercion

代碼工作,如果我直接和不添加變量通過函數。我相信這與功能如何將xvar插入到as.numeric()函數中有關。我不確定,但你們中的任何一個人都知道如何處理這個問題?

test <- function (Data, xvar, yvar){ 
    # Plot data 
    plot <- ggplot(subset(Data,!is.na((yvar))), aes_string(xvar, yvar)) + geom_line(colour="darkblue") + theme_bw() 
    # Add Trendline for recent data 
    plot <- plot + geom_smooth(data=subset(Data, xvar > as.numeric(xvar)[max(which(complete.cases(yvar)))-8]), method = "lm") 
    # Label most recent data 
    plot + geom_text(data = Data, aes_string(xvar[max(which(complete.cases(yvar)))], 
          yvar[max(which(complete.cases(yvar)))], 
          label = as.numeric(yvar)[max(which(complete.cases(yvar)))], 
          hjust= -0.5, vjust = 0.5)) 
+0

'aes_string'需要一個字符串。 'xvar [max(which(complete.cases(yvar)))'在這裏沒有意義。在ggplot2之外計算這個值。另外,不要在函數內部使用'subset'。這是文件警告這一點。 – Roland

+0

非常感謝!我明白你的意思了! – Binggg

回答

1

作爲xvar可能是(你不顯示可再現的例子)長度爲1的字符向量,像xvar[]子集將不會產生所希望的結果。

你可以嘗試像

library(ggplot2) 
f <- function(data, xvar, yvar) { 
    ggplot(data, aes_string(xvar, yvar)) + 
    geom_point() + 
    geom_smooth(data=subset(data, eval(parse(text=xvar)) > 5), method = "lm") 
} 

f <- function(data, xvar, yvar) { 
    ggplot(data, aes_string(xvar, yvar)) + 
    geom_point() + 
    geom_smooth(data = data[data[, xvar]>5, ], method = "lm") 
} 

f(mtcars, "cyl", "disp") 
+0

我仍然沒有得到如何糾正?我知道它是一個長度爲1的字符向量(這與字符串是否相同?)。假設我有一個這樣的數據: > X1 < - as.yearqtr(seq(asDate(「2010/3/1」),by =「quarter」,length.out = 10)) > Y1 < - as.vector(c(124,315,363,574,345,434,141,512,142,647)) > Data1 < - data.frame(X1,Y1) – Binggg

0

我覺得@LukeA已經得到了幾乎你一路有,但這裏是使用你的數據,並增加了一些例子更多的列可以幫助演示如何將列名傳遞到您自己的函數中的ggplot。

它使用你的變量名稱。它將您的數據分成一個data.frame,其中y的值爲非缺失值,然後將您的數據分爲獨立的data.frame,允許您爲平滑功能添加額外的過濾標準。

library(zoo) 
set.seed(72) 

X1 <- as.yearqtr(seq(as.Date("2010/3/1"), by = "quarter", length.out = 10)) 
Y1 <- as.vector(c(124,315,363,574,345,434,141,512,142,647)) 
Y2 <- sample(Y1) 
Y3 <- sample(Y1) 

Data1 <- data.frame(X1, Y1, Y2, Y3) 


plot_function <- function(data, xvar, yvar){ 

    # remove rows with NA on yvar 
    mydata1 <- data[!is.na(data[, yvar]), ] 

    # remove rows with NA on yvar and subset yvar above some threshold 
    mydata2 <- data[!is.na(data[, yvar]) & data[, yvar] > 400, ] 

    # plot it 
    myplot <- ggplot(mydata1, aes_string(xvar, yvar)) + 
    geom_line(colour="darkblue") + 
    scale_x_yearqtr(limits = c(min(mydata1[, xvar]), max(mydata1[, xvar])), format = "%YQ%q") + 
    geom_smooth(data = mydata2, aes_string(xvar, yvar), method = "lm") + 
    geom_text(data = mydata1, aes_string(xvar, yvar, label = yvar), hjust= -0.5, vjust = 0.5) + 
    theme_bw() 

    return(myplot) 
} 


plot_function(data = Data1, xvar = "X1", yvar = "Y1") 
plot_function(data = Data1, xvar = "X1", yvar = "Y2") 
plot_function(data = Data1, xvar = "X1", yvar = "Y3")