使用tmap函数显示点的所有属性

问题描述 投票:0回答:2

在此处输入图像描述我正在尝试制作一个闪亮的应用程序,显示华盛顿州贝灵厄姆周围公共果树的位置。当用户单击树位置时,我希望他们能够看到给定树的所有属性,例如状态(已确认/未确认)和纬度/经度坐标。然而,当我创建交互式地图时,我只能看到用于对点进行颜色编码的属性,在本例中为物种。 tmap 图例使物种变得明显,我希望在单击某个点时能够看到状态(已确认/未确认)和纬度/经度坐标。

这是我使用 rshiny 创建一个简单的交互式地图应用程序的代码。我加载了必要的包。

library(dplyr)
library(shiny)
library(leaflet)
library(tmap)
library(sf)

我创建了数据框。

dat <- data.frame(lat = c(48.7323,48.7308,
                          48.7301,48.7276,
                          48.7246,48.7323,
                          48.7211),
long = c(-122.4928,-122.4940,
         -122.4942,-122.4939,
         -122.4958,-122.4975,
         -122.4946),
 species = c("Apple", "Apple",
             "Pear", "Plum",
             "Fig", "Plum",
             "Pear"),
 status = c("Confirmed", "Unconfirmed", "Confirmed", "Confirmed", "Confimed", "Unconfirmed", "Confirmed"))
# created ui and server components 

ui <- fluidPage( mainPanel(
  tmapOutput(outputId = "tmapMap"),
)
)


server <- function(input, output, session) {
  output$tmapMap <- renderTmap({
 
# converted to spatial points df

dat2plot <- st_as_sf(dat2plot, coords = c("long","lat"), crs=st_crs("EPSG:4326"))
 
   # and plotted map 
    
map1 <- tm_shape(dat2plot) +
 tm_dots(col= "species", alpha=0.8,size = 0.1) +
 
  
tm_legend(show = TRUE) +
 tm_view(set.zoom.limits = c(14,16))
    map1

})
 

}

shinyApp(ui = ui, server = server)

所附图片是返回的内容。

r spatial tmap
2个回答
0
投票

这似乎是一个错误,因为它仅在您通过

col
定义单一颜色时才有效。如果您按如下方式调整相应的行,您至少会看到所有属性,但不会出现颜色编码:

# converted to spatial points df
dat2plot <- st_as_sf(dat, coords = c("long","lat"), crs = st_crs("EPSG:4326"), remove = FALSE) %>% 
   relocate(species, .before = lat)

# and plotted map 
map1 <- tm_shape(dat2plot) +
 tm_dots(col= "blue", alpha = 0.8, size = 0.1) +
 tm_legend(show = TRUE) +
 tm_view(set.zoom.limits = c(14,16))

map1

enter image description here


0
投票

您可以使用

popup.vars
tm_dots
参数控制显示的字段,包括当您使用颜色来表示变量时。这需要 TRUE/FALSE 来绘制所有/无变量,或您想要显示的变量的字符向量。

#data per mgrunds' answer
dat2plot <- st_as_sf(dat, coords = c("long","lat"), crs=st_crs("EPSG:4326"), remove = FALSE) %>% 
  relocate(species, .before = lat) # species as the first col to show that when hovering over a point

#simple plot
tm_shape(dat2plot) +
  tm_dots(col= "species", alpha=0.8,size = 0.1,
          popup.vars = c("status", "lat", "long")) #character vector of desired variables

它还允许重命名弹出窗口中的变量:

tm_shape(dat2plot) +
  tm_dots(col= "species", alpha=0.8,size = 0.1,
          popup.vars = c("Status" = "status",
                         "lat",  "long"))

The second code snippet returns this map, showing the named variables.

© www.soinside.com 2019 - 2024. All rights reserved.