如何使用R在世界地图中添加长点/经点? [英] How to add long/lat points in world map with R?

查看:70
本文介绍了如何使用R在世界地图中添加长点/经点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向世界地图添加点并为其着色.我具有每个点的经度和纬度信息,但是以某种方式将结果点放置在地图中的错误位置.这与我使用的地图投影有关.这是我要添加到世界地图中的一些要点(位置"):

I am trying to add points to a world map and color them. I have longitude and latitude information for each point, but somehow the outcome points are positioned in a wrong place in the map. It has to be something with the map projection I am using. This are some of the points ('locations') I am trying to add to the world map:

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

希望尽快解决此问题.感谢您的宝贵时间!

Hope to solve this soon. Thanks for your time!

推荐答案

我认为我发现了问题:1.您的纬度&经度似乎相反,并且2.对于蓝色亚种,您的y值似乎具有应有的相反符号.我做了一些 dplyr 更改这些变量的名称以交换坐标,并更改了蓝色y值的符号.这是您期望的样子吗?

I think I found the problems: 1. your latitude & longitude seemed to be reversed, and 2. for the blue subspecies, your y values seemed to have the opposite sign as they should. I did a little dplyring to change the names of those variables to swap the coordinates, and changed the sign of blue y values. Is this what you expect it to look like?

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.

Created on 2018-04-17 by the reprex package (v0.2.0).

这篇关于如何使用R在世界地图中添加长点/经点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆