使用ggplot2和plotly,我可以轻松地创建一个美国县的等值区并设置自定义工具提示。但是,当我添加状态边界时,工具提示无法在某些情况下呈现。问题似乎与接近国家边界有关。可能导致这种情况的原因,我该如何解决?
首先,添加第二层的示例完全破坏了工具提示。
library(plotly)
library(tidyverse)
### One layer -- works fine
a <- data.frame(group = seq(3085), val = rnorm(3085)) %>%
left_join(ggplot2::map_data("county"), by = "group")
p <- ggplot(a, aes(long, lat, group = group)) +
geom_polygon(aes(fill = val, text = subregion))
ggplotly(p, tooltip="text")
### Two layers -- no tool tips
b <- ggplot2::map_data("state")
p <- ggplot(a, aes(long, lat, group = group)) +
geom_polygon(aes(fill = val, text = subregion)) +
geom_path(data = b, color = "black")
ggplotly(p, tooltip="text")
当我使用更小的等值线(一种状态)时,即使有两层,工具提示也能正常显示。
### Just Delaware: shows tool tips
b <- ggplot2::map_data("state") %>%
filter(region == "delaware")
a <- data.frame(group = seq(3085), val = rnorm(3085)) %>%
left_join(ggplot2::map_data("county"), by = "group") %>%
filter(region == "delaware")
p <- ggplot(a, aes(long, lat, group = group)) +
geom_polygon(aes(fill = val, text = subregion)) +
geom_path(data = b, color = "black")
ggplotly(p, tooltip="text")
具有两层的中等尺寸等值线显示远离状态边界的工具尖端,但不接近它。
### New England: *sometimes* shows tool tips
new_england <- c("maine", "new hampshire", "vermont", "massachusetts", "connecticut", "rhode island")
b <- ggplot2::map_data("state") %>%
filter(region %in% new_england)
a <- data.frame(group = seq(3085), val = rnorm(3085)) %>%
left_join(ggplot2::map_data("county"), by = "group") %>%
filter(region %in% new_england)
p <- ggplot(a, aes(long, lat, group = group)) +
geom_polygon(aes(fill = val, text = subregion)) +
geom_path(data = b, color = "black")
ggplotly(p, tooltip="text")
我注意到切换到“比较悬停时的数据”解决了这个问题,但这对我的读者要求太多,除非有办法在默认情况下启用它。