在 ggmap 形状上绘制 geom_sf

发布于 2025-01-17 00:01:46 字数 1083 浏览 2 评论 0原文

我正在尝试在使用 ggmap 包下载的地图上绘制地图 shapefile。但是,我收到一条神秘的错误消息和一个不太有用的回溯:

代码:

map <- get_stamenmap( bbox = c(left = 3, bottom = 48, right = 3.5, top = 49), zoom = 12, maptype = "terrain")

transformed_sample = st_transform(rpg_sf %>% filter(commune %in% smallcomslist), crs=4326)

ggmap(map) + geom_sf(data=transformed_sample, mapping=aes(fill=commune),lwd=0) 

#Here transformed_sample is an object of class "sf" containing some outlines of villages in the area given by bbox.

#This returns:

ggmap(map) + 
   geom_sf(data=transformed_sample, mapping=aes(fill=commune),lwd=0) 

坐标系已存在。添加新的坐标系,它将取代现有的坐标系。

FUN(X[[i]], ...) 中的错误:未找到对象“lon” 回溯()
10: FUN(X[[i]], ...)
9:lapply(美学,eval_tidy,数据=数据,env = env)
8: f(..., self = self)
7: l$compute_aesthetics(d, 绘图)
6: f(l = 层[[i]], d = 数据[[i]])
5: by_layer(函数(l, d) l$compute_aesthetics(d, 绘图))
4: ggplot_build.ggplot(x)
3:ggplot_build(x)
2: print.ggplot(x)
1:(函数(x,...)
UseMethod("打印"))(x)

我不知道为什么需要“lon”。有什么想法吗?

I'm trying to plot a map shapefile on top of a map downloaded using the ggmap package. However, I get a cryptic error message and a not very useful traceback:

Code:

map <- get_stamenmap( bbox = c(left = 3, bottom = 48, right = 3.5, top = 49), zoom = 12, maptype = "terrain")

transformed_sample = st_transform(rpg_sf %>% filter(commune %in% smallcomslist), crs=4326)

ggmap(map) + geom_sf(data=transformed_sample, mapping=aes(fill=commune),lwd=0) 

#Here transformed_sample is an object of class "sf" containing some outlines of villages in the area given by bbox.

#This returns:

ggmap(map) + 
   geom_sf(data=transformed_sample, mapping=aes(fill=commune),lwd=0) 

Coordinate system already present. Adding new coordinate system, which will replace the existing one.

Error in FUN(X[[i]], ...) : object 'lon' not found
traceback()
10: FUN(X[[i]], ...)
9: lapply(aesthetics, eval_tidy, data = data, env = env)
8: f(..., self = self)
7: l$compute_aesthetics(d, plot)
6: f(l = layers[[i]], d = data[[i]])
5: by_layer(function(l, d) l$compute_aesthetics(d, plot))
4: ggplot_build.ggplot(x)
3: ggplot_build(x)
2: print.ggplot(x)
1: (function (x, ...)
UseMethod("print"))(x)

I have no idea why a "lon" is expected. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

翻了热茶 2025-01-24 00:01:46

参加这个聚会已经很晚了,但是在经历了坐标参考系统更改的兔子洞之后,我通过简单地在 geom_sf() 中添加 inherit.aes = FALSE 解决了类似的错误> 层。因此,您的代码将变成

ggmap(map) + 
      geom_sf(data = transformed_sample, mapping = aes(fill=commune), lwd=0, inherit.aes = FALSE) 

仅供参考,如果您经常使用 tidyverse,我建议不要命名对象 map。当您运行 library(tidyverse) 时加载的包 purrr 也有一个函数 map()

Very late to this party, but after going down a rabbit hole of Coordinate Reference System changes, I solved a similar error by simply adding inherit.aes = FALSE in the geom_sf() layer. So your code would become

ggmap(map) + 
      geom_sf(data = transformed_sample, mapping = aes(fill=commune), lwd=0, inherit.aes = FALSE) 

As an FYI, if you use tidyverse quite a bit, I would advise against naming an object map. The package purrr, which gets loaded when you run library(tidyverse) also has a function map().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文