R 传单图形未显示多边形的正确国家/地区和值

R 传单图形未显示多边形的正确国家/地区和值,r,merge,shiny,leaflet,spatial,R,Merge,Shiny,Leaflet,Spatial,我有一个4列的示例数据集 连接年份 国家 连接方法 用户数量 我正在尝试使用Shining创建一个仪表板,要求用户输入年份下拉列表,然后单击submit按钮。单击“提交”后,输出应为所选年份的传单地图,并反映来自不同国家/地区的用户数量 我遵循了使用maps包获取shapefile、ShapepolygonDataFrame的步骤,然后尝试将shapefile与dataframe合并-dataframe仅在用户选择一组特定日期时生成 但是,当我绘制地图时,它会显示错误的国家名称和值,这是非

我有一个4列的示例数据集

  • 连接年份
  • 国家
  • 连接方法
  • 用户数量
我正在尝试使用Shining创建一个仪表板,要求用户输入年份下拉列表,然后单击submit按钮。单击“提交”后,输出应为所选年份的传单地图,并反映来自不同国家/地区的用户数量

我遵循了使用maps包获取shapefile、ShapepolygonDataFrame的步骤,然后尝试将shapefile与dataframe合并-dataframe仅在用户选择一组特定日期时生成

但是,当我绘制地图时,它会显示错误的国家名称和值,这是非常随机的。例如,如果您选择2018,则所有值都会正确显示,但当您选择2019或2020时,则会为poloygon分配不正确的国家名称和值

我做错了什么或错过了什么? 我尝试引用,也按照说明进行操作,但没有成功。我已经包含了输出的图像,您可以观察到它绘制了正确的国家多边形,数据集中的国家名称和值不正确,但数据集中的国家多边形存在数据

我真的很感激任何帮助

代码如下:

library(shiny)
library(shinydashboard)
library(lubridate)
library(sp)
library(sf)
library(leaflet)
library(stringr)
library(dplyr)
library(maps)
library(maptools)



#Read the file
my_data <- read.csv(file.choose(),header = T)

my_data$YEAR_OF_CONNECTION <- strptime(my_data$YEAR_OF_CONNECTION,"%Y")


my_data$YEAR_OF_CONNECTION <- as.numeric(format(my_data$YEAR_OF_CONNECTION,"%Y"))



print(my_data)

print(class(my_data$YEAR_OF_CONNECTION))






# Define UI 
ui <- navbarPage("Cell Connectivity",
                 
                 
                 tabPanel("Country based Cellular Connections",
                          sidebarLayout(
                            sidebarPanel(
                              
                              selectInput(
                                inputId =  "val", 
                                label = "Select time period:", 
                                choices = 2014:2021
                              ),
                              
                              br(),
                              br(),
                              
                              
                              submitButton("Submit")
                              
                            ),
                            
                            
                            
                            # 
                            mainPanel(
                              
                              leafletOutput("map")
                              
                              
                            )
                          ) 
                          
                          
                 )
                 
                 
)



# Define server logic required to draw a histogram
server <- function(input, output) {
  
  selected <- reactive({
    
    fildata <- filter(my_data,my_data$YEAR_OF_CONNECTION == input$val)
    fildata
    # my_data[my_data$YEAR_OF_CONNECTION == input$val,]
    # 
    # print(class((input$val)))
    # print(input$val)
    
  })
  
 
  
  
  output$map <- renderLeaflet({
    # Use leaflet() here, and only include aspects of the map that
    # won't need to change dynamically (at least, not unless the
    # entire map is being torn down and recreated).
    leaflet() %>% 
      addProviderTiles("CartoDB.Positron") 
      
      
  })
  
  
  
  
  observe({
    if(!is.null(input$val)){
      
      
      #t2@data <- left_join(t2@data, selected (), by="country")
      
      worldmap <- map("world", fill=TRUE, plot=FALSE)
      
      
      IDs <- sapply(strsplit(worldmap$names, ":"), function(x) x[1])
    
      
      worldmap_poly <- map2SpatialPolygons(worldmap, IDs=IDs, proj4string=CRS("+proj=longlat +datum=WGS84"))
      
      
      
      worldmap_poly_sp <- SpatialPolygonsDataFrame(worldmap_poly,
                                                   data.frame(IDs=names(worldmap_poly), 
                                                              stringsAsFactors=FALSE), FALSE)
      
      
      ##subset the SpatialpolygonDataFrame only for country of interest
      t2 <- subset(worldmap_poly_sp, IDs %in% selected()$country)
      
      
      
     
      
      t2@data <- inner_join(t2@data, selected(), by=character(),copy = TRUE)
      
      # t3 <- merge(t2, selected(),by.x="country",by.y="country",all.y= TRUE)
      
      
      
      leafletProxy("map", data = t2 ) %>%
        
        
        
        #addTiles() %>% 
        clearShapes() %>% 
        addPolygons(data = t2, 
                    #fillColor = "Green",
                    fillOpacity = 0.7, 
                    #color = "Blue", weight = 2,
                    popup = ~ paste0("USER_COUNTS: ", as.character(selected()$USER_COUNTS),
                                     "<br>","<b>",
                                     "COUNTRY:",as.character(selected()$country)))
      
    }
    
  })
  
  
  
  
}

# Run the application 
shinyApp(ui = ui, server = server)
库(闪亮)
图书馆(shinydashboard)
图书馆(lubridate)
图书馆(sp)
图书馆(sf)
图书馆(单张)
图书馆(stringr)
图书馆(dplyr)
图书馆(地图)
图书馆(地图工具)
#读文件
我的数据
 t2@data <- inner_join(t2@data, selected(), by=character(),copy = TRUE)
t3 <- merge(t2@data, selected(),by.x="IDs",by.y="country",all.y= TRUE)
      
      t2@data <- t3
      
      leafletProxy("map", data = t2 ) %>%
        
        
        
        addTiles() %>% 
        clearShapes() %>% 
        addPolygons(data = t2, 
                    #fillColor = "Green",
                    fillOpacity = 0.7, 
                    #color = "Blue", weight = 2,
                    popup = ~ paste0("USER_COUNTS: ", as.character(t2@data$USER_COUNTS),
                                     "<br>","<b>",
                                     "COUNTRY:",as.character(t2@data$IDs)))