Search code examples
rggplot2axis

Change the default line/label intervals - axis.ticks


How do I change the default line/label intervals in the figure below?

I'm trying to make a map figure. My code, and the figure it makes, are below.

I'd like to change the figure so that the lines and labels for lat and long occur at each degree, rather than at each 0.5 degree for lat and every other degree for long, as it is now. How do I do that?

My best guess so far is to use theme(axis.ticks), theme(panel.grid), or derivatives of those, but no luck so far.

world <- ne_countries(scale = "large", returnclass = "sf")
north_america <- c("Canada", "United States of America")
na_map <- subset(world, name %in% north_america)

ggplot(data = na_map) +
  geom_sf(fill = "lightgrey") +
  xlim(-130, -122) +
  ylim(45.7, 49.3)+
  theme_dark() 

enter image description here


Solution

  • You could set the number and interval for the breaks via the breaks= argument of scale_x/y_continuous. In that case we also have to set the limits via the limits= argument instead of using the convenience functions x/ylim():

    library(ggplot2)
    library(rnaturalearth)
    
    world <- ne_countries(scale = "large", returnclass = "sf")
    north_america <- c("Canada", "United States of America")
    na_map <- subset(world, name %in% north_america)
    
    ggplot(data = na_map) +
      geom_sf(fill = "lightgrey") +
      scale_x_continuous(
        breaks = seq(-130, -122),
        limits = c(-130, -122)
      ) +
      scale_y_continuous(
        breaks = seq(45, 49),
        limits = c(45.7, 49.3)
      ) +
      theme_dark()