将数据加入 .kmz 或 .kml 并绘制
join data to a .kmz or .kml and plot
我对 R 比较陌生,在地理空间方面对 R 完全陌生。我正在尝试读取可下载的 .kml
,将我自己的数据加入其中,然后绘制数据。
.kml
来自此主页:https://www.cnrfc.noaa.gov/ -- 地图正下方 'Download Overlay Files' 下拉菜单中的 "Drainage Basins" 图层(文件很小) .
library(rgdal)
library(tidyverse)
# read in the downloaded file
# downloads as "basins.kml", "layer name ID'd from viewing .kml as text
spatialpolydf <- readOGR("basins.kml", "cnrfc_09122018_basins_thin")
#view the polygons (california river basins)
ggplot() + geom_path(data = spatialpolydf, aes(x=long, y=lat, group = group)) +
coord_quickmap()
#create example observations for the 339 river basins
observation_value2 <- sample(1:1000, 339)
#get the basin names #not sure this is reliable
observation_place <- spatialpolydf@data
#create data.frame for joining to the spatial data frame
#but, I'm not sure I quite need a data.frame, maybe just a tibble or data.table?
obs_place_value <- data.frame(observation_place, observation_value2)
所以从这里开始,我希望使用上面的库或任何其他库来加入和可视化观察结果,例如:
spatialpolydf_withjoineddata <- some_join_function(obs_place_value,
spatialpolydf)
ggplot() + geom_path(data = spatialpolydf_withjoineddata, aes(x=long, y=lat,
group = group, fill = observation_value2)) + coord_quickmap()
似乎最好有一个 object/data.frame 的 339 行数据,其中每一行可以代表多个多边形,就像 ESRI 的属性 table 那样,通常不需要几何数据隐。我对所有建议持开放态度,但理想情况下,我将学习将数据保留为 useable/flexible 格式以供以后 processing/visualization 使用的方法,而不是仅仅为了查看这些数据而进行快速修复。
您使用 sf
标签标记了您的问题,因此我认为您可能想了解有关 sf
的更多信息。下面是一个脚本,用于将您的 sp
对象转换为 sf
对象,加入您的数据框 obs_place_value
,然后使用 observation_value2
作为填充值可视化数据。 sf
对象是一种特殊的数据框,因此像 left_join
这样的函数适用于数据框,也可以适用于 sf
对象。 geom_sf
是绘制 sf
对象的函数。
library(rgdal)
library(tidyverse)
library(sp)
library(sf)
# read in the downloaded file
# downloads as "basins.kml", "layer name ID'd from viewing .kml as text
spatialpolydf <- readOGR("Data/basins.kml", "cnrfc_09122018_basins_thin")
# set.seed for reproducibility
set.seed(123)
#create example observations for the 339 river basins
observation_value2 <- sample(1:1000, 339)
#create data.frame for joining to the spatial data frame
obs_place_value <- data.frame(observation_place, observation_value2)
# Convert spatialpolydf to an sf object
spatialpolysf <- spatialpolydf %>% st_as_sf()
# Join the data
spatialpolysf2 <- spatialpolysf %>%
left_join(obs_place_value, by = c("Name", "Description"))
# Use ggplot2 to plot the data
ggplot(spatialpolysf2) +
geom_sf(aes(fill = observation_value2))
我对 R 比较陌生,在地理空间方面对 R 完全陌生。我正在尝试读取可下载的 .kml
,将我自己的数据加入其中,然后绘制数据。
.kml
来自此主页:https://www.cnrfc.noaa.gov/ -- 地图正下方 'Download Overlay Files' 下拉菜单中的 "Drainage Basins" 图层(文件很小) .
library(rgdal)
library(tidyverse)
# read in the downloaded file
# downloads as "basins.kml", "layer name ID'd from viewing .kml as text
spatialpolydf <- readOGR("basins.kml", "cnrfc_09122018_basins_thin")
#view the polygons (california river basins)
ggplot() + geom_path(data = spatialpolydf, aes(x=long, y=lat, group = group)) +
coord_quickmap()
#create example observations for the 339 river basins
observation_value2 <- sample(1:1000, 339)
#get the basin names #not sure this is reliable
observation_place <- spatialpolydf@data
#create data.frame for joining to the spatial data frame
#but, I'm not sure I quite need a data.frame, maybe just a tibble or data.table?
obs_place_value <- data.frame(observation_place, observation_value2)
所以从这里开始,我希望使用上面的库或任何其他库来加入和可视化观察结果,例如:
spatialpolydf_withjoineddata <- some_join_function(obs_place_value,
spatialpolydf)
ggplot() + geom_path(data = spatialpolydf_withjoineddata, aes(x=long, y=lat,
group = group, fill = observation_value2)) + coord_quickmap()
似乎最好有一个 object/data.frame 的 339 行数据,其中每一行可以代表多个多边形,就像 ESRI 的属性 table 那样,通常不需要几何数据隐。我对所有建议持开放态度,但理想情况下,我将学习将数据保留为 useable/flexible 格式以供以后 processing/visualization 使用的方法,而不是仅仅为了查看这些数据而进行快速修复。
您使用 sf
标签标记了您的问题,因此我认为您可能想了解有关 sf
的更多信息。下面是一个脚本,用于将您的 sp
对象转换为 sf
对象,加入您的数据框 obs_place_value
,然后使用 observation_value2
作为填充值可视化数据。 sf
对象是一种特殊的数据框,因此像 left_join
这样的函数适用于数据框,也可以适用于 sf
对象。 geom_sf
是绘制 sf
对象的函数。
library(rgdal)
library(tidyverse)
library(sp)
library(sf)
# read in the downloaded file
# downloads as "basins.kml", "layer name ID'd from viewing .kml as text
spatialpolydf <- readOGR("Data/basins.kml", "cnrfc_09122018_basins_thin")
# set.seed for reproducibility
set.seed(123)
#create example observations for the 339 river basins
observation_value2 <- sample(1:1000, 339)
#create data.frame for joining to the spatial data frame
obs_place_value <- data.frame(observation_place, observation_value2)
# Convert spatialpolydf to an sf object
spatialpolysf <- spatialpolydf %>% st_as_sf()
# Join the data
spatialpolysf2 <- spatialpolysf %>%
left_join(obs_place_value, by = c("Name", "Description"))
# Use ggplot2 to plot the data
ggplot(spatialpolysf2) +
geom_sf(aes(fill = observation_value2))