【问题标题】:ggmap with value showing on the countries在国家/地区显示值的ggmap
【发布时间】:2017-08-19 09:07:46
【问题描述】:

我正在就某一列上的国家给定样本数据寻求一些帮助,并在另一列上进行计数。我正在尝试使用 ggplot 构建地理地图,当我将鼠标悬停在国家上空时,在地图的各个位置显示国家的数量和名称。以下是给出的样本数据。我尝试使用具有纬度和经度位置的 ggmap 来识别国家,但无法在悬停时显示国家的计数和名称。

structure(list(Countries = c("USA", "India", "Europe", "LATAM", 
"Singapore", "Phillipines", "Australia", "EMEA", "Malaysia", 
"Hongkong", "Philippines", "Thailand", "New Zealand"
), count = c(143002, 80316, 33513, 3736, 2180, 1905, 1816, 921, 
707, 631, 207, 72, 49)), .Names = c("Countries", "count"), row.names = c(NA, 
13L), class = "data.frame")

我尝试了下面的代码。

countries = geocode(Countryprofile$Countries)
Countryprofile = cbind(Countryprofile,countries)
mapWorld <- borders("world", colour="grey", fill="lightblue")
q<-ggplot(data = Countryprofile) + mapWorld + geom_point(aes(x=lon, y=lat) ,color="red", size=3)+
  geom_text(data = Countryprofile,aes(x=lon,y=lat,label=Countries))

ggplotly(q)

【问题讨论】:

  • 您能否包含您尝试过的实际代码以及它不适合您的地方?
  • 我已经添加了我工作的代码。我觉得地图看起来很简单,所以最初没有发布。
  • 你检查过这个post吗?
  • 是的,但是我在悬停时找不到相应位置上方的值和名称
  • 这个post怎么样?我还将建议您重新编辑您的问题,并让我们知道您在其他地方提出的类似问题方面还阅读了哪些其他 SO 帖子。这将为您的问题添加内容。

标签: r ggplot2 plotly ggmap


【解决方案1】:

您可以从ggplotly 更改结果中的任何属性。在这种情况下,您可以设置第二个跟踪(定义标记的位置)的text 属性。

plotly_map <- ggplotly(q)
plotly_map$x$data[[2]]$text <- paste(Countryprofile$Countries,
                                     Countryprofile$count, 
                                     sep='<br />')
plotly_map


library(plotly)
library(ggmap)
Countryprofile  <- structure(list(Countries = c("USA", "India", "Europe", "LATAM", 
                             "Singapore", "Phillipines", "Australia", "EMEA", "Malaysia", 
                             "Hongkong", "Philippines", "Thailand", "New Zealand"
), count = c(143002, 80316, 33513, 3736, 2180, 1905, 1816, 921, 
             707, 631, 207, 72, 49)), .Names = c("Countries", "count"), row.names = c(NA, 
                                                                                      13L), class = "data.frame")
countries = geocode(Countryprofile$Countries)
Countryprofile = cbind(Countryprofile,countries)
mapWorld <- borders("world", colour="grey", fill="lightblue")
q<-ggplot(data = Countryprofile) + mapWorld + geom_point(aes(x=lon, y=lat) ,color="red", size=3)+
  geom_text(data = Countryprofile,aes(x=lon,y=lat,label=Countries))

plotly_map <- ggplotly(q)
plotly_map$x$data[[2]]$text <- paste(Countryprofile$Countries, Countryprofile$count, sep='<br />')
plotly_map

【讨论】:

  • 非常感谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多