使用传单和地图将值分配给错误的国家/地区 [英] Value assigned to the wrong country using Leaflet and Maps

查看:60
本文介绍了使用传单和地图将值分配给错误的国家/地区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Leaflet绘制欧洲国家/地区,但是分配给这些国家/地区的值不正确.目标是创建一个欧洲的Choropleth地图,其中每个国家都为其分配了特定的值.我使用了原始数据集的样本,在当前输出"图像中可以看到,瑞典获得了德国的价值.如何为正确的GEO(国家/地区)分配正确的值

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)

当前输出

我在这里想念什么?

推荐答案

这是由于国家/地区的排序所致,您认为这是理所当然的.

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)

将返回您要求的多边形,但不一定以相同的顺序.另外,如果一个国家/地区由多个多边形组成(例如,法国也有"France:Corsica"),这也会使您的数据复杂化.因此只需设置

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

这篇关于使用传单和地图将值分配给错误的国家/地区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆