如何在 R 的 ggplot 中添加额外的主要网格线

问题描述

我有以下数据集:

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...

如果我把它和 dput 放在一起,那么把它放在这里太长了。我的问题是,我想将这些数据绘制在每度都有主要网格线的网格上。我的代码如下所示:

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

情节是这样的:

plot

但我想要的是网格线之间的间距为 1 度,而不是现在的 10 度。我已经尝试过使用 breaks 但没有用。

有人可以帮我吗?

解决方法

breaksscale_*_continuous 中调整主要网格线:

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

您是否为 breaks 指定了负值?