[英]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.