如何在绘图图中更改属性表弹出/悬停文本

时间:2019-01-03 15:57:02

标签: r ggplot2 hover tooltip r-plotly

我正在尝试从shapefile制作交互式地图,并使用悬停/工具提示显示一些信息。

当前,当我将鼠标悬停在地图上的某个区域时,它只会提供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版本3.4.0已修补(2017-05-31 r72750)
  平台:x86_64-w64-mingw32 / x64(64位)
  在以下系统上运行:Windows 7 x64(内部版本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")

1 个答案:

答案 0 :(得分:1)

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

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

还要在您的DATA data.frame中,将列NUTS1重命名为id,以使其与id数据中的列名NUTS1相匹配.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

enter image description here


请注意,使用mapview package可以轻松制作交互式地图。对于您的特定问题,请检查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")))

enter image description here

要填充VAR1,您需要遵循this example并在zcol = "VAR1"中呼叫mapview,例如:

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