Roelalex1996
Roelalex1996

Reputation: 73

How to add extra major grid lines in ggplot in R

I have the following dataset:

Simple feature collection with 6 features and 2 fields
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: -2486022 ymin: -1025641 xmax: 954903.6 ymax: 2252011
CRS:            EPSG:32119
          VAR     residual                       geometry
1 total_resid  0.052358204 MULTIPOLYGON (((-260020.6 -...
2 total_resid  0.051623652 MULTIPOLYGON (((904798.7 65...
3 total_resid -1.780438621 MULTIPOLYGON (((772940.8 57...
4 total_resid -0.012465629 MULTIPOLYGON (((429779.8 -9...
5 total_resid -0.009121893 MULTIPOLYGON (((7030.591 15...
6 total_resid  0.097626207 MULTIPOLYGON (((-2401716 17...

If I put it here with dput it is too long to put it here. My question, I want to plot this data on a grid with major grid lines every degree. My code looks like this:

facet_sources_State_Residuals = ggplot() + 
  geom_sf(data = sources_State_Residuals, aes(fill=residual)) + 
  facet_wrap(~VAR, ncol=3, nrow=1) +
  theme(panel.grid.major = element_line(color = "white")) +
  scale_fill_gradient2(low='blue', mid='white', high='red', midpoint=0, limits=c(-0.25,0.25), oob=squish) + 
  geom_sf(data = usbigcities, color = 'black') + labs(fill="USGS-PCRGLOB [m3/m2/year]")

And the plot looks like this: plot

But what I want is to have a spacing of 1 degree between the gridlines instead of the 10 degrees it is now. I already tried to use breaks but that did not work.

Can anyone help me out here?

Upvotes: 3

Views: 1169

Answers (1)

rcs
rcs

Reputation: 68839

breaks works in scale_*_continuous to adjust the major gridlines:

library("ggplot2")
library("sf")
library("raster")

us <- raster::getData("GADM", country = "USA", level = 1) %>%
  st_as_sf %>%
  st_transform(32119) 
ggplot() +
  geom_sf(data = us) +
  coord_sf(expand = FALSE) +
  scale_x_continuous(breaks = seq(-130, -70, 5)) +
  facet_wrap(~GID_0)

map

Did you specify negative values for breaks?

Upvotes: 2

Related Questions