簡體   English   中英

使用 rgeolocate 圖在 R 中疊加世界地圖

[英]Overlay worldmap in R with rgeolocate plot

在我之前的問題中,我提出我有一個帶有 IP 的“帖子”表,並且我想對它們進行地理定位。 基於 IP 地址對大量帖子進行地理定位。 (880,000 行)

答案演示了如何使用 rgeolocate 來實現這一點,經過一些努力學習 RI 已經設法實現了相同的結果:

library(iptools)
library(rgeolocate)
library(tidyverse)
library(readxl)
library(rworldmap)
library(ggmap)
library(rworldxtra)
post <- read_excel("filepath/post.xlsx")
view(post)

## grab my ips and, format them

ips <- unlist(post[,3], use.names=FALSE)

#geolocte them
system.time(
rgeolocate::maxmind(
ips, "~/R/GeoLite2-City.mmdb", c("longitude", "latitude")
) -> xdf
)

#user  system elapsed 
#6.04    0.02    6.05 

xdf %>% 
  count(longitude, latitude) -> pts

#And, plot them:
  ggplot(pts) +
  geom_point(
    aes(longitude, latitude, size = n), 
    shape=21, fill = "steelblue", color = "white", stroke=0.25
  ) +
  ggalt::coord_proj("+proj=wintri") +
  ggthemes::theme_map() +
  theme(legend.justification = "center") +
  theme(legend.position = "bottom")

結果如下所示:

ips圖

根據數據,此圖准確顯示了我所期望的分組類型。 所以成功的一步!

當然,下一個合乎邏輯的步驟是提高分辨率並添加世界地圖的疊加層。 我一直無法實現這兩個目標中的任何一個。

使用此代碼,我可以制作高分辨率的世界地圖:

newmap <- getMap(resolution = "high")
plot(newmap)

結果如下所示:

世界地圖沒有點

不知何故,我無法實現地圖和正在繪制的數據的組合。 似乎任何創建地圖的嘗試都需要我繪制地圖本身,而任何向該地圖添加點的嘗試都失敗了。 例如:

newmap <- getMap(resolution = "high")
plot(newmap)
ggmap(newmap) +
  geom_point(data = pts, aes(x = longitude, y = latitude, size=n), 
             shape=21, fill = "steelblue", color = "white", stroke=0.25)

錯誤:ggmap 繪制 ggmap 類的對象,請參閱 ?get_map

我一直在嘗試根據http://www.milanor.net/blog/maps-in-r-plotting-data-points-on-a-map/的建議工作,但該網站專注於歐洲地圖,我想在世界地圖上顯示我的數據。

感謝您的幫助。

library(iptools)
library(rgeolocate)
library(tidyverse)

ips <- ip_random(1000000)

rgeolocate::maxmind(
  ips, "~/Data/GeoLite2-City.mmdb", c("longitude", "latitude")
) -> xdf

xdf %>% 
  mutate(
    longitude = (longitude %/% 5) * 5,
    latitude = (latitude %/% 5) * 5
  ) %>%  
  count(longitude, latitude) -> pts

wrld <- tbl_df(map_data("world"))
wrld <- filter(wrld, region != "Antarctica")

ggplot() +
  geom_map(
    map = wrld, data = wrld, aes(long, lat, map_id=region),
    color = "black", fill ="white", size=0.125
  ) +
  geom_point(
    data = pts, aes(longitude, latitude, size = n), 
    shape=21, fill = "steelblue", color = "white", stroke=0.25
  ) +
  scale_size(name = "# IPs", label=scales::comma) +
  ggalt::coord_proj("+proj=wintri") +
  ggthemes::theme_map() +
  theme(legend.justification = "center") +
  theme(legend.position = "bottom")

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM