r - 在 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)
解决方案
#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 建议进行绘图。这解决了我的问题。
推荐阅读
- django - 如何在 Django 中为模型计算两个外键
- javascript - 超时后中断并重试函数执行
- python - 机器人框架不允许我打印多行
- firebase-remote-config - Firebase 远程配置 - 条件参数无法选择“用户属性”
- chart.js - ChartJs 星期几来自数字
- python - 如何根据错误值找到两个熊猫系列之间的交集
- python - 使用python浏览给定目录中的文件,逐行读取每个文件并删除该行中的第一个和最后一个字符串并保存更新的文件
- swift - 成功登录后 SwiftUI Firebase 身份验证关闭视图
- javascript - 如何在铯中使用折线创建轨道线?
- python - Tkinter:多项选择测验应用程序,无法更改问题(和其他问题)