在发布这个问题之前,我非常小心,以避免任何重复。所以我打算创建一个基于季度的分面图,配备主要和次要网格,而纬度和经度标签仅在主要网格上。这是MWE:
library(sf)
library(rnaturalearth)
sf::sf_use_s2(FALSE)
dummy <- data.frame(
Lat1 = seq(-14.5, 4.5, by = 1),
Lon1 = seq(105.5, 124.5, by = 1),
quarter = rep(1:4, 10)
)
worldmap <- ne_countries(scale = 'medium', type = 'map_units', returnclass = 'sf')
wpp <- st_crop(worldmap, ymin = -15, ymax = -5, xmin = 105, xmax = 125)
minor.x <- seq(105, 125, by = 1)
minor.y <- seq(-15, -5, by = 1)
ggplot() +
geom_sf(data = wpp, color = "gray", fill = "gray") +
coord_sf(xlim=c(min(data.pie$Lon1-0.5),max(data.pie$Lon1)+0.5),
ylim=c(min(data.pie$Lat1)-0.5,max(data.pie$Lat1)+0.5),
expand = FALSE) +
geom_hline(aes(yintercept = minor.y), color = "gray", linetype = "solid", size = 0.1) +
geom_vline(aes(xintercept = minor.x), color = "gray", linetype = "solid", size = 0.1) +
geom_point(data = dummy, aes(x = Lon1, y = Lat1), color= "black") +
xlab("Longitude") + ylab("Latitude") +
scale_x_continuous(breaks = seq(105,125,2), minor_breaks = seq(106, 124, 2)) +
scale_y_continuous(breaks = seq(-15,-5,2), minor_breaks = seq(-14, -4, 2)) +
facet_wrap(~quarter) +
theme_minimal() +
theme(
# panel.grid.minor = element_line(color = "blue", size = 0.1),
panel.grid.major = element_blank(),
panel.background = element_rect(colour = "black", size=0.5),
legend.position = "right",
legend.title.align = 0.5,
legend.text.align = 1)
但是,当我使用时facet_wrap(~quarter)
出现错误:Error in FUN(X[[i]], ...) : subscript out of bounds
如果我关闭了,我设法生成了一个分面图geom_vline
,geom_hline
但同时,我将失去对次要网格的控制(只显示了主要网格。有什么解决方法吗?PS。我使用的是最新的 R 和ggplot()
版本.