Reputation: 152
I am trying to make an interactive plotly map from a shapefile, with a hover/tooltip that shows some information.
Currently when I hover over an area on the map it only gives the information for VAR1
(A,B,C,D) rather than the information in DATA$HOVER
The map (Called NUTS1
in my code) is obtained from: http://geoportal.statistics.gov.uk/datasets/nuts-level-1-january-2018-full-clipped-boundaries-in-the-united-kingdom)
I do not have access to 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")
Upvotes: 1
Views: 987
Reputation: 6426
I think, first of all, your DATA
object should be a data.frame
(from your code I get a list, which is no accepted by the ggplot()
function).
Then, try to map the column HOVER
into the text
aesthetic in geom_map()
and not in ggplot()
.
Also, in your DATA
data.frame, rename the column NUTS1
to id
, so that it matches the column name id
from NUTS1
data.frame obtained with fortify
(you see that this is important from the examples found in 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
Note that, interactive maps are more straightforward to make with the mapview
package. For your particular issue, check mapview popups. Here is an attempt with 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")))
To fill by VAR1
, you need to follow this example and call zcol = "VAR1"
in mapview
, like:
mapview(nuts, zcol = "VAR1",
popup = popupTable(nuts, zcol = c("nuts118cd", "VAR1", "VAR2", "VAR3")))
Upvotes: 3