R 在自定义函数中使用ggplot参数会引发“找不到对象”错误

R 在自定义函数中使用ggplot参数会引发“找不到对象”错误,r,function,ggplot2,R,Function,Ggplot2,我编写了一个函数来加载空间数据,从输入数据集中提取数据,并将该数据集和空间数据合并。然后,我的函数返回一个映射,在该映射上绘制案例 如果按以下方式返回绘图,则我的函数工作正常: (填充=totalCases) 但是,我的目标是传递一个为fill参数提供值(=变量)的参数,如下面的代码所示。但这会引发以下错误: eval(expr、envir、enclose)中出错:找不到对象“variable” 这是我的密码: plotMonths <- function(data, variable

我编写了一个函数来加载空间数据,从输入数据集中提取数据,并将该数据集和空间数据合并。然后,我的函数返回一个映射,在该映射上绘制案例

如果按以下方式返回绘图,则我的函数工作正常: (填充=totalCases)

但是,我的目标是传递一个为fill参数提供值(=变量)的参数,如下面的代码所示。但这会引发以下错误:

eval(expr、envir、enclose)中出错:找不到对象“variable”

这是我的密码:

  plotMonths <- function(data, variable, date) {
    # Reloading district polygons
    sl_adm2_months <- readOGR("C:/Users/woba/Documents/Ordina/TFS-Projects/Ordina - Mail Analytics/Johnson/Wouter/03. GeoData map - R/Sierra Leone adm2", "SLE_adm2", verbose = TRUE, stringsAsFactors = FALSE)
    sl_adm2_months_DF <- fortify(sl_adm2_months, region = "NAME_2") 

    # Getting the correct District names
    colnames(sl_adm2_months_DF)[7] <- "District"
    sl_adm2_months_DF$District <- ifelse(sl_adm2_months_DF$District == "Western Rural", "Western Area Rural", as.character(sl_adm2_months_DF$District))
    sl_adm2_months_DF$District <- ifelse(sl_adm2_months_DF$District == "Western Urban", "Western Area Urban", as.character(sl_adm2_months_DF$District))
    sl_adm2_months_DF$District <- as.factor(sl_adm2_months_DF$District)

    #Extracting district names for plotting
    sl_adm2_months_names_DF <- data.frame(long = coordinates(sl_adm2_months[, 1]), lat = coordinates(sl_adm2_months[, 2]))
    sl_adm2_months_names_DF[, "ID_2"] <- sl_adm2_months@data[, "ID_2"]
    sl_adm2_months_names_DF[, "NAME_2"] <- sl_adm2_months@data[, "NAME_2"]

    # Subset May data
    sl_Month <- data[data$Country == "Sierra Leone" & data$Date <= as.Date(date), ]
    sl_Month <- droplevels(sl_Month)
    sl_Month[is.na(sl_Month)] <- 0
    confirmed <- ddply(sl_Month, .(Localite), function(x){max(x$cmlConfirmed.cases, na.rm = T)})
    cases <- ddply(sl_Month, .(Localite), function(x){max(x$cmlCases, na.rm = T)})
    deaths <- ddply(sl_Month, .(Localite), function(x){max(x$cmlDeaths, na.rm = T)})
    sl_Month <- merge(cases, deaths, by = "Localite")
    sl_Month <- merge(sl_Month, confirmed, by = "Localite")
    sl_Month <- droplevels(sl_Month)
    sl_Month <- droplevels(sl_Month)
    colnames(sl_Month)<- c("District", "totalCases", "totalDeaths", "totalConfirmed")
    sl_Month <- sl_Month[-which(sl_Month$District == "National"),]  

    # Merging Month data with District polygons 
    sl_adm2_Month <- merge(sl_adm2_months_DF, sl_Month, by = "District", all.x = TRUE)
    sl_adm2_Month$totalCases <- as.numeric(sl_adm2_Month$totalCases)
    sl_adm2_Month$totalDeaths <- as.numeric(sl_adm2_Month$totalDeaths)
    sl_adm2_Month$totalConfirmed <- as.numeric(sl_adm2_Month$totalConfirmed)

    #NA to 0 for values missing for districts
    sl_adm2_Month[is.na(sl_adm2_Month)] <- 0

    #Sorting
    sl_adm2_Month <- sl_adm2_Month[order(sl_adm2_Month$District, sl_adm2_Month$order), ]

    # Prints & Views
    print(head(sl_Month))
    View(sl_Month)
    View(sl_adm2_Month)

    Sys.setlocale("LC_TIME", "English")

   # Plotting Cases
    return ({
      ggplot() +
      geom_polygon(data = sl_adm2_Month, aes(x = long, y = lat, group = group,
                                           fill = variable), colour = "white") +
      geom_text(data = sl_adm2_months_names_DF, aes(label = NAME_2, x = long.1, y = lat.2, group = NAME_2), size = 3) + 
#       labs(title = paste("Ebola", str_sub(as.character(variable), 6, -1), "cases by district in Sierra Leone - until",  format(as.Date(date), "%B %Y"))) + 
      xlab("") + 
      ylab("") + 
      theme_gray() + 
      theme(legend.position = "bottom")
    })
  }

  # Plotting the months - variable = second input and must be IN c(totalDeaths, totalCases, totalConfirmed)
  plotMonths(final_dataset, "totalCases", "2014-05-31")

plotMonths使用'aes_string'而不是'aes'解决了我的问题

aes_string(x = "long", y = "lat", group = "group", fill = variable)        
有关ggplot2软件包的aes和aes_字符串之间差异的说明,请参见:


所有的功劳都归于阿克塞曼和本杰明——他们的回答解决了我的问题

变量
似乎不是数据框中的列。你应该在
sl_adm2_Month
中添加
variable
,这样可以解决你的错误。哦,如果
variable
是一个字符串,表示填充
所用的列,那么你应该查看
aes_字符串
是的,你可能想要
aes_字符串(x=“long”,y=“lat”,group=“group”,fill=variable)
或类似。非常感谢各位,aes\u字符串似乎工作得很好!我过去一直在使用aes,从来没有一次我不得不使用aes_字符串。。。刚刚查找了aes和aes_字符串之间的差异,现在我清楚了为什么需要使用此字符串。:)感谢@Axeman提供的信息,用正确的答案更新了此主题。
aes_string(x = "long", y = "lat", group = "group", fill = variable)