在国家/地区显示价值的 ggmap

ggmap with value showing on the countries

我正在为一列中给定的国家/地区样本数据寻求帮助,并在另一列中计数。我正在尝试使用 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)

您可以更改 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