r - 在 R 中使用 ggplot 分面进行地理空间映射?

标签 r ggplot2 geospatial spatial facet-wrap

我有一个 Watershed有 33 subbasins .我无法想出一个代码来绘制可重现的 shapefile对于集水区,我附上了我的 Shapefile .我有 four models生成 Evapotranspiration (ET)历年数据2005-2008 .我想用ggplot比较每年的四种型号产品faceting功能。我尝试了一些事情(请参阅我的示例代码)但没有成功。我很感激前进的道路。

library(sf)
library(tidyverse)

shape <- read_sf(dsn = ".", layer = "Watershed")


ETM1 = data.frame(Subbasin = 1:33, Yr2005 = runif(33, 500,700), Yr2006 = runif(33, 600,750), Yr2007 = runif(33, 450,750),
                  Yr2008 = runif(33, 550,800), Model = rep("M1", 33))

ETM2 = data.frame(Subbasin = 1:33, Yr2005 = runif(33, 600,750), Yr2006 = runif(33, 550,750), Yr2007 = runif(33, 600,750),
                  Yr2008 = runif(33, 700,800), Model = rep("M2", 33))

ETM3 = data.frame(Subbasin = 1:33, Yr2005 = runif(33, 500,750), Yr2006 = runif(33, 650,750), Yr2007 = runif(33, 700,750),
                  Yr2008 = runif(33, 500,800), Model = rep("M3", 33))

ETM4 = data.frame(Subbasin = 1:33, Yr2005 = runif(33, 400,750), Yr2006 = runif(33, 450,750), Yr2007 = runif(33, 300,750),
                  Yr2008 = runif(33, 400,800), Model = rep("M4", 33))

ETData = rbind(ETM1,ETM2,ETM3,ETM4)
Combine = gather(ETData, key = "Variable", value = "Value", -c("Model","Subbasin"))

SpData = merge(shape, Combine, by='Subbasin')

ggplot() + 
  geom_polygon(SpData, aes(x = Lat, y = Long_, fill = Value))+
  facet_wrap(~Model, nrow = 4, ncol = 4)

这是我使用 plot(shape$geometry) 绘制的 shapefile 图片
enter image description here

这是一个近似值 figure我想构建的,虽然它只有 3 行。
enter image description here

我做了一个反射(reflect)我最终目标的人物(手绘)——每个椭圆形(有点)代表我的 Catchment shapefile除法为 subbasins .我为我画的不好而道歉。
enter image description here

最佳答案

你已经非常接近解决方案了。您的最终数据包含获得您的情节的所有内容。

基本上,您可以使用 facet_grid分隔“Year”和“Model”变量和geom_sf这将使用数据集的“几何”列来绘制形状(更多信息在这里:https://ggplot2.tidyverse.org/reference/ggsf.html)。将值作为填充传递到 aes剩下的。

在这里,我使用了 scale_fill_gradient设置颜色和几个函数以使绘图与您想要的绘图一样接近:

library(sf)
library(tidyverse)

ggplot() +
  geom_sf(data = SpData, aes(fill = Value))+
  facet_grid(Variable~Model)+
  scale_fill_gradient(name =  "Evapotranspiration (ET)", low = "green", high = "red",
                      limits = c(300,900), 
                      breaks = c(300, 500, 700, 900))+
  theme_void()+
  theme(legend.position = "bottom")

enter image description here

关于r - 在 R 中使用 ggplot 分面进行地理空间映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60050476/

相关文章:

r - 如何去除R包 “levelplot”中 “rasterVis”中的框框

r - 检查 data.frame 列中的任何值是否为空

r - 如何在 ggplot2 中制作多重条形图?

r - 如何查看存储图的代码 (ggplot)

R 函数用于将单元格中的逗号分隔值转换为具有相同行名的多行?

r - 按标准在数据框中引入数据

r - 将图例/文本表添加到具有多个图层的 ggmap

javascript - 谷歌地球问题

mysql - 我的多边形是否正确插入到数据库中?

r - 在 R 中使用 ggmap 添加上下文(或任意) map 插图?