代码之家  ›  专栏  ›  技术社区  ›  Yang Yang

如何使用ggplot2在地图上添加经纬线?

  •  4
  • Yang Yang  · 技术社区  · 8 年前

    我现在正在绘制加拿大地图,使用 ggplot2 。因为默认投影方法是“aea”(阿尔伯斯等面积),所以经纬度是地图上的直线。我想知道如何在地图上以“110W,100W,90W”和“50N,60N,70N”的形式显示经度和纬度。它们应该是曲线。谢谢。

    arcgis形状文件下载自 https://www.arcgis.com/home/item.html?id=dcbcdf86939548af81efbd2d732336db enter image description here

    library(ggplot2)
    library(rgdal)
    countries<-readOGR("Canada.shp", layer="Canada")
    ggplot()+geom_polygon(data=countries,aes(x=long,y=lat,group=group),fill='white',color = "black")
    

    最终结果应该是这样的。 enter image description here

    2 回复  |  直到 8 年前
        1
  •  5
  •   Chris    8 年前

    您可以使用 coord_map ggplot参数 documented here

    这将使用投影来改变坐标网格。曲线将包括等距投影,但您应该看起来 here 查看所有允许投影的列表。你选择哪一种是一种偏好。

    使用 azequidistant (我认为这是 Azimuthal equidistant projection ),并手动添加标签:

    axis_labels <- rbind(
                      data.frame(long = rep(-140,5),lat = seq(40,80,10), labels = seq(40,80,10)), # x axis labels
                      data.frame(long = seq(-140,-60,40),lat = rep(85,3), labels = seq(140,60,-40))  # y axis labels
    )
    
       ggplot() +
      geom_polygon(data=countries,aes(x=long,y=lat,group=group),fill='white',color = "black") + 
      coord_map("azequidistant") +
      scale_x_continuous(breaks = seq(-140,60, by = 20))+
      scale_y_continuous(breaks = seq(40,80, by = 10)) +
      geom_text(data = axis_labels, aes(x = long, y = lat, label = labels)) +
      theme_bw() +
      theme(panel.grid.major = element_line(colour = "grey"),
            panel.border = element_blank(),
            axis.text = element_blank())
    

    enter image description here

        2
  •  0
  •   the_skua    8 年前

    您可以使用一个单独的空间数据网格层,然后根据加拿大图层投影该网格层。

    您可以在以下网址找到免费的网格层下载: NaturalEarthData .

    countries<-readOGR("Canada.shp", layer="Canada")
    grat <- readOGR("graticule.shp", layer="graticule")
    
    grat_prj <- spTransform(grat, CRS(countries))
    
    ggplot() + 
    geom_polygon(data=countries, aes(x=long,y=lat,group=group),fill='white',color = "black") + 
    geom_path(data=grat_prj, aes(long, lat, group=group, fill=NULL), linetype="solid", color="grey50")