Home > Software engineering >  R: Use real lon/lat values at x and y axis
R: Use real lon/lat values at x and y axis

Time:05-17

I've generated the following chart with R:

library(rnaturalearth)
library(rnaturalearthdata)
library(ggplot2)
library(rworldmap)
library(scales)
library(sf)
library(mapdata)
library(maptools)
library(ggthemes)

data(wrld_simpl)

antarctica <- wrld_simpl[wrld_simpl$NAME == "Antarctica", ]
pr <- " proj=laea  lat_0=-90  ellps=WGS84  datum=WGS84  no_defs  towgs84=0,0,0"
antarctica.laea <- spTransform(antarctica, CRS(pr))

antarctica_plot <- ggplot()  
  geom_polygon(data = antarctica.laea, aes(x=long, y=lat, group=group), fill = '#003f5c')  
  labs( x = "Longitude", y = "Latitude")  
  annotation_scale(location = "bl", width_hint = 0.5)  
  annotation_north_arrow(location = "bl", which_north = "true", 
                         pad_x = unit(0.75, "in"), pad_y = unit(0.5, "in"),
                         style = north_arrow_fancy_orienteering)  
  theme_wsj()

antarctica_plot

Antarctica map

How can I display the real latitude and longitude values on my axis?

CodePudding user response:

You can solve this transforming the spatial polygon (sp) to simple feature class (sf) and using the geom_sf instead geom_polygon. I couldnt find the functions annotation_scale and annotation_north_arrow in any of those packages btw...

antarctica.laea <- st_as_sf(antarctica.laea)
        
    ggplot(antarctica.laea)  
      geom_sf(aes(), fill = '#003f5c') 
      labs( x = "Longitude", y = "Latitude")  
      theme_wsj()

enter image description here

  • Related