Saurabh
Saurabh

Reputation: 121

Getting error while adding legend in leaflet object in R

I am trying to create a map in ggplot using leaflet library in R, I am pretty new to R programing. My dataset is per capita carbon emission of different countries in the world. For this, I first merge the shp file with my dataset, then created lables, bins and palette and then added polygons. But when I am trying to add legend using addlegend(), its throwing the following error: Error in as.character(x) : cannot coerce type 'closure' to vector of type 'character'. I have checked code in stages and codes works until I add legend, so suspecting either some issue in pal or value in addlegend().

library(leaflet)
library(ggplot2)
library(dplyr)
library(rgeos)
library(maptools)
library(ggmap)
library(broom)
library(readr)
library(lubridate)
library(dplyr)
library(tidyr)
library(scales)
library(RColorBrewer)
theme_set(theme_minimal())

# Read data into per_capita_emission object
per_capita_emission <- read.csv('co-emissions-per-capita.csv') 

# Filter dataframe for year 2017 
per_capita_emission_2017 <- per_capita_emission  %>% filter(Year == "2017")

# read shape file into R
Countries_shp<-readShapeSpatial("World_Countries/World_Countries.shp",delete_null_obj=TRUE)

# leaflet
p1 <- leaflet(Countries_shp) %>% 
  setView(lng = 145.5, lat = -36.5, zoom = 2)
p1 %>% addPolygons()
per_capita_emission_2017$COUNTRY <- per_capita_emission_2017$Entity
per_capita_emission_2017<-select (per_capita_emission_2017,-c(Entity))
merge.per_capita_emission_2017_shp<-sp::merge(Countries_shp, per_capita_emission_2017, 
                                               by="COUNTRY", duplicateGeoms = TRUE)

bins <- quantile(
  merge.per_capita_emission_2017_shp$Per_capita_emissions_in_tonnes,
  probs = seq(0,1,.2), names = FALSE, na.rm = TRUE)

pal <- colorBin(
  "YlOrRd",
  domain = merge.per_capita_emission_2017_shp$Per_capita_emissions_in_tonnes,
  bins = 4,
  pretty = FALSE
)

p1 <- leaflet(merge.per_capita_emission_2017_shp) %>% 
  setView(lng = 147, lat = -36.5, zoom = 2)
p1 %>% addPolygons(
  fillColor = ~pal(Per_capita_emissions_in_tonnes),
  weight = 2,
  opacity = 1,
  color = "white",
  dashArray = "3",
  fillOpacity = 0.7,
  highlight = highlightOptions(
    weight = 3,
    color = "#666",
    dashArray = "",
    fillOpacity = 0.7,
    bringToFront = TRUE))

labels <- sprintf(
  "<strong>%s</strong><br/>%g Per_capita_emissions_in_tonnes",
  merge.per_capita_emission_2017_shp$COUNTRY, 
  merge.per_capita_emission_2017_shp$Per_capita_emissions_in_tonnes
) %>% lapply(htmltools::HTML)

p1 %>% addPolygons(
  data = merge.per_capita_emission_2017_shp,
  fillColor = ~pal(merge.per_capita_emission_2017_shp$Per_capita_emissions_in_tonnes),
  weight = 2,
  opacity = 1,
  color = "white",
  dashArray = "3",
  fillOpacity = 0.7,
  highlight = highlightOptions(
    weight = 5,
    color = "#666",
    dashArray = "3",
    fillOpacity = 0.7,
    bringToFront = TRUE),
  label = labels,
  labelOptions = labelOptions(
    style = list("font-weight" = "normal", padding = "3px 8px"),
    textsize = "15px",
    direction = "auto"))%>%
  addLegend(pal = pal, 
            values = merge.per_capita_emission_2017_shp$Per_capita_emissions_in_tonnes,
            opacity = 0.7, 
            title = "Per_capita_emissions_in_tonnes",
            position = "bottomright"
  ) %>% 
  addControl(title, position = "topright")

Following is the link to get the dataset, please include blob: to download data: blob:https://ourworldindata.org/27f19210-ac40-4909-9826-c7a72dc64a23

Following is the link to get the shp file for country's cordinates: https://tapiquen-sig.jimdofree.com/app/download/5496966159/World_Countries.rar?t=1589254856

Upvotes: 0

Views: 955

Answers (2)

OzcanK
OzcanK

Reputation: 51

I think you should change like that:

addLegend(pal = pal, <br>
            values = per_capita_emission_2017_shp$Per_capita_emissions_in_tonnes,<br>
            opacity = 0.7, <br>
            title = "Per_capita_emissions_in_tonnes",<br>
            position = "bottomright"<br>
  )

Upvotes: 0

NaFerr
NaFerr

Reputation: 46

Sorry this is going to be more of a clarification, but I cant comment (not enough reputation).

I think what may be happening is the addLegend is seeing your values as a character or its not recognizing it because it could be seeing it as a shapefile database, so maybe something like merge.per_capita_emission_2017_shp@data$Per_capita_emissions_in_tonnes.

When you merge that data to the shapefile you produce both the shapes and data together in a list like vector, so be sure to call the data portion of that from where you merged it.

For example in my leaflet if have (values = dataM@data[,6]) with data being the sub-spreadsheet i want to call from within the shapefile list.

I dont know the structure of the dataset (the link is 404ing), so this is purely guesswork.

Sorry I cant be more specific

Upvotes: 0

Related Questions