自定义 tooltip/hovertext 用于通过 R plotly 创建的等值线

custom tooltip/hovertext for choropleth created with plotly via R

我是一名经验丰富的 R 用户,尝试使用 plotly-r。 (我也是使用地理数据的新手。)我正在尝试构建一个交互式等值线,并且我已经能够非常接近我想要的 ggplot() >> ggplotly(),但是我也希望能够从 plot_ly().

开始做同样的事情

我在使用工具提示时遇到问题。我已经能够使用第一个工作流程设置自定义工具提示,但在从 plot_ly() 开始时我不知道该怎么做。我的代码如下。工具提示显示从 split 参数到 add_sf() 的值。我希望它显示 tmp1$text.

中包含的字符串
tmp0 <-
    ct.covid.positivity.0 %>%
    select(Town, town.positivity, pop.2010, tests.10k)

tmp1 <-
    ct.shp %>%
    left_join(tmp0, by=c("NAME10" = "Town")) %>%
    mutate(text=paste0(NAME10,
            "\nTest Pos: ", formatC(town.positivity, format="f", digits=2), "%",
            "\nPopulation: ", formatC(pop.2010, format="d"),
            "\nTests/10k/day: ", formatC(tests.10k/10, format="f", digits=2)))

map.positivity.plotly  <-
    plot_ly(data=tmp1,                  # plot_ly and plot_geo both get coordinate system wrong in different ways (cf. ggplotly)
            hoverinfo='text',
            text=~text) %>%
    add_sf(
        split=~NAME10,                  # hover text seems determined by split variable
        color=~town.positivity,
        colors="magma",                 # set colorscale
        alpha=1,
        stroke=I("grey90")
    ) %>%
    config(displayModeBar = FALSE) %>%
    layout(hovermode = "closest",
           hoverlabel = list(bgcolor="darkgreen",
                             bordercolor="black",
                             font=list(color="white", family = "Roboto Condensed", size=15)
                             ),
           title = list(text = paste("<b>10 Day Average Covid-19 Test Positivity in Connecticut Towns\nfor period ending",
                                     format(max(ct.covid$Date), "%B %d, %Y"), "</b>"),
                        x = 0,
                        xanchor = "left",
                        yanchor = "top",
                        pad = list(b=10, l=10, r=10, t=10))) %>%
    colorbar(title = "<b>Test Positivity (%)</b>") %>%
    hide_legend()              

您可以在此处查看结果:https://davebraze.github.io/ct-covid-map-examples/#plot_ly,在 Plot_ly 选项卡上。 (查看 Ggplotly 选项卡了解接近我想做的事情的想法。)

我的问题:如何调整我的代码,使用 plot_ly() 在鼠标悬停在每个城镇上时弹出的工具提示中显示 tmp1$text 的内容?任何帮助将不胜感激。

如果您包含数据样本,我猜您会在发布后几分钟内得到答案...

你们太亲密了。您缺少一个参数。在 plotly 调用中添加 hoveron = "fills"

(map.positivity.plotly  <-
  plot_ly(data=tmp1,        
          hoverinfo='text',
          hoveron = "fills",     #################   <--- add me!!
          text=~text) %>%
  add_sf(
    split=~NAME10,           # hover text seems determined by split variable
    color=~town.positivity,
    colors="magma",          # set colorscale
    alpha=1,
    stroke=I("grey90")
  ) %>%
  config(displayModeBar = FALSE) %>%
  layout(hovermode = "closest",
         hoverlabel = list(bgcolor="darkgreen",
                           bordercolor="black",
                           font=list(color="white", 
                                     family = "Roboto Condensed", 
                                     size=15)
         ),
         title = list(text = paste("<b>10 Day Average Covid-19 Test Positivity in Connecticut Towns\nfor period ending",
                                   format(max(ct.covid$Date), "%B %d, %Y"), "</b>"),
                      x = 0,
                      xanchor = "left",
                      yanchor = "top",
                      pad = list(b=10, l=10, r=10, t=10))) %>%
  colorbar(title = "<b>Test Positivity (%)</b>") %>%
  hide_legend())