为ggplot

时间:2016-05-31 08:28:48

标签: r plot

我是R的新手并且一直试图解决这个问题。基本上,我有一个数据框和各种y变量。我正在尝试编写一个函数,这将允许我为我拥有的许多不同y变量提供自定义图形模板。我正在尝试下面的代码,但我遇到了这个错误:

  

1:在eval(expr,envir,enclos)中:强制引入的NA

     

2:在aes_string(xvar [max(which(complete.cases(yvar)))]中,yvar [max(which(complete.cases(yvar)))],:     强制引入的NA

如果我直接添加变量而不是通过函数添加变量,则代码可以正常工作。我认为这与函数如何将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))

2 个答案:

答案 0 :(得分: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")

答案 1 :(得分:0)

我认为@LukeA已经让你几乎一直在那里,但是这里有一个使用你的数据的例子,并添加了一些列来帮助演示如何在你自己的函数中将列名传递给ggplot。

它使用您的变量名称。它将您的数据子集化为具有y的非缺失值的data.frame,然后将您的数据子集化为单独的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")