ggplot 指定经度/纬度轴中断

hel*_*n.h 4 r

我使用以下代码从 shapefile 创建一个“Trial_area”的地图,并覆盖第二个 shapefile 中的海岸线和“prod_areas”。然后,我使用 coord_sf 将地图缩放到试验区域多边形的 st_bbox。但是,对于某些区域(参见下面的示例),轴刻度文本标签最终会重叠,有没有办法可以指定轴刻度线间隔以避免这种情况(例如,纬度为 0.1,经度为 0.5)?

poly <- trial_areas %>% 
filter(Id==5) 
ext <- st_bbox(poly)

plot_SoundOfSleat <- ggplot() +
theme(panel.background = element_rect(fill = 'light blue'),element_line()) +
geom_sf(data=poly)+
geom_sf(data=prod_areas,fill=mycol) +
geom_sf(data = Scot, aes(),
        fill = "lightgreen",col="darkgreen") +
coord_sf(xlim = c(ext[1], ext[3]), ylim = c(ext[2], ext[4]))  +
ggtitle("Sound of Sleat Trial Area 5") +
geom_sf_text(aes(label = Producti_1), data=prod_areas,size=3,hjust=0, vjust=0) +
labs(x = "Longitude", y= "Latitude")

plot_SoundOfSleat
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

mrh*_*ann 6

geom_sf()应该与 无缝协作ggplot2::scale_*_continuous(),您可以在其中使用breaks =参数。请小心西经度,因为它们在数据中为负数,但在标签中为正数。

我在下面列出了一些示例:

library(sf)

# sample data
nc <-  st_read(system.file("shape/nc.shp", package="sf"))

# No edits to graticules
nc_1 <- ggplot(nc) + 
          geom_sf() +
          ggtitle('original')

nc_2 <- ggplot(nc) +
          geom_sf() + 
          scale_y_continuous(breaks = c(34, 35, 36)) + 
          scale_x_continuous(breaks = seq(-84, -76, by = 1)) +
          ggtitle('fewer lat, more lon')

nc_3 <- ggplot(nc) +
          geom_sf(data = st_graticule(nc, 
                                      lat = seq(34, 36, by = 1),
                                      lon = seq(-84, -76, by = 4)), 
                  color = 'orange') +
          geom_sf() +
          coord_sf(datum = NA) +
          ggtitle('using st_graticule')

# using cowplot to output single image, rather than 3
cowplot::plot_grid(nc_1, nc_2, nc_3, ncol = 1)

Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如果需要,您应该能够使用 st_bbox 的输出来自动生成合理数量的标线(网格线)。