0

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?

2
  • Plotly brings over the ggplot2 figure. Can you add a column? You can edit the tooltip as well. Commented Aug 11, 2015 at 21:23
  • 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 Commented Aug 13, 2015 at 14:17

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for fixing it Chris!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.