在 R 中使用 ggplot 分面进行地理空间映射?
Geospatial mapping using ggplot faceting in R?
我有一个 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 的图片
这是我想构造的近似值 figure
,尽管它只有 3 行。
我制作了一个图形(手绘)来反映我的最终目标 - 每个椭圆形(某种程度上)代表我的 Catchment shapefile
,除法为 subbasins
。我为我糟糕的绘画道歉。
您非常接近解决方案。您的最终数据包含获取绘图的所有内容。
基本上,您可以使用 facet_grid
来分隔 "Year" 和 "Model" 变量,geom_sf
将使用数据集的列 "geometry"绘制形状(更多信息在这里: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")
我有一个 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 的图片
这是我想构造的近似值 figure
,尽管它只有 3 行。
我制作了一个图形(手绘)来反映我的最终目标 - 每个椭圆形(某种程度上)代表我的 Catchment shapefile
,除法为 subbasins
。我为我糟糕的绘画道歉。
您非常接近解决方案。您的最终数据包含获取绘图的所有内容。
基本上,您可以使用 facet_grid
来分隔 "Year" 和 "Model" 变量,geom_sf
将使用数据集的列 "geometry"绘制形状(更多信息在这里: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")