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

和自甴很熟 提交于 2019-12-05 13:44:15

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

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