更改 ggplotly 地图中的悬停消息

Change hover message in ggplotly map

我设法绘制了一张地图,其中每个城市都根据数据框中列的值填充了不同的颜色。这是我用于绘图的代码:

mapa_df2 %>%
  ggplot(aes(x = long, y = lat, group = group)) +
  geom_polygon(color = "black",aes(fill = nivel1)) +
  coord_map("mercator") +
  labs(title = "Day 1 level map") +
  scale_fill_gradientn("",colours=c("green","yellow","orange","red"),na.value = "transparent",
                       breaks=c(0,1,2,3),labels=c("Normal","Moderado","Alto","Extremo"),
                       limits=c(0,3)) 

现在我正在尝试使用 plotly 转换为交互式地图。

p <- mapa_df2 %>% 
  ggplot(aes(x = long, y = lat, group = group, text = nombre)) +
  geom_path(data=mapa.prov_df2, aes(x=long, y=lat, group=group),color="blue", size=1.5) +
  geom_polygon(color = "white",aes(fill = nivel1)) +
  coord_map("mercator") +
  labs(title = "Day 1 level map") +
  scale_fill_gradientn("",colours=c("green","yellow","orange","red"),na.value = "transparent",
                       breaks=c(0,1,2,3),labels=c("Normal","Moderado","Alto","Extremo"),
                       limits=c(0,3)) 


ggplotly(p, width = 748, height = 1024, tooltip = "text")

然后我收到一条错误消息:Error in FUN(X[[i]], ...) : object 'nombre' not foundnombre 是数据框 mapa_df2 中的列之一,它实际上存在。

我希望当鼠标悬停时出现自治市的名称。如果我从上面的代码中删除 text=nombre,那么我会在悬停消息中得到 trace: valuetooltiptext= 肯定有错误。任何提示或想法都将非常受欢迎。

head(mapa_df2)

的输出
        long      lat   order  hole piece          id         group nombre_municipio c_autonoma provincia municipio zona  nombre
1 -0.1963169 38.85944 2421544 FALSE     1 34100303001 34100303001.1       l'Atzúbia         10        03     03001   20 Adsubia
2 -0.1959364 38.86003 2421545 FALSE     1 34100303001 34100303001.1       l'Atzúbia         10        03     03001   20 Adsubia
3 -0.1957341 38.86049 2421546 FALSE     1 34100303001 34100303001.1       l'Atzúbia         10        03     03001   20 Adsubia
4 -0.1957434 38.86084 2421547 FALSE     1 34100303001 34100303001.1       l'Atzúbia         10        03     03001   20 Adsubia
5 -0.1976370 38.86632 2421548 FALSE     1 34100303001 34100303001.1       l'Atzúbia         10        03     03001   20 Adsubia
6 -0.1939176 38.86917 2421549 FALSE     1 34100303001 34100303001.1       l'Atzúbia         10        03     03001   20 Adsubia
  nivel1 nivel2 nivel3
1      1      1      1
2      1      1      1
3      1      1      1
4      1      1      1
5      1      1      1
6      1      1      1

编辑 1 由于 dput 的输出太大,只是添加了一个 link 到脚本中使用的样本数据 Link to sample data

编辑 2 图形输出。在标签上写着 'trace 0' 的地方,我想替换为 nombre 列,它会显示市政当局的名称。

编辑 3 正如@stefan 所说,运行 没有调用 geom_path 并且经过一些更改 ggplot 运行良好

p <- mapa_df2 %>% 
  ggplot(aes(x = long, y = lat, group = group, fill = nivel1,
                       text = nombre)) +
  geom_polygon(color = "white") +
  coord_map("mercator") +
  scale_fill_gradientn("",colours=c("green","yellow","orange","red"),na.value = "transparent",
                       breaks=c(0,1,2,3),labels=c("Normal","Moderado","Alto","Extremo"),
                       limits=c(0,3)) 

但现在我需要找到如何在不影响 hover

的情况下覆盖 mapa_prov.df2

编辑 4 Link 到 mapa_prov.df2 mapa_prov.csv

中的数据

(: "nombre variable is not present here"?这是我在第一条评论中提出的问题,也是错误的原因。你输入 ggplot(aes(...)) 的每个变量都必须出现在 each 数据框,即这些是全局美学。因此,添加 mapa_prov.df2 时会收到错误消息,因为 ggplot2 也在该数据集中查找变量 nombre。为了防止您必须使 text 成为 geom_polygon 的本地 aes,即 geom_polygon(color = "white",aes(fill = nivel1, text=nombre)).

library(plotly) 

mapa_df2 <- readr::read_csv("CSV_data.csv")
mapa.prov_df2 <- readr::read_csv("mapa_prov.csv")

ggplot(mapa_df2, aes(x = long, y = lat, group = group)) +
  geom_path(data= mapa.prov_df2, aes(x=long, y=lat, group=group),color="blue", size=1.5) +
  geom_polygon(color = "white",aes(fill = nivel1, text = nombre)) +
  coord_map("mercator") +
  labs(title = "Day 1 level map") +
  scale_fill_gradientn("",colours=c("green","yellow","orange","red"),na.value = "transparent",
                       breaks=c(0,1,2,3),labels=c("Normal","Moderado","Alto","Extremo"),
                       limits=c(0,3)) 


ggplotly(width = 748, height = 1024, tooltip = "text")