简体   繁体   English

Z - R中多边形(shapefile)的值

[英]Z - Values for polygon (shapefile) in R

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. 我的目标是在R中创建一个3D可视化。我在柏林有一个城区的形状文件(Ortsteile),并希望将值(居民/km²)作为z值突出显示。 I have implemented the shapefile into R and coloured the value for desnity ("Einwohnerd") as followed: 我已将shapefile实现为R并为desnity(“Einwohnerd”)着色,如下所示:

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")

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? 如何将某个多边形(市区)引用为z值(居民/km²),如何突出显示该z值?

Hope that someone will have an answer! 希望有人能有答案! Best regars SB 最好的后卫SB

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. 感谢您的回答,但我仍然想知道最好将密度用作z值,这样我就可以创建一个3D模型。 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. 我发现不可能使用形状的多边形,但可以光栅化多边形并使用矩阵进行不同的透视和旋转。

Here is the code but the final 3D visualization looks not sharp and good enough. 这是代码,但最终的3D可视化看起来不够锐利和足够好。 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: 也许最好以另一种方式计算z值,以便第一个值不会开始如此之高或者使用多边形的中心而不是在z方向上绘制一个列:

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)

Is this what you had in mind? 这是你的想法吗?

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()

Explanation 说明

This is a great question, in that it provides an example of a very basic choropleth map using ggplot in R. 这是一个很好的问题,因为它提供了一个使用R中的ggplot的非常基本的等值线图的ggplot

Shapefiles can be read into R using readOGR(...) , producing SpatialDataFrame objects. 可以使用readOGR(...)将Shapefile读入R,从而生成SpatialDataFrame对象。 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. 后者基本上有两个部分:包含多边形边界坐标的polygons部分,以及包含shapefile中属性表的信息的data部分。 These can be referenced, respectively, as map@polygons and map@data . 这些可以分别作为map@polygonsmap@data引用。

The code above reads the shapefile and transforms the coordinates to epsg:4839 . 上面的代码读取shapefile并将坐标转换为epsg:4839 Then we prepend the polygon ids (stored in the rownames) to the other information in map@data , creating map.data . 然后我们将多边形id(存储在rownames中)添加到map@data的其他信息,创建map.data Then we use the fortify(...) function in ggplot to convert the polygons to a dataframe suitable for plotting ( map.df ). 然后我们使用ggplotfortify(...)函数将多边形转换为适合绘图的数据map.dfmap.df )。 This dataframe has a column id which corresponds to the id column in map.data . 此数据map.data的列idmap.dataidmap.data Then we merge the attribute information ( map.data ) into map.df based on the id column. 然后我们基于id列将属性信息( map.data )合并到map.df

The ggplot calls create the map layers and render the map, as follows: ggplot调用创建地图图层并渲染地图,如下所示:

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

A note on scale_fill_gradientn(...) : this function assigns colors to the fill values by interpolating a color palette provided in the colours= parameter. 关于scale_fill_gradientn(...)注释:此函数通过插入colours=参数中提供的调色板为填充值指定颜色。 Here we use the Spectral palette from www.colorbrewer.org . 在这里,我们使用www.colorbrewer.org的Spectral调色板。 Unfotrunately, this palette has the colors revered (blue - red), so we use rev(...) to reverse the color order (high=red, low=blue). 不幸的是,这个调色板有着名的颜色(蓝色 - 红色),所以我们使用rev(...)来反转颜色顺序(高=红色,低=蓝色)。 If you prefer the more highly saturated colors common in matlab, use library(colorRamps) and replace the call to scale_fill_gradientn(...) with: 如果您更喜欢matlab中常见的更高饱和度的颜色,请使用library(colorRamps)并将对scale_fill_gradientn(...)的调用替换为:

  scale_fill_gradientn(colours=matlab.like(10))+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM