首页 > 解决方案 > 在 Shiny 中与 tmap 并排地图

问题描述

我正在尝试使用 Shiny 中的 tmap 包比较两个专题地图。但是我得到一个错误参数长度为零。另外我想通过选择 Shinyinput 面板绘制这两个地图。我的意思是从 4 个输入中可以绘制 4 个绘图的 2 个组合。当 input$hafta 的长度等于 2 时必须绘制绘图。我向您发送数据类型和我写的代码。

PS:我尝试在 tmap 中使用构面,但据我发现闪亮不支持它。谢谢

str(turcov@data)

'data.frame':   81 obs. of  16 variables:
 $ NAME_1   : chr  "Adana" "Adıyaman" "Afyon" "Ağrı" ...
 $ GID_0    : chr  "TUR" "TUR" "TUR" "TUR" ...
 $ NAME_0   : chr  "Turkey" "Turkey" "Turkey" "Turkey" ...
 $ GID_1    : chr  "TUR.1_1" "TUR.2_1" "TUR.3_1" "TUR.4_1" ...
 $ VARNAME_1: chr  "Seyhan" "Adıyaman" "Afyonkarahisar" "Ağri|Karaköse" ...
 $ NL_NAME_1: chr  NA NA NA NA ...
 $ TYPE_1   : chr  "Il" "Il" "Il" "Il" ...
 $ ENGTYPE_1: chr  "Province" "Province" "Province" "Province" ...
 $ CC_1     : chr  NA NA NA NA ...
 $ HASC_1   : chr  "TR.AA" "TR.AD" "TR.AF" "TR.AG" ...
 $ sub4     : num  41.2 116.2 38.4 19 123.1 ...
 $ mart1    : num  63 154.6 47.5 22.2 173.5 ...
 $ fark     : num  52.8 33.1 23.6 16.9 41 ...
 $ kategori : Factor w/ 2 levels "artış","azalış": 1 1 1 1 1 1 1 1 1 1 ...
 $ mart2    : num  63 154.6 47.5 22.2 173.5 ...
 $ mart3    : num  61.6 169.8 47.4 41.5 214.4 ...


library(rgdal)
library(sp)
library(spdep)
library(ggplot2)
library(raster)
library(tmap)
library(plotly)
library(reshape2)
library(shiny)

ui <- fluidPage(
sidebarLayout(
    sidebarPanel(
        selectInput("hafta", "Hafta seçiniz:", choices=c("sub4","mart1","mart2","mart3") 
,selected=NULL,multiple=TRUE,selectize = TRUE)),
        mainPanel(
            tabsetPanel(
                tabPanel("İki hafta il sınıflandırma karşılaştırması", 
                         tmapOutput("map1", width = "100%", height = 800)), 
                tabPanel("Artış-azalış ve Artış-azalış yüzdeleri", 
                         tmapOutput("map2", width = "100%", height = 800)),
                tabPanel("İl ve komşuları haftalık vaka trendi", 
                         plotlyOutput("map3", width = "100%", height = 800))
                
              )
        )))
server <- function(input, output) {

output$map1<-renderTmap({ print(input$hafta)
breaks<-c(0,20,50,100,Inf)
renk=c("blue","yellow","orange","red")
tmap1<-tmap_mode("view")+
    tm_shape(turcov) +
    tm_fill(input$hafta[1],breaks = breaks, 
            title=c(" İllere göre 100 binde vaka sayıları(input$hafta[1])"),palette = renk) +
    tm_borders() +
    tm_text("NAME_1")
  
tmap2<-tmap_mode("view")+
    tm_shape(turcov) +
    tm_fill(input$hafta[2],breaks = breaks, 
            title=c(" İllere göre 100 binde vaka sayıları(input$hafta[1])"),palette = renk) +
    tm_borders() +
    tm_text("NAME_1")

  tmap_arrange(tmap1,tmap2,ncol=2)
   })
  } 
shinyApp(ui = ui, server = server)

标签: rshinytmap

解决方案


#UI
tabPanel("İki hafta il sınıflandırma karşılaştırması", fluidRow(
                        column(6, tmapOutput("map1", width = "100%", height = 800)),
                        column(6, tmapOutput("map2", width = "100%", height = 800))))
#SERVER
output$map1<-renderTmap({ 
breaks<-c(0,20,50,100,Inf)
renk=c("blue","yellow","orange","red")
tmap1<-tmap_mode("view")+
tm_shape(turcov) +
tm_fill(input$hafta[1],breaks = breaks, 
        title=c(" İllere göre 100 binde vaka sayıları(input$hafta[1])"),palette = 
renk) +
tm_borders() +
tm_text("NAME_1")
output$map2<-renderTmap({ 
tmap1<-tmap_mode("view")+
tm_shape(turcov) +
tm_fill(input$hafta[2],breaks = breaks, 
        title=c(" İllere göre 100 binde vaka sayıları(input$hafta[1])"),palette = 
renk) +
tm_borders() +
tm_text("NAME_1")
})

好吧,只要我了解闪亮不支持构面类型的地图,我就通过将 plotouput 分成两列来根据个人地块的 tmap 建议进行绘图。这解决了我的问题。


推荐阅读