geom_sf:使用 expand=FALSE 重新标记经纬网
geom_sf: Relabeling Graticules with expand=FALSE
我创建了一个 25 x 20 的空间矩形,我只想在绘制它时标记四肢 (0, X) 和 (0, Y)。
它在 coord_sf(expand=T) 时工作正常,但如果 expand=F.
我收到一条错误消息
矩形定义为
library(sf)
x <- c(0, 25, 25, 0, 0)
y <- c(0, 0, 20, 20, 0)
poly.sf <- st_sf(geometry = st_sfc(st_polygon(list(matrix(c(x1,y1), ncol=2)))))
下面的情节很好
library(ggplot)
ggplot() +
geom_sf(data=poly.sf) +
scale_y_continuous(breaks=c(0,20), labels=c("0", "Y")) +
scale_x_continuous(breaks=c(0,25), labels=c("0", "X"))
但是因为我不想在四肢前后添加space,所以我添加了
+ coord_sf(expand=FALSE)
我收到以下错误:
“错误:沿 x 方向的中断和标签长度不同”
这对我来说毫无意义。
我怎样才能得到一个坐标轴标记为 (0, X) 和 (0, Y) 而前后没有 space 的图
四肢?
我已经尝试为标签创建一个自定义函数,试试这个:
library(sf)
x <- c(0, 25, 25, 0, 0)
y <- c(0, 0, 20, 20, 0)
poly.sf <- st_sf(geometry = st_sfc(st_polygon(list(matrix(c(x, y), ncol = 2)))))
library(ggplot2)
# Custom labelling functions
labsy <- function(y) {
y[y != 0] <- "Y"
paste0(y, "")
}
labsx <- function(x) {
x[x != 0] <- "X"
paste0(x, "")
}
ggplot() +
geom_sf(data = poly.sf) +
scale_y_continuous(breaks = c(0, 20), labels = labsy) +
scale_x_continuous(breaks = c(0, 25), labels = labsx) +
coord_sf(expand = FALSE)
我创建了一个 25 x 20 的空间矩形,我只想在绘制它时标记四肢 (0, X) 和 (0, Y)。
它在 coord_sf(expand=T) 时工作正常,但如果 expand=F.
矩形定义为
library(sf)
x <- c(0, 25, 25, 0, 0)
y <- c(0, 0, 20, 20, 0)
poly.sf <- st_sf(geometry = st_sfc(st_polygon(list(matrix(c(x1,y1), ncol=2)))))
下面的情节很好
library(ggplot)
ggplot() +
geom_sf(data=poly.sf) +
scale_y_continuous(breaks=c(0,20), labels=c("0", "Y")) +
scale_x_continuous(breaks=c(0,25), labels=c("0", "X"))
但是因为我不想在四肢前后添加space,所以我添加了
+ coord_sf(expand=FALSE)
我收到以下错误:
“错误:沿 x 方向的中断和标签长度不同”
这对我来说毫无意义。
我怎样才能得到一个坐标轴标记为 (0, X) 和 (0, Y) 而前后没有 space 的图 四肢?
我已经尝试为标签创建一个自定义函数,试试这个:
library(sf)
x <- c(0, 25, 25, 0, 0)
y <- c(0, 0, 20, 20, 0)
poly.sf <- st_sf(geometry = st_sfc(st_polygon(list(matrix(c(x, y), ncol = 2)))))
library(ggplot2)
# Custom labelling functions
labsy <- function(y) {
y[y != 0] <- "Y"
paste0(y, "")
}
labsx <- function(x) {
x[x != 0] <- "X"
paste0(x, "")
}
ggplot() +
geom_sf(data = poly.sf) +
scale_y_continuous(breaks = c(0, 20), labels = labsy) +
scale_x_continuous(breaks = c(0, 25), labels = labsx) +
coord_sf(expand = FALSE)