简体   繁体   English

R-绘图-ggplot

[英]R - plotly - ggplot

I found the following example for plotting a map with Canadian cities: https://plot.ly/ggplot2/maps/ 我发现了以下示例,用于绘制带有加拿大城市的地图: https : //plot.ly/ggplot2/maps/

The R code reads as follows: R代码如下:

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? 我需要如何修改以上示例来实现此目的?

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: 我只是在这里发布了一个修复程序,因此尝试重新安装( devtools::install_github("ropensci/plotly") )并重新运行:

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! 这是截图,带有自定义悬停文字!

带有ggplot2图的自定义悬浮文本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM