2017-08-19 1 views
0

1つの列にある国のサンプルデータを参考にして別の列に数えます。私は地理上のマップを作成しようとしています.GGplotを使用して、国の上にカーソルを置くと、地図のそれぞれの場所に国の数と名前が表示されます。以下は与えられたサンプルデータです。私は国を特定するために緯度と経度の高いggmapを試しましたが、ホバリング時に国の数と名前を表示することはできませんでした。国で値が表示されている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) 
+0

あなたが試してみた&それはあなたのために働いていないところ実際のコードを含めることはできますか? –

+0

私が働いたコードを追加しました。私は地図がとても平凡に見えたので、最初は投稿しなかったのです。 – ssan

+0

この[投稿](https://stackoverflow.com/questions/33578168/how-to-plot-regions-in-a-country-each-shaded-by-some-corresponding-value)を確認しましたか? – Ashish

答えて

0

結果の属性をggplotlyから変更できます。この場合、2番目のトレース(マーカが定義されている場所)のtext属性を設定することができます。

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 
+0

ありがとうございます! – ssan

関連する問題