Visualising GIS data in R

Plotting a map

library(rgdal)
yta = readOGR("/home/hans/text/forskning/brandforsk/create-map-data/sve_yt.shp", "sve_yt")
vägar <- readOGR("/home/hans/text/forskning/brandforsk/create-map-data/sve_vag.shp", "sve_vag")
sdn = readOGR("/home/hans/text/forskning/brandforsk/create-map-data/sdn-dataframe-coordinates.shp", "sdn-dataframe-coordinates", input_field_name_encoding="iso-8859-1")
for(i in 1:length(names(sdn))) {
  if (class(sdn[[i]]) == "factor") {
    sdn[[i]] <- factor(iconv(sdn[[i]], from="latin1", to="utf-8"))
  }
}
sdn <- spTransform(sdn, CRS(proj4string(vägar)))
plot(yta[which(yta$KKOD == 5),], col = "lightgreen")
plot(yta[which(yta$KKOD == 4),], col = "yellow", add=T)
plot(yta[which(yta$KKOD == 1),], col = "blue", add=T)
plot(yta[which(yta$KKOD == 7),], col = "white", add=T)
plot(yta[which(yta$KKOD == 3),], col = "lightblue", add=T)
plot(vägar[which(vägar$VAGNR1 != ""),], col = "red", add=T)
plot(sdn, add=T)

Plotting a part of a map?

plot(sdn[which(sdn$NAMN == "14 Tynnered"),]) plot(coords[which(overlay(coords[anlagda], sdn[which(sdn$NAMN == "14 Tynnered"),]) == 1)], add = T)

Plot a part of a map, include only events for a certain period

plot(sdn[which(sdn$NAMN == "14 Tynnered"),]) plot(coords[which(overlay(coords[anlagda], sdn[which(sdn$NAMN == "14 Tynnered"),]) == 1)], add = T)

Plotting a map to png, and dots on a transparent png (overlay)

png(file="karta.png", width=1600, height=1200)
plot(yta[which(yta$KKOD == 5),], col = "lightgreen")
plot(yta[which(yta$KKOD == 4),], col = "yellow", add=T)
plot(yta[which(yta$KKOD == 1),], col = "blue", add=T)
plot(yta[which(yta$KKOD == 7),], col = "white", add=T)
plot(yta[which(yta$KKOD == 3),], col = "lightblue", add=T)
plot(vägar[which(vägar$VAGNR1 != ""),], col = "red", add=T)
plot(sdn, add=T)
dev.off()

png(file="2001.png", width=1600, height=1200, bg="transparent")
plot(yta[which(yta$KKOD == 5),], col = "transparent")
plot(coords, add=T)
dev.off()

Adding a large number of overlays to one map.

If you have data with both time and space points, you might want to create a animated movie showing events taking place over time.

To create a large number of overlays to a map - which then can be rendered with the map as background and joined to an animated movie - we repeat the second step above generalising filename and the coords to be plotted. To generalise the coords to be plotted a function is needed to create a vector of indices based an a (series of) condition(s).

Create a list of vectors of coordinates

Let's start with creating one overlay per year between 2000 and 2009.

bränder[[6]]
holds the year of the event.
a <- sapply(2000:2009, function(x) {coords[which((Orsak == "Anlagd med uppsåt" | Orsak == "Barns lek med eld" | Orsak == "Fyrverkerier" ) & bränder[[6]] == x),]})

Generating png:s from this list of vectors

my.list <- lapply(1, function(x) {png(file=paste(x,".png", sep =""), width=1600, height=1200, bg="transparent") print(str(a$"2009")) plot(yta[which(yta$KKOD == 5),], col = "transparent") add=T) plot(a$"2009", add=T) dev.off()})

comments powered by Disqus


Back to the index

Blog roll

R-bloggers, Debian Weekly
Valid XHTML 1.0 Strict [Valid RSS] Valid CSS! Emacs Muse Last modified: oktober 17, 2019