如何在 plotly 地图中更改属性 table popup/hover 文本

How to change the attribute table popup/hover text in a plotly map

我正在尝试从 shapefile 制作交互式 地图,其中 hover/tooltip 显示一些信息。

目前,当我将鼠标悬停在地图上的某个区域时,它只会提供 VAR1(A、B、C、D)的信息,而不是 DATA$HOVER

中的信息

地图(在我的代码中称为 NUTS1)来自:http://geoportal.statistics.gov.uk/datasets/nuts-level-1-january-2018-full-clipped-boundaries-in-the-united-kingdom)

我无法访问 SF。

sessionInfo()
R version 3.4.0 Patched (2017-05-31 r72750)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

packageVersion("plotly")
[1] ‘4.7.1’

packageVersion("ggplot2")
[1] ‘2.2.1’

DATA <- c(NA)
DATA$NUTS1 <- c("UKC","UKD","UKE","UKF","UKG","UKH","UKI","UKJ","UKK","UKL","UKM",
"UKN")
DATA$VAR1  <- c("D","D","A","D","A","C","B","C","B","C","B","A")
DATA$VAR2  <- c("8","4","1","10","4","7","5","1","8","6","3","8")
DATA$VAR3  <- c("1","2","3","4","5","6","7","8","9","10","11","12")
NUTS1 <- readOGR(dsn = 
          "NUTS_Level_1_January_2018_Full_Clipped_Boundaries_in_the_United_Kingdom",
                 layer = 
          "NUTS_Level_1_January_2018_Full_Clipped_Boundaries_in_the_United_Kingdom")
NUTS1 <- fortify(NUTS1, region = "nuts118cd")
DATA$HOVER <- with(DATA, paste(NUTS1, "<br>", VAR1, "<br>", VAR2, "<br>", VAR3))

p <- ggplot(data = DATA, text = HOVER) + 
  geom_map(data = DATA, aes(map_id = NUTS1, fill = VAR1), map = NUTS1) + 
  expand_limits(x = NUTS1$long, y = NUTS1$lat)

p <- ggplotly(p, tooltip = "text")

我认为,首先,您的 DATA 对象应该是一个 data.frame(从您的代码中我得到一个列表,ggplot() 函数不接受该列表)。

然后,尝试将列 HOVER 映射到 geom_map() 而不是 ggplot() 中的 text 审美。

此外,在您的 DATA data.frame 中,将列 NUTS1 重命名为 id,使其与 [=] 中的列名称 id 相匹配21=] data.frame 通过 fortify 获得(从 help(geom_map) 中的示例可以看出这很重要)。

library(ggplot2)
library(plotly)
library(rgdal)

DATA <- data.frame(id = c("UKC","UKD","UKE","UKF","UKG","UKH","UKI","UKJ","UKK","UKL","UKM","UKN"),
                   VAR1 = c("D","D","A","D","A","C","B","C","B","C","B","A"),
                   VAR2 = c("8","4","1","10","4","7","5","1","8","6","3","8"),
                   VAR3 = 1:12)
DATA$HOVER <- with(DATA, paste(id, "<br>", VAR1, "<br>", VAR2, "<br>", VAR3))

NUTS1 <- readOGR(dsn = "NUTS_Level_1_January_2018_Full_Clipped_Boundaries_in_the_United_Kingdom",
                 layer = "NUTS_Level_1_January_2018_Full_Clipped_Boundaries_in_the_United_Kingdom")
NUTS1 <- fortify(NUTS1, region = "nuts118cd")

p <- ggplot(data = DATA) + 
  geom_map(aes(map_id = id, fill = VAR1, text = HOVER), map = NUTS1) +
  expand_limits(x = NUTS1$long, y = NUTS1$lat)

p2 <- ggplotly(p)
p2


请注意,使用 mapview package. For your particular issue, check mapview popups 可以更直接地制作交互式地图。这是 mapview:

的尝试
library(mapview)
library(rgdal)

NUTS1 <- readOGR(dsn = "NUTS_Level_1_January_2018_Full_Clipped_Boundaries_in_the_United_Kingdom",
                 layer = "NUTS_Level_1_January_2018_Full_Clipped_Boundaries_in_the_United_Kingdom")

DATA <- data.frame(NUTS1 = c("UKC","UKD","UKE","UKF","UKG","UKH","UKI","UKJ","UKK","UKL","UKM","UKN"),
                   VAR1 = c("D","D","A","D","A","C","B","C","B","C","B","A"),
                   VAR2 = c("8","4","1","10","4","7","5","1","8","6","3","8"),
                   VAR3 = 1:12)
# merge DATA with the SpatialPolygonsDataFrame NUTS1
nuts <- merge(x = NUTS1, y = DATA, by.x = "nuts118cd", by.y = "NUTS1")

mapview(nuts, popup = popupTable(nuts, zcol = c("nuts118cd", "VAR1", "VAR2", "VAR3")))

要填写 VAR1,您需要跟随 this example 并在 mapview 中调用 zcol = "VAR1",例如:

mapview(nuts, zcol = "VAR1", 
        popup = popupTable(nuts, zcol = c("nuts118cd", "VAR1", "VAR2", "VAR3")))