【问题标题】:Overlap map created in ggplot and shapefile在 ggplot 和 shapefile 中创建的重叠图
【发布时间】:2021-11-18 03:24:44
【问题描述】:

我试图在使用 ggplot 创建的地图上重叠 shp 层,当我绘制两个独立的图时,你建议我如何重叠两个层?

data.shape<-readOGR(dsn="departamentos",layer="DEPARTAMENTOS")


ggplot()+
  geom_tile(data = tx_trend, aes(x = longitude, y = latitude, fill = slope))+
  scale_fill_gradientn(colors = rev(pals::linearlhot(100)), name = "ºC/10y", limits = c(0.1,0.5)) +
  #scale_fill_gradientn(colors = (pals::isol(100)), name = "ºC/10y", limits = c(0.1,0.45)) +
#  geom_point(data = filter(tx_trend, sign < 0.01),aes(x = longitude, y = latitude, color = "Sign. trend \n p-value <0.01"),
  geom_point(data = filter(tx_trend, sign < 0.01),aes(x = longitude, y = latitude, color = "Sign. trend \n p-value <0.01"),
                        size = 0.7, show.legend = T) +
  scale_color_manual(values = c("black"), name = "")+
  coord_fixed(1.3)+
  xlab("Longitude") + ylab("Latitude")+
  labs(title = "Decadal trend Summer", 
       subtitle = "(1981-2016)",
       caption = "")+
  theme_bw() +
  guides(fill = guide_colourbar(barwidth = 9, barheight = 0.5, title.position="right"))+
  theme(legend.position = "bottom")

【问题讨论】:

  • 据我所知,这里只有两个美学:geom_tilegeom_point 都显示在第一个情节中。
  • 他们在两个不同的地块。

标签: r ggplot2 overlap shapefile rgdal


【解决方案1】:

如果没有可重复的示例很难,但我试一试!所以这是我的建议:

  1. 使用install.packages("sf") 安装“sf”包并使用library(sf) 加载库

  2. 使用st_read() 以sf 格式导入您的图层:

data.shape

  1. 使用专用的geom_sf() 在您的图块和点图层上绘制您的形状。您只需要在 ggplot() 代码块中添加以下代码行:

geom_sf(data = st_geometry(data.shape), fill = NA, color = "red") +

coord_sf(default_crs = sf::st_crs(4326)) +

所以我建议:

ggplot()+
  geom_tile(data = tx_trend, aes(x = longitude, y = latitude, fill = slope))+
  scale_fill_gradientn(colors = rev(pals::linearlhot(100)), name = "ºC/10y", limits = c(0.1,0.5)) +
  #scale_fill_gradientn(colors = (pals::isol(100)), name = "ºC/10y", limits = c(0.1,0.45)) +
#  geom_point(data = filter(tx_trend, sign < 0.01),aes(x = longitude, y = latitude, color = "Sign. trend \n p-value <0.01"),
  geom_point(data = filter(tx_trend, sign < 0.01),aes(x = longitude, y = latitude, color = "Sign. trend \n p-value <0.01"),
                        size = 0.7, show.legend = T) +
  geom_sf(data = st_geometry(data.shape), fill = NA, color = "red") + # ADDED HERE
  coord_sf(default_crs = sf::st_crs(4326)) +       # ADDED HERE
  scale_color_manual(values = c("black"), name = "")+
  coord_fixed(1.3)+
  xlab("Longitude") + ylab("Latitude")+
  labs(title = "Decadal trend Summer", 
       subtitle = "(1981-2016)",
       caption = "")+
  theme_bw() +
  guides(fill = guide_colourbar(barwidth = 9, barheight = 0.5, title.position="right"))+
  theme(legend.position = "bottom")

【讨论】:

  • 感谢您的建议,但仍有问题得到此错误: vapply 中的错误(lst,class,rep(NA_character_,3)):值必须是长度 3,但 FUN(X[[1 ]]) 结果是长度 2 。你能建议另一种方法来绘制我的数据吗?
  • 正如我所担心的,没有任何代表,很难帮助你。运行哪行代码后出现这个错误?
  • 对不起,我错了。我通过用 st_geometry(data.shape) 替换 st_sfc(data.shape) 来编辑我的答案。它应该工作!告诉我。
  • 好的。只需指示与其他两层相同的 CRS 我不知道您使用哪一个:我通过指示 WGS84(即 EPSG 代码:4326)来编辑帖子,因为我在您的代码中看到了经度和纬度。但如果我错了,请指出正确的EPSG代码。希望它会奏效!
  • 很难帮你解决这个问题。三层必须具有相同的坐标系,但似乎并非如此。如果不知道“tile”和“point”两层的CRS,可以尝试将coord_sf(default_crs = sf::st_crs(4326)) +替换为coord_sf() +。这应该强制从定义 CRS 的第一层获取 CRS。但请注意,图层 data.shape 的几何形状可能是错误的,因为它可能被投影到不属于它自己的系统中。
猜你喜欢
  • 2022-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多