gpt4 book ai didi

r - 无法将 ggsn 比例尺添加到 ggplot

转载 作者:行者123 更新时间:2023-12-01 21:16:21 25 4
gpt4 key购买 nike

我正在尝试使用 ggsn::scalebar() 将比例尺添加到 ggplot map ,但是尽管尝试了许多不同的参数组合,我总是会遇到一些错误或其他错误。这是我的 map 的代码:

# load libraries
library(tidyverse)
library(viridis)
library(sp)
library(rgdal)
library(ggsn)

# data
my.data.df <- data.frame(value = c(-1.2269832, -1.0153486, -0.9581069, -1.1534667, -1.0139735, -0.8711997, -0.8618286, -0.8524683),
x = c(538127.3, 538129.3, 538125.3, 538127.3, 538129.3,538131.3, 538121.3, 538123.3),
y = c(1101055, 1101055, 1101053, 1101053, 1101053, 1101053, 1101051, 1101051))
bb <- data.frame(long = c(538107, 538142), lat = c(1101020.5, 1101058.5))


# make a map
my.map <- ggplot(my.data.df, aes(x=x, y=y, fill=value)) +
geom_tile() +
geom_path(data = lines, aes(x=long, y=lat, group=group), inherit.aes = FALSE) +
scale_fill_viridis("Subsidence (m)",
limits = c(-1.5, 0.5)) +
scale_x_continuous(limits = c(538107, 538142)) +
scale_y_continuous(limits = c(1101020.5, 1101058.5)) +
coord_fixed() +
theme(plot.title = element_text(hjust = 0.5),
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
legend.justification=c(1,0),
legend.position=c(1,0),
plot.margin=unit(c(0,0,0,0), "cm"))

Take a look at the map here.当我尝试向 map 添加比例尺时,问题就出现了:

my.map + 
scalebar(dist = 5, dd2km = FALSE, dist_unit = 'm', location = 'bottomleft')

这会返回错误:

Error in scalebar(dist = 5, dd2km = FALSE, dist_unit = "m", location = "bottomleft") : argument "x.min" is missing, with no default

添加 x.min、x.max、y.min、y.max:

my.map + 
scalebar(dist = 5, dd2km = FALSE, dist_unit = 'm', location = 'bottomleft', x.min = 538107, x.max = 538142, y.min = 1101020.5, y.max = 1101058.5)

返回此错误:

Error in FUN(X[[i]], ...) : object 'value' not found

尝试添加数据参数:

my.map + 
scalebar(data = my.data.df, dist = 5, dd2km = FALSE, dist_unit = 'm', location = 'bottomleft', x.min = 538107, x.max = 538142, y.min = 1101020.5, y.max = 1101058.5)

返回错误:

Error: data must be uniquely named but has duplicate columns In addition: Warning messages: 1: In min(data$long) : no non-missing arguments to min; returning Inf 2: In max(data$long) : no non-missing arguments to max; returning -Inf 3: In min(data$lat) : no non-missing arguments to min; returning Inf 4: In max(data$lat) : no non-missing arguments to max; returning -Inf

最后,我发现帮助说scalebar()中的数据参数实际上是在寻找一个边界框作为数据框,所以我尝试使用它:

my.map + 
scalebar(data = bb, dist = 5, dd2km = FALSE, dist_unit = 'm', location = 'bottomleft', x.min = 538107, x.max = 538142, y.min = 1101020.5, y.max = 1101058.5)

但出现此错误:

Error in FUN(X[[i]], ...) : object 'value' not found

我想知道添加比例尺是否会导致 ggplot 出现问题,因为它不再将“值”识别为数据框中的列以用于填充两次尝试。不过,我真的不确定。任何见解或解决方案将不胜感激!

最佳答案

我也遇到了同样的问题。通过将填充映射从 ggplot 调用移动到 geom_tile 调用,它解决了问题。

关于r - 无法将 ggsn 比例尺添加到 ggplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49910266/

25 4 0
文章推荐: java - HashMap 中的 ArrayList 之谜