[英]How to add long/lat points in world map with R?
我正在嘗試將點添加到世界地圖並為其着色。 我有每個點的經度和緯度信息,但是以某種方式將結果點放置在地圖中的錯誤位置。 這與我使用的地圖投影有關。 這是我要添加到世界地圖中的一些要點(“位置”):
longitude latitude subspecies
-54.8706 -67.4208 red
-53.8057 -67.6862 red
-53.9433 -67.4866 red
-54.4833 -69.6666 red
-54.3833 -66.5666 red
-54.7333 -65.2 red
-53.2333 -68.2 red
-54.2666 -66.7333 red
-52.7166 -68.5333 red
-54.4 -66.55 red
-52.08 -68.6012 red
-54.2166 -66.8666 red
-53.4 -68.0666 red
-53.3 -68.2 red
-35.2141 -75.516 blue
-35.5656 -75.4556 blue
-35.8177 -75.5352 blue
-36.2046 -75.7549 blue
-36.467 -75.8373 blue
-36.7096 -75.9115 blue
...
這是我的代碼:
locations <- read.csv(file = "stranded_location.csv", header = T)
map <- ggplot() + coord_fixed() + xlab("") + ylab("")
base_world_messy <- map +
geom_polygon(data=world_map, aes(x=long, y=lat, group=group),
colour="grey21", fill="grey21")
base_world_messy
map_data_locations <- base_world_messy +
geom_point(data=locations,
aes(x=longitude, y=latitude), colour=locations$subspecies,
fill=locations$subspecies, pch=21, alpha=I(0.7))
map_data_piloto
希望盡快解決。 謝謝你的時間!
我認為我發現了問題:1.您的緯度和經度似乎相反,並且2.對於藍色的亞種,您的y值似乎具有應有的相反符號。 我做了一些dplyr
以更改這些變量的名稱以交換坐標,並更改了藍色y值的符號。 這是您期望的樣子嗎?
library(tidyverse)
locations <- "longitude latitude subspecies
-54.8706 -67.4208 red
-53.8057 -67.6862 red
-53.9433 -67.4866 red
-54.4833 -69.6666 red
-54.3833 -66.5666 red
-54.7333 -65.2 red
-53.2333 -68.2 red
-54.2666 -66.7333 red
-52.7166 -68.5333 red
-54.4 -66.55 red
-52.08 -68.6012 red
-54.2166 -66.8666 red
-53.4 -68.0666 red
-53.3 -68.2 red
-35.2141 -75.516 blue
-35.5656 -75.4556 blue
-35.8177 -75.5352 blue
-36.2046 -75.7549 blue
-36.467 -75.8373 blue
-36.7096 -75.9115 blue " %>%
read_table2() %>%
# there are stray whitespaces coming in when I copy & paste from SO, so need to delete extra column
select(-X4)
world_map <- map_data("world")
locations %>%
# need to swap longitude & latitude
# changing the names of longitude & latitude to x & y just for clarity
rename(x = latitude, y = longitude) %>%
# somehow blue points have y values flipped
mutate(y = ifelse(subspecies == "blue", y * -1, y)) %>%
ggplot() +
geom_polygon(aes(x = long, y = lat, group = group), data = world_map, fill = "grey21", color = "grey21") +
geom_point(aes(x = x, y = y, color = subspecies)) +
scale_color_identity() +
coord_fixed() +
xlab("") +
ylab("")
由reprex軟件包 (v0.2.0)於2018-04-17創建。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.