So I am working on an interactive map of Jordan, to display registered Syrian refugees per governorate. The interactivity added is pretty simple; using add_tooltip, once you hover over a governorate it shows the name and the total number of refugees. However it does not show the name. When I use print or format on it, it then shows 'NULL'. What could be the reason?
A reproducible code can be found below:
library(choroplethrAdmin1)
library(choroplethr)
library(ggvis)
library(dplyr)
jor<-get_admin1_map("jordan")
jor %>% ggvis(~long, ~lat) %>%
layer_paths(data = jor %>% group_by(group),
strokeWidth := 0, fill = ~total) %>%
hide_axis("x") %>% hide_axis("y") %>%
add_tooltip(function(data){paste("Gov: ", data$name, "<br>", "Total: ", as.character(data$lat))}, "hover")
On layer_paths
you group the data frame by group
and you use a fill
of the total and you also choose long and lat in your original ggvis
call. Therefore, your data frame going in add_tooltip
contains exactly those columns i.e. there is no name
column.
A fast check showed me that there each group corresponds to only one name as you can see below:
> table(jor$group, jor$name)
ajlun amman aqaba balqa irbid jarash karak ma`an madaba mafraq tafilah zarqa
2009.1 0 0 87 0 0 0 0 0 0 0 0 0
2010.1 0 0 0 0 0 0 0 0 0 115 0 0
2011.1 0 102 0 0 0 0 0 0 0 0 0 0
2012.1 0 0 0 0 0 0 0 0 0 0 67 0
2013.1 0 0 0 0 0 0 0 70 0 0 0 0
2014.1 0 0 0 0 159 0 0 0 0 0 0 0
2015.1 41 0 0 0 0 0 0 0 0 0 0 0
2016.1 0 0 0 0 0 42 0 0 0 0 0 0
2017.1 0 0 0 80 0 0 0 0 0 0 0 0
2018.1 0 0 0 0 0 0 0 0 60 0 0 0
2019.1 0 0 0 0 0 0 74 0 0 0 0 0
2020.1 0 0 0 0 0 0 0 0 0 0 0 86
Therefore, just changing the group argument to name
instead of group seems to work. See below the code:
#I have added a fake total column
jor$total <- runif(983) * 100
and this code works:
jor %>% ggvis(~long, ~lat) %>%
layer_paths(data = jor %>% group_by(name),
strokeWidth := 0, fill = ~total) %>%
hide_axis("x") %>% hide_axis("y") %>%
add_tooltip(function(data){paste("Gov: ", data$name, "<br>", "Total: ", as.character(data$lat))}, "hover")
Actually, I think that the total column is actually the lat
column in your case (judjing from as.character(data$lat))
that you write in add_tooltip
) in which case doing
jor %>% ggvis(~long, ~lat) %>%
layer_paths(data = jor %>% group_by(name),
strokeWidth := 0, fill = ~lat) %>%
hide_axis("x") %>% hide_axis("y") %>%
add_tooltip(function(data){paste("Gov: ", data$name, "<br>", "Total: ", as.character(data$lat))}, "hover")
will work anyway.