0

因此,我在制作气泡图时复制了 R 的 plotly 示例。我目前可以制作气泡图,但无法使 hoverinfo 工作。我在其他帖子上看到 hoverinfo 给其他人带来了问题,但我发现的修复都没有让我的工作。我在下面给出了我正在使用的少量数据。

任何知道情节的人都可以看看我是否犯了一个我没有看到的小错误吗?

数据

All_Time_Backers_city <- c(871,25,478,25,14,193)
Latitude <- c(32.44861,42.10472,42.48500,34.06583,34.77444,41.93167)
Longitude <- c(-99.73278,-70.94583,-71.43333,-84.67694,-96.67806,-87.98889)
City <- c("Abilene","Abington","Acton","Acworth","Ada","Addison")
z <- data.frame(All_Time_Backers_city, Latitude, Longitude, City)

代码

library(plotly)
z$q <- with(z, cut(All_Time_Backers_city, quantile(All_Time_Backers_city)))
levels(z$q) <- paste(c("1st", "2nd", "3rd", "4th", "5th"), "Quantile")
z$q <- as.ordered(z$q)

g <- list(
  scope = 'usa',
  projection = list(type = 'albers usa'),
  showland = TRUE,
  landcolor = toRGB("gray85"),
  subunitwidth = 1,
  countrywidth = 1,
  subunitcolor = toRGB("white"),
  countrycolor = toRGB("white")
)

p <- plot_geo(z, locationmode = 'USA-states', sizes = c(5, 250)) %>%
   add_markers(
     x = ~Longitude, y = ~Latitude, size = ~All_Time_Backers_city, color = 
~q,
     text = ~paste(City, "<br />", All_Time_Backers_city, "Backers"), 
hoverinfo = "text+x+y"
    )%>%
   layout(title = 'Backers City All Time', geo = g)
p
4

1 回答 1

0

我发现了弹出框的问题。我有一些数据点在加拿大,一些在墨西哥。我用于 plotly 的地图只是美国地图,我仍然在地图外绘制点,但是当鼠标悬停时框没有弹出。我不得不换成北美大小的地图。

代码:

z$q <- with(z, cut(All_Time_Backers_city, quantile(All_Time_Backers_city)))
levels(z$q) <- paste(c("1st", "2nd", "3rd", "4th", "5th"), "Quantile")
z$q <- as.ordered(z$q)

g <- list(
  scope = 'north america',
  showland = TRUE,
  landcolor = toRGB("grey83"),
  subunitcolor = toRGB("white"),
  countrycolor = toRGB("white"),
  showlakes = TRUE,
  lakecolor = toRGB("white"),
  showsubunits = TRUE,
  showcountries = TRUE,
  resolution = 50,
  projection = list(
    type = 'conic conformal',
    rotation = list(lon = -100)
  ),
  lonaxis = list(
    showgrid = TRUE,
    gridwidth = 0.5,
    range = c(-140, -55),
    dtick = 5
  ),
  lataxis = list(
    showgrid = TRUE,
    gridwidth = 0.5,
    range = c(15, 70),
    dtick = 5
  )
)

p <- plot_geo(z, sizes = c(5, 250))%>%
  add_markers(x = ~Longitude, y = ~Latitude,
      size = ~All_Time_Backers_city, 
     color = ~q, text = ~paste(z$City, "<br />", z$All_Time_Backers_city, 
"Backers")
   )%>%
  add_trace(
    z = ~Mapping$`Mean Campaign USD`, text = ~Mapping$hover, locations = 
~Mapping$code
  ,locationmode="USA-states")%>%
   layout(title = 'Backers City All Time', geo = g)
p 
于 2018-02-06T06:49:53.593 回答