Plotly hoverinfo不会在R中工作

时间:2018-02-04 23:21:45

标签: r hover plotly r-plotly

因此,我在制作气泡图时将R的图示复制为R。我现在可以制作气泡图,但我无法使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

1 个答案:

答案 0 :(得分:0)

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

代码:

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