1

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")
4

1 回答 1

2

layer_paths您对数据框进行分组时group,您使用fill总数中的一个,您还可以在原始ggvis调用中选择 long 和 lat。因此,您进入的数据框add_tooltip正好包含那些列,即没有name列。

快速检查显示每个组只对应一个名称,如下所示:

> 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

因此,只需将 group 参数更改为name而不是 group 似乎有效。见下面的代码:

#I have added a fake total column
jor$total <- runif(983) * 100

这段代码有效:

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")

在此处输入图像描述

实际上,我认为总列实际上是lat您的情况下的列(从as.character(data$lat))您写的情况来看add_tooltip),在这种情况下,

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")

无论如何都会工作。

于 2015-10-26T23:14:52.317 回答