0
votes

So I copied the example off of plotly for R on making a bubble map. I can currently make a bubble map but I am unable to make hoverinfo work. I have seen on other posts that hoverinfo has given other people problems but none of the fixes I have found is making mine work. I have given a small amount of the data that I am using below.

Can anybody who knows plotly see if I am making a small mistake that I am not seeing?

Data

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)

Code

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
If I just run your code as is, with or without hoverinfo, I don't get any reasonable-looking plotRolandASc
without this hoverinfo = "text+x+y" it works fine. what do you want with: hoverinfo = "text+x+y"MLavoie
I tried without hoverinfo and its still not giving me info when i place the mouse over the points. I want similar effect as the example plotly gives but with my data.Clinton Woods

1 Answers

0
votes

I found out what the problem with getting the boxes to pop up. I had a few data points that were in Canada and a few in mexico. The map i was using for plotly was a US map only, i still had points plotting outside the map but the boxes did not pop up when the mouse hovered over. I had to change to a north america sized map.

Code:

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