0

I am looking for some help with the given sample data of countries on one column and count on another column. I am trying a build a geo maps using ggplot showing the count and name of the country in the respective places of the map when I hover above the country. Below is the sample data given. I tried with the ggmap with the lat and long position to identify the country but not able to show the count and name of the country on hovering.

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")

I tried the below code.

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)
ssan
  • 301
  • 1
  • 9
  • Can you include the actual code that you've tried & where it's not working for you? – Z.Lin Aug 19 '17 at 09:28
  • I have added the code which I worked. I felt the map looks so plain and that's why did not posted initially. – ssan Aug 19 '17 at 09:44
  • have you checked this [post](https://stackoverflow.com/questions/33578168/how-to-plot-regions-in-a-country-each-shaded-by-some-corresponding-value)? – mnm Aug 19 '17 at 09:45
  • Yes, but I could not find on hovering seeing the value and name above the respective place – ssan Aug 19 '17 at 09:54
  • 1
    how about this [post](https://stackoverflow.com/questions/38917101/how-do-i-show-the-y-value-on-tooltip-while-hover-in-ggplot2)? I will also recommend that you re-edit your question and let us know what other SO posts have you read in terms of similar questions asked elsewhere. This will add content to your question. – mnm Aug 19 '17 at 09:59

1 Answers1

0

You can change any attribute in the result from ggplotly. In this case you can set the text attribute of the 2nd trace (where you markers are defined).

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

enter image description here


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
Maximilian Peters
  • 30,348
  • 12
  • 86
  • 99