【问题标题】:Hover text for plot_geo function in plotlyplotly 中 plot_geo 函数的悬停文本
【发布时间】:2018-02-21 09:29:19
【问题描述】:

我想悬停显示县名(子区域)和人口值(pop_cat)。

这是我尝试过的,

  library(tidyverse)
    library(plotly)

    df <- read.csv("https://raw.githubusercontent.com/bcdunbar/datasets/master/californiaPopulation.csv")

    cali <- map_data("county") %>%
      filter(region == 'california')

    pop <- df %>%
      group_by(County.Name) %>%
      summarise(Pop = sum(Population))

    pop$County.Name <- tolower(pop$County.Name) # matching string

    cali_pop <- merge(cali, pop, by.x = "subregion", by.y = "County.Name")

    cali_pop$pop_cat <- cut(cali_pop$Pop, breaks = c(seq(0, 11000000, by = 500000)), labels=1:22)

geo <- list(
  scope = 'usa',
  showland = TRUE,
  landcolor = toRGB("gray95"),
  countrycolor = toRGB("gray80")
)

library(plotly)

geo <- list(
  scope = 'usa',
  showland = TRUE,
  landcolor = toRGB("gray95"),
  countrycolor = toRGB("gray80")
)

p <- cali_pop %>%
  group_by(group) %>%
  plot_geo(
    x = ~long, y = ~lat, color = ~pop_cat, colors = c('#ffeda0','#f03b20'),
    text = ~pop_cat, hoverinfo = 'text') %>%
  add_polygons(line = list(width = 0.4)) %>%
  add_polygons(
    fillcolor = 'transparent',
    line = list(color = 'black', width = 0.5),
    showlegend = FALSE, hoverinfo = 'none'
  ) %>%
  layout(
    title = "California Population by County",
    geo = geo)

p

虽然我在plot_geo 函数中给出了text = ~pop_cat, hoverinfo = 'text',但当我将鼠标悬停在绘图上时它没有显示出来。当我将鼠标悬停在绘图上时,我应该怎么做才能同时显示 pop_catsubregion

这是生成的情节。我放大了加利福尼亚地区。

【问题讨论】:

  • 删除 hoverinfo = 'none'hoverinfo = 'text'。而且你在df中没有`subregionìn。
  • @MLavoie 谢谢。我现在收到pop_cat。它还显示了我不想显示的坐标。您能否建议如何删除坐标并添加县名。 subregion 是县名。它发生在这里,cali &lt;- map_data("county") %&gt;% filter(region == 'california')
  • @MLavoie 谢谢!它成功了 :)
  • text = ~pop_cat 似乎又可以工作了

标签: r plot maps plotly


【解决方案1】:

plotly 中存在某种错误。正如@MLavoie 所说,您可以在这里找到解决方案https://github.com/ropensci/plotly/issues/1152

我尝试了 plotly 的开发版本,它已修复。还要显示我使用的县名和人口text = ~paste(cali_pop$subregion, "&lt;br /&gt;", cali_pop$pop_cat)

【讨论】:

    【解决方案2】:

    如果您在从开发人员版本安装后仍然难以悬停,并且如果错误显示类似于Error: package or namespace load failed for ‘plotly’ in get(Info[i, 1], envir = env): lazy-load database,只需刷新 R-session,这对我有用。

    @Harikrishnan 为您解答。它帮助了我。

    【讨论】:

      猜你喜欢
      • 2021-04-19
      • 1970-01-01
      • 2016-10-30
      • 2018-06-25
      • 2018-10-06
      • 2021-04-04
      • 2018-09-19
      • 2022-01-09
      • 2018-09-04
      相关资源
      最近更新 更多