1

I'm trying to plot European Countries using Leaflet but the Values that are assigned to the countries are incorrect. The goal is to create a choropleth map of Europe where each country has a specific value assigned to it. I used a sample of the original dataset and what can be seen in the image "The Current Output" is that Sweden get the Value of Germany. How can I assign the right Value to the right GEO(country)

require(shiny)
require(leaflet)
require(htmltools)
require(ggplot2)
require(highcharter)
require(maps)
require(dplyr)

rm(list = ls())

df <- data.frame(GEO = c("Belgium", "Germany" , 
"France","Italy","Sweden","Slovakia" ),
             PRODUCT = c("Total petroleum products"),
             TIME = c(1990),
             Value = c(18345, 126544, 88659,90069,14670,4974), 
stringsAsFactors = FALSE)

ui <- fluidPage(
  titlePanel("Energy consumption in Europe"),
  tabsetPanel(
    tabPanel("Map overview", sidebarLayout(
      sidebarPanel(selectInput("Type", "Select Type", choices = 
unique(df$PRODUCT), selected = ""), 
               selectInput("Year", "Select Year", choices = 
unique(df$TIME), selected = "")),
      mainPanel(leafletOutput("firstMap", height = 800)
      )))
  )
)

server <- function(input, output) {

  output$firstMap <- renderLeaflet({

    df_tmp1<-reactive({
      subset(df, df$PRODUCT==input$Type & df$TIME==input$Year)
    })

    df_tmp <-df_tmp1()

    bounds <- map("world", unique(df_tmp$GEO), fill = TRUE, plot = FALSE)
    bounds$Value <- df_tmp$Value
    pal <- colorNumeric("Blues", df$Value)

    leaflet(bounds)%>%
      addTiles()%>%
      addPolygons(stroke = TRUE,
                  smoothFactor = 0.5,
                  fillOpacity=0.8,
                  fillColor = ~pal(df_tmp$Value),
                  color = "black",
                  opacity = 0.7,
                  weight = 1,
                  popup = paste(bounds$names, "<br>", "Value: ", 
df_tmp$Value, "KTOE"
              )
      )%>%
      addLegend("topright", pal = pal, values = df$Value,
                title = "Consumption in KTOE",
                labels = c(min(df$Value), max(df$Value)),
                opacity = 0.6
      )
  })

}


shinyApp(ui, server)

The Current output

What am I missing here?

  • Could you provide some data? Is this an issue only in your shiny app or does the issue arise also when creating the map in a normal session? – niko Jan 13 '19 at 13:16
  • @nate, I updated the the code and provided sample data. I don't know if this issue arises when creating a normal session. I'm new to R and shiny so I have minor knowledge... – Joris Bertens Jan 13 '19 at 14:23

1 Answers1

0

This is due to the ordering of the countries, which you seem to take for granted.

bounds <- map("world", unique(df_tmp$GEO), fill = TRUE, plot = FALSE)

will return the polygons you ask, but not necessarily in the same order. Also, If a country consists of several polygons, (e.g. France also has "France:Corsica") this will also complicate your data. So simply setting

bounds$Value <- df_tmp$Value

will indeed give rather random results. You have to make sure you match the right value to the right polygon(s). So first you have to extract the country names without additions from bounds:

bounds$country <- vapply(strsplit(bounds$name, ":"), function(x) x[1], FUN.VALUE="a")

Now you can give the correct values per entry:

bounds$Value <- df_tmp$Value[match(bounds$country, df_tmp$GEO)]
Alex Deckmyn
  • 1,017
  • 6
  • 11