How to add lines of longitude and latitude on a map using ggplot2?

风格不统一 提交于 2019-12-07 08:17:15

问题


I am now plotting the map of Canada using ggplot2. Because the default projection method is "aea"(Albers Equal Area), so the longitude and latitude are straight lines on the map. I wonder how I can display the longitude and latitude in the form of "110W, 100W, 90W" and "50N, 60N, 70N" on the map. They should be curves. Thanks a lot.

The arcgis shapfile is downloaded from https://www.arcgis.com/home/item.html?id=dcbcdf86939548af81efbd2d732336db

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")

The final result should be like this.


回答1:


You can do this with the coord_map argument of ggplot documented here

This uses projections to alter the coordinate grid. Curved lines would include equal-distance projections, but you should look here for a list of all projections allowed. Which one you choose is a manner of preference.

Using azequidistant (I think this is the Azimuthal equidistant projection), and adding labels manually:

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())




回答2:


You can use a separate graticule layer of spatial data, which you then project based on your Canada layer.

You can find free graticule layers for download at 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")


来源:https://stackoverflow.com/questions/38532070/how-to-add-lines-of-longitude-and-latitude-on-a-map-using-ggplot2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!