Z - Values for polygon (shapefile) in R - 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. 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")
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?
Hope that someone will have an answer!
Best regars
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. 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. 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)

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.
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.
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.
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
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))+

Related

Base R Choropleth: colors aren't being applied to the map according to the order of the interval/breaks which makes the map hard to read

I created a choropleth with base R but I'm struggling with the colors. First, the colors don't follow the same order as the intervals and second, two of the intervals are using the same color, all of which makes the graph hard to read. This happens regardless of how many colors I use. It also doesn't matter whether I'm using brewer.pal or base colors.Here is a map with its respective legend illustrating the issue.
Below are the statements that I use to create the graph once data has been downloaded:
#Relevant packages:
library(dplyr)
library(RColorBrewer)
library(rgdal)
#create colors vector
pop_colors <- brewer.pal(8,"Purples")
#create breaks/intervals
pop_breaks <- c(0,20000,40000,60000,80000,100000,120000)
#apply breaks to population
cuts <- cut(cal_pop$Pop2016, pop_breaks, dig.lab = 6)
#create a vector with colors by population according to the interval they belong to:
color_breaks <- pop_colors[findInterval(cal_pop$Pop2016,vec = pop_breaks)]
Create choropleth
plot(cal_pop,col = color_breaks, main = "Calgary Population (2016)")
#create legend
legend("topleft", fill = color_breaks, legend = levels(cuts), title = "Population")
I used readOGR() command to read the shape file, which I'm linking here in case anybody is interested in taking a look at the data.
I'd appreciate any advice you could give me.
Thanks!
Your error is in this line:
color_breaks <- pop_colors[findInterval(cal_pop$Pop2016,vec = pop_breaks)]
I can't read your data file, so I'll use a built-in one from the sf package.
library(sf)
nc <- readOGR(system.file("shapes/", package="maptools"), "sids")
str(nc#data)
colors <- brewer.pal(8,"Purples")
#create breaks/intervals
sid_breaks <- c(0,2,4,6,8,10,12,20,60)
#apply breaks to population
sid_cuts <- cut(nc$SID79, sid_breaks, dig.lab = 6, include=TRUE)
#create a vector with colors by population according to the interval they belong to:
sid_colors <- colors[sid_cuts]
#Create choropleth
par(mar=c(0,0,0,0))
plot(nc, col = sid_colors)
legend("bottomleft", fill = colors, legend = levels(sid_cuts), nc=2, title = "SID (1979)", bty="n")

Overlap image plot on a Google Map background in R

I'm trying to add this plot of a function defined on Veneto (italian region)
obtained by an image and contour:
image(X,Y,evalmati,col=heat.colors(100), xlab="", ylab="", asp=1,zlim=zlimits,main=title)
contour(X,Y,evalmati,add=T)
(here you can find objects: https://dl.dropboxusercontent.com/u/47720440/bounty.RData)
on a Google Map background.
I tried two ways:
PACKAGE RGoogleMaps
I downloaded the map mbackground
MapVeneto<-GetMap.bbox(lonR=c(10.53,13.18),latR=c(44.7,46.76),size = c(640,640),MINIMUMSIZE=TRUE)
PlotOnStaticMap(MapVeneto)
but i don't know the commands useful to add the plot defined by image and contour to the map
PACKAGE loa
I tried this way:
lat.loa<-NULL
lon.loa<-NULL
z.loa<-NULL
nx=dim(evalmati)[1]
ny=dim(evalmati)[2]
for (i in 1:nx)
{
for (j in 1:ny)
{
if(!is.na(evalmati[i,j]))
{
lon.loa<-c(lon.loa,X[i])
lat.loa<-c(lat.loa,Y[j])
z.loa<-c(z.loa,evalmati[i,j])
}
}
}
GoogleMap(z.loa ~ lat.loa*lon.loa,col.regions=c("red","yellow"),labels=TRUE,contour=TRUE,alpha.regions=list(alpha=.5, alpha=.5),panel=panel.contourplot)
but the plot wasn't like the first one:
in the legend of this plot I have 7 colors, and the plot use only these values. image plot is more accurate.
How can I add image plot to GoogleMaps background?
If the use of a GoogleMap map is not mandatory (e.g. if you only need to visualize the coastline + some depth/altitude information on the map), you could use the package marmap to do what you want. Please note that you will need to install the latest development version of marmap available on github to use readGEBCO.bathy() since the format of the files generated when downloading GEBCO files has been altered recently. The data from the NOAA servers is fine but not very accurate in your region of interest (only one minute resolution vs half a minute for GEBCO). Here is the data from GEBCO I used to produce the map : GEBCO file
library(marmap)
# Get hypsometric and bathymetric data from either NOAA or GEBCO servers
# bath <- getNOAA.bathy(lon1=10, lon2=14, lat1=44, lat2=47, res=1, keep=TRUE)
bath <- readGEBCO.bathy("GEBCO_2014_2D_10.0_44.0_14.0_47.0.nc")
# Create color palettes for sea and land
blues <- c("lightsteelblue4", "lightsteelblue3", "lightsteelblue2", "lightsteelblue1")
greys <- c(grey(0.6), grey(0.93), grey(0.99))
# Plot the hypsometric/bathymetric map
plot(bath, land=T, im=T, lwd=.03, bpal = list(c(0, max(bath), greys), c(min(bath), 0, blues)))
plot(bath, n=1, add=T, lwd=.5) # Add coastline
# Transform your data into a bathy object
rownames(evalmati) <- X
colnames(evalmati) <- Y
class(evalmati) <- "bathy"
# Overlay evalmati on the map
plot(evalmati, land=T, im=T, lwd=.1, bpal=col2alpha(heat.colors(100),.7), add=T, drawlabels=TRUE) # use deep= shallow= step= to adjust contour lines
plot(outline.buffer(evalmati),add=TRUE, n=1) # Outline of the data
# Add cities locations and names
library(maps)
map.cities(country="Italy", label=T, minpop=50000)
Since your evalmati data is now a bathy object, you can adjust its appearance on the map like you would for the map background (adjust the number and width of contour lines, adjust the color gradient, etc). plot.bath() uses both image() and contour() so you should be able to get the same results as when you plot with image(). Please take a look at the help for plot.bathy() and the package vignettes for more examples.
I am not realy inside the subject, but Lovelace, R. "Introduction to visualising spatial data in R" might help you
https://github.com/Robinlovelace/Creating-maps-in-R/raw/master/intro-spatial-rl.pdf From section "Adding base maps to ggplot2 with ggmap" with small changes and data from https://github.com/Robinlovelace/Creating-maps-in-R/archive/master.zip
library(dplyr)
library(ggmap)
library(rgdal)
lnd_sport_wgs84 <- readOGR(dsn = "./Creating-maps-in-R-master/data",
layer = "london_sport") %>%
spTransform(CRS("+init=epsg:4326"))
lnd_wgs84_f <- lnd_sport_wgs84 %>%
fortify(region = "ons_label") %>%
left_join(lnd_sport_wgs84#data,
by = c("id" = "ons_label"))
ggmap(get_map(location = bbox(lnd_sport_wgs84) )) +
geom_polygon(data = lnd_wgs84_f,
aes(x = long, y = lat, group = group, fill = Partic_Per),
alpha = 0.5)

Plot polygons with different colors and not overwrite the previous polygons if overlapped

I am graphing several n-edge polygons on the same plot. Let say:
1/ n=3: draw polygon with 3 edges, color it with "pink"
2/ n=6: draw polygon with 6 edges, will color with "grey". At this point, I see that the first polygon in step 1 is overlapped by this one. In this case, I just want to keep the "pink" color of the first polygon and color the rest "un-overlapped" area of 2nd polygon with "grey" color.
I have tried some code as follow, but it always display "grey" polygon, instead of "pink" and "grey" areas.
BTW, I also walked around this problem by "draw the 6-edge polygon (n=6) first, and then draw 3-edges polygon (n=3)". By changing the drawing order from the biggest polygon to the smallest one, I can keep the color of the biggest and smallest polygons at the end. However, I would like to do the steps as I mentioned at the beginning of this questions so that i can see the plotting areas are increasing when n (number of edges) keeps increasing.
If you have any suggestions, please advice me. Thank you very much!
cat("\014")
rm(list=ls())
#############################
# first polygon
#n=3
xx3=c(0,-3,3);xx3
yy3=c(1,1,-2);yy3
#plot each intersection /vertex of polygon n=3
plot (xx3, yy3,type = "p", xlim=c(-8,8), ylim=c(-8,8),col="blue",xlab = "x", ylab = "y")
# display value of each point above
text(xx3, yy3, paste("(",round(xx3, 2),"," ,round(yy3, 2), ")"),
cex=0.8,family="mono", font=1, adj=1.5, pos=3)
#fill the shade area
polygon(xx3, yy3, col = "pink", border = "pink")
title("Plot n-edge polygon")
#############################
# RUN untill this point and stop.
#And then run following part, you will see the 1st polygon is overlapping region
#and is fully overwrited by the second polygon.
#############################
# Second polygon
#n=6
par(new=TRUE)
xx=c(0,-15/11,-15/4,-45/11,-3, 3);xx
yy=c(1,20/11,5/2,20/11,1,-2);yy
#plot each intersection /vertex of polygon n=6
points(xx, yy,type = "p", col="blue",xlab = "x", ylab = "y")
# display value of each point above
text(xx, yy, paste("(",round(xx, 2),"," ,round(yy, 2), ")"),
cex=0.8,family="mono", font=1, adj=1.5, pos=3)
#fill the shade area
polygon(xx, yy, col = "grey", border = "grey")
#draw x=0,y=0
abline(a=0,b=0,v=0)
One possibility is to compute the difference between the current polygon (bigger), and the previous one (smaller). I don't know if there is some easy way to compute geometries other than using sp (spatial objects) and rgeos.
Here some code that uses the packages sp and rgeos packages. The approach consists to compute the polygonal difference, by means of spatial objects, and plot it. This might not be the most elegant way, but at least it will do what you want.
require(sp)
require(rgeos)
#test data
xx3=c(0,-3,3);xx3
yy3=c(1,1,-2);yy3
xx=c(-5,-5,5,5);xx
yy=c(-5,5,5,-5);yy
#create a SpatialPolygons object for n = 3
sp3 <- as.data.frame(cbind(xx3,yy3))
sp3 <- rbind(sp3, sp3[1,])
coordinates(sp3) <- c("xx3","yy3")
p3 <- Polygon(sp3)
ps3 <- Polygons(list(p3),1)
sps3 <- SpatialPolygons(list(ps3))
#create a SpatialPolygons object for n = 6
sp <- as.data.frame(cbind(xx,yy))
sp <- rbind(sp, sp[1,])
coordinates(sp) <- c("xx","yy")
p <- Polygon(sp)
ps <- Polygons(list(p),1)
sps <- SpatialPolygons(list(ps))
#compute the difference (with rgeos)
#between the current polygon (bigger) and the previous one (smaller)
spsdiff <- gDifference(sps, sps3)
For plotting the difference, 2 ways:
#Plotting 1: based on sp-plot
#===========
plot(sps, border="transparent") #to set some bigger extent
plot(sps3, add=T, col = "pink")
plot(spsdiff, add=T, col = "grey")
#Plotting 2: use polygon and polypath base functions
#===========
#preparing data for using polypath (polygons with hole)
polys <- spsdiff#polygons[[1]]#Polygons
coords <- do.call("rbind", lapply(polys, function(x){ if(x#hole) x#coords }))
holes <- do.call("rbind", lapply(polys,function(x){ if(!x#hole) rbind(rep(NA,2),x#coords) }))
poly.coords <- rbind(coords,holes)
#plot it
plot(xx, yy, col = "transparent")
polygon(xx3, yy3, col = "pink")
polypath(poly.coords[,1],poly.coords[,2],col="grey", rule="evenodd")
If you have to repeat this, you can re-use this code within a loop to iteratively plot the polygon differences.
Note: rgeos requires you to install the GEOS library on your machine

How to add continuous color legend to an R map made with maps

I'm using the R code shown below, which loads libraries maps and RColorBrewer, to create a map of the world with countries color-coded by population rank. As you can see in the image below, I'm using a green palette in which the darker the green, the larger the population.
I'd like to add a continuous color legend showing the full palette to denote that light green = small population and dark green = large population, but I can't find a way to do it via maps. Could you tell me what is the easiest way to add a continuous color legend (or color key/color scale) to my map?
# Load libraries
library(maps)
library(RColorBrewer)
# Load world data
data(world.cities)
# Calculate world population by country
world.pop = aggregate(x=world.cities$pop, by=list(world.cities$country.etc),
FUN=sum)
world.pop = setNames(world.pop, c('Country', 'Population'))
# Create a color palette
palette = colorRampPalette(brewer.pal(n=9, name='Greens'))(nrow(world.pop))
# Sort the colors in the same order as the countries' populations
palette = palette[rank(-world.pop$Population)]
# Draw a map of the world
map(database='world', fill=T, col=palette, bg='light blue')
The world map in the maps package is about 30 years old (e.g., has USSR & Yugoslavia).
Plus you have a glitch in your code that causes the overpopulated Greenland that #Jealie noticed (and India is less populated than Antarctica).
You can create a continuousish legend with a modern world using rworldmap.
library(rworldmap)
library(RColorBrewer)
#get a coarse resolution map
sPDF <- getMap()
#using your green colours
mapDevice('x11') #create a map shaped device
numCats <- 100 #set number of categories to use
palette = colorRampPalette(brewer.pal(n=9, name='Greens'))(numCats)
mapCountryData(sPDF,
nameColumnToPlot="POP_EST",
catMethod="fixedWidth",
numCats=numCats,
colourPalette=palette)
You can alter the legend adding more labels etc. by doing something like this :
mapParams <- mapCountryData(sPDF, nameColumnToPlot="POP_EST", catMethod="pretty", numCats=100, colourPalette=palette, addLegend=FALSE)
#add a modified legend using the same initial parameters as mapCountryData
do.call( addMapLegend, c( mapParams
, legendLabels="all"
, legendWidth=0.5
))
Just briefly to explore the glitch in your code. It occurs because you create a palette for the number of countries in world.cities (239) and then apply it to the number of polygons in the world database from maps (2026). So it probably gets recycled and the colours of your countries have no relation to population. The code below demonstrates the source of your problem.
#find the countries used in the maps world map
mapCountries <- unique( map('world',namesonly=TRUE) )
length(mapCountries)
#[1] 2026
#exclude those containing ':' e.g. "USA:Alaska:Baranof Island"
mapCountries2 <- mapCountries[-grep(':',mapCountries)]
length(mapCountries2)
#[1] 186
#which don't match between the map and world.cities ?
#cityCountries <- unique( world.cities$country.etc )
cityCountries <- world.pop$Country
length(cityCountries)
#[1] 239
#which countries are in the map but not in world.cities ?
mapCountries2[ is.na(match(mapCountries2,cityCountries)) ]
#includes USSR, Yugoslavia & Czechoslovakia
Within the library SDMTools there is the function legend.gradient
adding this code to the end of your code should give the desired result:
# Draw a map of the world
map(database='world', fill=T, col=palette, bg='light blue')
x = c(-20, -15, -15, -20)
y = c(0, 60, 60, 0)
legend.gradient(cbind(x = x - 150, y = y - 30),
cols = brewer.pal(n=9, name='Greens'), title = "TITLE", limits = "")
You will need to fiddle with the x & y coordinates to get the legend into the desired location however.
EDIT
The x and y coordinates also adjust the shape of the box so I changed the code so that the box shape would not change if you only alter the numbers within the legend.gradient function. Below is what this code should produce

plot multiple shp file on a graph using spplot in R

I have 3 shp files representing the house, room, and beds of a house respectively. I need to plot them on a graph using R so that they all overlap with each other. I know that in plot function, I can use line to plot new lines on top of the existing plot, is there anything equivalent in spplot? Thanks.
Here's one approach, using the nifty layer() function from the latticeExtra package:
# (1) Load required libraries
library(sp)
library(rgeos) # For its readWKT() function
library(latticeExtra) # For layer()
# (2) Prepare some example data
sp1 = readWKT("POLYGON((0 0,1 0,1 1,0 1,0 0))")
sp2 = readWKT("POLYGON((0 1,0.5 1.5,1 1,0 1))")
sp3 = readWKT("POLYGON((0.5 0,0.5 0.5,0.75 0.5,0.75 0, 0.5 0))")
# spplot provides "Plot methods for spatial data with attributes",
# so at least the first object plotted needs a (dummy) data.frame attached to it.
spdf1 <- SpatialPolygonsDataFrame(sp1, data=data.frame(1), match.ID=1)
# (3) Plot several layers in a single panel
spplot(spdf1, xlim=c(-0.5, 2), ylim=c(-0.5, 2),
col.regions="grey90", colorkey=FALSE) +
layer(sp.polygons(sp2, fill="saddlebrown")) +
layer(sp.polygons(sp3, fill="yellow"))
Alternatively, you can achieve the same result via spplot()'s sp.layout= argument. (Specifying first=FALSE ensures that the 'roof' and 'door' will be plotted after/above the grey square given as spplot()'s first argument.)
spplot(spdf1, xlim=c(-0.5, 2), ylim=c(-0.5, 2),
col.regions="grey90", colorkey=FALSE,
sp.layout = list(list(sp2, fill="saddlebrown", first=FALSE),
list(sp3, fill="yellow", first=FALSE)))
You can use the sp.layout argument in spplot. Alternatively, you can use ggplot2. Some example code (untested):
library(ggplot2)
shp1_data.frame = fortify(shp1)
shp1_data.frame$id = "shp1"
shp2_data.frame = fortify(shp2)
shp2_data.frame$id = "shp2"
shp = rbind(shp1_data.frame, shp2_data.frame)
ggplot(aes(x = x, y = y, group = group, col = id), data = shp) + geom_path()
In ggplot2, columns in the data are linked to graphical scales in the plot. In this case x is the x-coordinate, y is the y-coordinate, group is a column in the data.frame shp which specifies to which polygon a point belongs, and col is the color of the polygon. The geometry I used is geom_path, which draws a series of lines based on the polygon input data.frame. An alternative is to use geom_poly, which also supports filling the polygon.

Resources