Z - R 中多边形(shapefile)的值 [英] Z - Values for polygon (shapefile) in R
问题描述
我的目标是在 R 中创建一个 3D 可视化.我有一个柏林市区 (Ortsteile) 的 shapefile,并希望将值 (居民/平方公里) 突出显示为 z 值.我已经将 shapefile 实现到 R 中,并为 desnity(Einwohnerd")的值着色如下:
my goal is to create a 3D-Visualization in R. I have a shapefile of urban districts (Ortsteile) in Berlin and want to highlight the value (inhabitants/km²) as a z-value. I have implemented the shapefile into R and coloured the value for desnity ("Einwohnerd") as followed:
library(rgdal)
library(sp)
berlin=readOGR(dsn="C...etc.", layer="Ortsteile")
berlin@data
col <- rainbow(length(levels(berlin@data$Name)))
spplot(berlin, "Einwohnerd", col.regions=col, main="Ortsteil Berlins", sub="Datensatz der Stadt Berlin", lwd=.8, col="black")
如何将某个多边形(市区)引用到 z 值(居民/平方公里)以及如何突出显示该 z 值?
How it is posible to refer a certain polygon (urban district) to a z-value (inhabitant/km²) and how can I highlight this z-value?
希望有人能给出答案!最好的问候SB
Hope that someone will have an answer! Best regars SB
感谢您的回答,但我仍在努力寻找使用密度作为 z 值的最佳方法,以便我可以创建 3D 模型.我发现无法使用形状的多边形,但可以栅格化多边形并使用矩阵进行不同的透视和旋转.
Thanks for the answer, but I am still on my wy to find out the best to use the density as z-value so that I can create a 3D Model. I found out that it is not possible to use the polygons of the shape but that it is possible to rasterize the polygon and to use a matrix for a different perspective and rotation.
这是代码,但最终的 3D 可视化看起来不够清晰和不够好.也许最好以另一种方式计算 z 值,这样第一个值就不会那么高,或者使用多边形的中心,而不是在 z 方向上绘制一列:
Here is the code but the final 3D visualization looks not sharp and good enough. Maybe it would be better to calculate the the z-value in anther way so that the first values did not start so high or to use the center of the polygon and than to draw a column in z-direction:
library(rgdal)
library(sp)
setwd("C:\...")
berlin=readOGR(dsn="C:\...\Ortsteile", layer="Ortsteile")
col <- rainbow(length(levels(berlin@data$Name)))
spplot(berlin, "Einwohnerd", col.regions=col, main="Ortsteil Berlins",
sub="Datensatz der Stadt Berlin", lwd=.8, col="black")
library(raster)
raster <- raster(nrows=100, ncols=200, extent(berlin))
test <- rasterize(berlin, raster, field="Einwohnerd")
persp(test, theta = 40, phi = 40, col = "gold", border = NA, shade = 0.5)
for(i in seq(0,90,10)){
persp(test, theta = 40, phi = i, col = "gold", border = NA, shade = 0.5)
}
library(rgl)
library(colorRamps)
mat <- matrix(test[], nrow=test@nrows, byrow=TRUE)
image(mat)
persp3d(z = mat, clab = "m")
persp3d(z = mat, col = rainbow(10),border = "black")
persp3d(z = mat, facets = FALSE, curtain = TRUE)
推荐答案
这是你的想法吗?
library(ggplot2)
library(rgdal) # for readOGR(...) and spTransform(...)
library(RColorBrewer) # for brewer.pal(...)
setwd("<directory with shapefile>")
map <- readOGR(dsn=".",layer="Ortsteile")
map <- spTransform(map,CRS=CRS("+init=epsg:4839"))
map.data <- data.frame(id=rownames(map@data), map@data)
map.df <- fortify(map)
map.df <- merge(map.df,map.data,by="id")
ggplot(map.df, aes(x=long, y=lat, group=group))+
geom_polygon(aes(fill=Einwohnerd))+
geom_path(colour="grey")+
scale_fill_gradientn(colours=rev(brewer.pal(10,"Spectral")))+
theme(axis.text=element_blank())+
labs(title="Berlin Ortsteile", x="", y="")+
coord_fixed()
说明
这是一个很好的问题,因为它提供了一个在 R 中使用 ggplot
的非常基本的等值线图示例.
This is a great question, in that it provides an example of a very basic choropleth map using ggplot
in R.
Shapefile 可以使用 readOGR(...)
读入 R,生成 SpatialDataFrame
对象.后者基本上有两个部分:包含多边形边界坐标的 polygons
部分和包含来自 shapefile 中的属性表的信息的 data
部分.这些可以分别引用为 map@polygons
和 map@data
.
Shapefiles can be read into R using readOGR(...)
, producing SpatialDataFrame
objects. The latter have basically two sections: a polygons
section containing the coordinates of the polygon boundaries, and a data
section containing information from the attributes table in the shapefile. These can be referenced, respectively, as map@polygons
and map@data
.
上面的代码读取 shapefile 并将坐标转换为 epsg:4839.然后我们在 map@data
中的其他信息前面添加多边形 id(存储在 rownames 中),创建 map.data
.然后我们使用 ggplot
中的 fortify(...)
函数将多边形转换为适合绘图的数据框(map.df
).这个数据框有一个 id
列,它对应于 map.data
中的 id
列.然后我们根据id
列将属性信息(map.data
)合并到map.df
中.
The code above reads the shapefile and transforms the coordinates to epsg:4839. Then we prepend the polygon ids (stored in the rownames) to the other information in map@data
, creating map.data
. Then we use the fortify(...)
function in ggplot
to convert the polygons to a dataframe suitable for plotting (map.df
). This dataframe has a column id
which corresponds to the id
column in map.data
. Then we merge the attribute information (map.data
) into map.df
based on the id
column.
ggplot 调用创建地图图层并渲染地图,如下所示:
The ggplot calls create the map layers and render the map, as follows:
ggplot: set the default dataset to map.df; identify x- and y-axis columns
geom_polygon: identify column for fill (color of polygon)
geom_path: polygon boundaries
theme: turn off axis text
labs: title, turn off x- and y-axis labels
coord_fixed: ensures that the map is not distorted
关于scale_fill_gradientn(...)
的说明:此函数通过插入colours=
参数中提供的调色板来为填充值分配颜色.这里我们使用来自 www.colorbrewer.org 的光谱调色板.不幸的是,这个调色板的颜色受到尊重(蓝色 - 红色),所以我们使用 rev(...) 来反转颜色顺序(高 = 红色,低 = 蓝色).如果您更喜欢 matlab 中常见的更高饱和度的颜色,请使用 library(colorRamps)
并将对 scale_fill_gradientn(...)
的调用替换为:
A note on scale_fill_gradientn(...)
: this function assigns colors to the fill values by interpolating a color palette provided in the colours=
parameter. Here we use the Spectral palette from www.colorbrewer.org. Unfotrunately, this palette has the colors revered (blue - red), so we use rev(...) to reverse the color order (high=red, low=blue). If you prefer the more highly saturated colors common in matlab, use library(colorRamps)
and replace the call to scale_fill_gradientn(...)
with:
scale_fill_gradientn(colours=matlab.like(10))+
这篇关于Z - R 中多边形(shapefile)的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!