0

I have written a function to load spatial data, extract data from an input dataset and merge this dataset with the spatial data. Then my function returns a map on which my cases get plotted.

My function works fine if I return my plot as the following: (with fill = totalCases)

    return ({
      ggplot() +
      geom_polygon(data = sl_adm2_Month, aes(x = long, y = lat, group = group,
                                           fill = totalCases), 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")
    })

However, my goal is to pass a parameter providing the value (= variable) for the fill parameter as you can see in my below code. But this throws the following error:

Error in eval(expr, envir, enclos) : object 'variable' not found

Here is my code:

  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")

I've read some similar questions on the forum but wasn't able to resolve my issue.

Any help on how to fix this is very welcome!

wbaeckelmans
  • 347
  • 3
  • 12
  • `variable` doesn't appear to be a column in your data frame. You should add `variable` to `sl_adm2_Month` somehow and that should resolve your error. – Benjamin Nov 25 '15 at 13:52
  • 2
    Oh, and if `variable` is a character string denoting the column to use for `fill`, then you should look into `aes_string` – Benjamin Nov 25 '15 at 13:53
  • Yes, you probably want `aes_string(x = "long", y = "lat", group = "group", fill = variable)` or similar. – Axeman Nov 25 '15 at 14:03
  • 1
    Thanks very much guys, aes_string seems to work perfectly! I've always been using aes in the past and never had an occasion that I was obliged to use aes_string... Just looked up the difference between aes and aes_string and now it's clear to me why I need to use this one. :) – wbaeckelmans Nov 25 '15 at 14:37
  • Thanks I didn't know `aes_string` is using regular evaluation. I have always used `aes`, based on non standard evaluation. Copy the working code containing `aes_string` as an answer if this fixes your issue. – Paul Rougieux Nov 25 '15 at 14:39
  • Note that the upcoming version of `ggplot` will include a new preferable option as well: http://docs.ggplot2.org/dev/aes_.html – Axeman Nov 25 '15 at 14:45
  • 1
    Thanks for the information @Axeman, updated this topic with the correct answer. – wbaeckelmans Nov 25 '15 at 14:52

1 Answers1

2

Using 'aes_string' instead of 'aes' solved my issue.

aes_string(x = "long", y = "lat", group = "group", fill = variable)        

Explanation on the differences between aes & aes_string for the ggplot2 package can be found here: What is the difference between aes and aes_string (ggplot2) in R

All credit goes to Axeman & Benjamin - their answers solved my issue!

Community
  • 1
  • 1
wbaeckelmans
  • 347
  • 3
  • 12