Z - Values ​​for the polygon (shapefile) in R

My goal is to create a 3D visualization in R. I have a shape file for the city blocks (Ortsteile) in Berlin and want to emphasize the value (inhabitants / km²) as z-value. I embedded the shapefile into R and colored the value for desnity ("Einwohnerd") as follows:

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 can a certain polygon (urban district) be attributed to the z value (resident / km²) and how can I distinguish this z value?

Hope someone will have an answer! Best Regan SB

Thanks for the answer, but I'm still on my way to find out how to use density as a z-value so that I can create a 3D model. I learned that it is impossible to use shape polygons, but you can rasterize a polygon and use the matrix for a different perspective and rotation.

, 3D- . , 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)
+4
1

, ?

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

, , ggplot R.

Shapefiles R readOGR(...), SpatialDataFrame. : polygons, , data, . , , map@polygons map@data.

epsg: 4839. ( ) map@data, map.data. fortify(...) ggplot , (map.df). id, id map.data. (map.data) map.df id.

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

scale_fill_gradientn(...): , colours=. www.colorbrewer.org. , , (-), rev (...) ( = , = ). , matlab, library(colorRamps) scale_fill_gradientn(...) :

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

All Articles