gpt4 book ai didi

r - plotly 中 plot_geo 函数的悬停文本

转载 作者:行者123 更新时间:2023-12-04 09:26:24 24 4
gpt4 key购买 nike

我想在悬停时显示县名(子区域)和人口值(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

虽然我给了 text = ~pop_cat, hoverinfo = 'text'plot_geo功能,当我将鼠标悬停在绘图上时,它不会显示。我应该怎么做才能同时显示 pop_catsubregion当我将鼠标悬停在 plotly 上时。

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

enter image description here

最佳答案

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

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

关于r - plotly 中 plot_geo 函数的悬停文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48902321/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com