0
votes

I found the following example for plotting a map with Canadian cities: https://plot.ly/ggplot2/maps/

The R code reads as follows:

library(plotly)
Sys.setenv("plotly_username"="XXXXXXXXX")
Sys.setenv("plotly_api_key"="YYYYYYYYY")

  data(canada.cities, package="maps")
  viz <- ggplot(canada.cities, aes(long, lat)) +
  borders(regions="canada", name="borders") +
  coord_equal() +
  geom_point(aes(text=name, size=pop), colour="red", alpha=1/2, name="cities")

  ggplotly()

I would like to modify the example in a way that the name of the city shows up when hovering with the mouse over the relevant point in the map.

How would I need to modify the above example to implement this?

1
Plotly brings over the ggplot2 figure. Can you add a column? You can edit the tooltip as well. - Mateo Sanchez
Thanks for your comment! After all, I think I prefer to use another way to produce the map in plotly, here are some nice examples link - kanimbla

1 Answers

2
votes

This ended up being a bug, so thanks for reporting! I just issued a fix here so try re-installing (devtools::install_github("ropensci/plotly")) and re-running:

data(canada.cities, package="maps")
viz <- ggplot(canada.cities, aes(long, lat)) +
        borders(regions="canada", name="borders") +
        coord_equal() +
        geom_point(aes(text=name, size=pop), colour="red", alpha=1/2, name="cities")
        ggplotly()

Here's a screenshot, with the custom hover text!

custom hovertext with ggplot2 figure