【问题标题】:Change hover message in ggplotly map更改ggplotly地图中的悬停消息
【发布时间】:2021-04-30 10:34:12
【问题描述】:

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

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

但是现在我需要找到如何覆盖mapa_prov.df2而不影响hover

编辑 4 链接到mapa_prov.df2mapa_prov.csv中的数据

【问题讨论】:

  • 请添加dput(head(mapa_df2))的输出。
  • 你的第二个dfmapa.prov_df2怎么样? nombre 也是这个数据集的一列吗?
  • @stefan 是 nombre 是数据集中的一列。它是我想在鼠标悬停时显示的自治市名称,填充多边形的颜色。
  • 当我在没有geom_polygon(使用mapa.prov_df2)的情况下运行您的代码时,它可以正常工作。我没有收到任何错误。由于您尚未提供第二个数据集,我猜想第二个数据集不包含名为 nombre 的列,这将引发与您收到的相同的错误消息。
  • 是的,第二个数据集是省界只是为了叠加。我收到的错误消息是Error in FUN(X[[i]], ...) : object 'nombre' not found。如果我从 ggplot 中删除`text = nombre`,它工作正常,但工具提示显示trace : value,值 o 到 3 分配给每个多边形的值。 map_prov.df2 可以从情节中删除,我认为它与工具提示问题无关。

标签: r plotly r-plotly ggplotly


【解决方案1】:

(: "nombre variable is not present here"?这是我在第一条评论中提出的问题以及错误的原因。您放入 ggplot(aes(...)) 的每个变量都必须存在于 each 数据框,即这些是全局美学。因此,添加 mapa_prov.df2 时会收到错误消息,因为 ggplot2 也在此数据集中查找变量 nombre。为了防止您必须将 text 设为本地 aes的geom_polygon,即做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")

【讨论】:

  • 那是我的错误,认为数据框在绘图时是独立的,因为它们是单独调用的。现在它工作正常。谢谢。
猜你喜欢
  • 2020-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-13
  • 1970-01-01
  • 2010-11-25
  • 1970-01-01
相关资源
最近更新 更多