first exercise: Click_here
second exercise: Click_here
solution to second exercise :click_here
############################################################################
### the purpose of this is to learn to create maps in R
############################################################################
###########################################################################
### installing and downloading relavant libraries
###########################################################################
#removing ervery ting in R
rm(list=ls())
# sometimes you need to define the loaction of the libraries
.libPaths("E:/Dropbox/R_packges")
.libPaths()
# list of relevant libraries
pack<-c("rgdal", "classInt", "GISTools", "maps", "dismo", "raster", "foreign", "shape")
# installing relavnt libraries from list
# for (i in pack){
# install.packages(pkgs=i, dependencies=TRUE)
# }
# update.packages()
# calling libraries
lapply(pack, require, character.only=T)
###########################################################################
### setting working directory
###########################################################################
# defining location
data <-"E:/Dropbox/postdoc/course/PHD_course/PHD_GIS_2017/exercises/data/GIS_data"
results <-"E:/Dropbox/postdoc/course/PHD_course/PHD_GIS_2017/exercises/exercise_1"
# what is my working directory
getwd()
# changing directory
setwd(data)
# looking in the directory folder
dir()
###########################################################################
### reading in a shapfiles into R
###########################################################################
# reading in a single shape fiel
Nord_jylland <- readOGR(dsn=data,
layer="Nord_jylland")
# chekking the class of the Nord_jylland object
class(Nord_jylland)
# cheking the projection
proj4string(Nord_jylland)
# setting the projecting
proj4string(Nord_jylland)<-CRS("+proj=utm +zone=32 +ellps=GRS80 +units=m +no_defs")
# ploting the Nord_jylland
plot(Nord_jylland)
# creating a summary of the attribute table of the shapefile
summary(Nord_jylland)
# looking into the column names of the dataframe
names(Nord_jylland)
## looking at a specific column
Nord_jylland$ID_NR
## looking into column type
str(Nord_jylland$ID_NR)
## changning the column type
str(Nord_jylland$ID_NR)
Nord_jylland$ID_NR_factor<-Nord_jylland$ID_NR
Nord_jylland$ID_NR_factor_text<-as.character(Nord_jylland$ID_NR)
Nord_jylland$ID_NR_factor_num<-as.numeric(as.character(Nord_jylland$ID_NR))
# looking at the attribute table
head(data.frame(Nord_jylland), n =10)
# choosing a line in the dataframe
data.frame(Nord_jylland)[1:10,]
# choosing a column in the dataframe
data.frame(Nord_jylland)[,1]
# choosing a specific number in the dataframe
data.frame(Nord_jylland)[2,1]
# removing Nord_jylland
rm(Nord_jylland)
# the list of objects loaded into R
ls()
###########################################################################
### the "for" loop
###########################################################################
# creating a list
shape_file_names<-c("Nord_jylland", "coast","forest","harbour","highway","high_way_2","lake"
,"large_roads","railways","municipality"
,"parish","sea","urban_area","windturbines","Aalborg")
# example of a for loop
for(i in shape_file_names){
print(i)
}
# printing the shape_file_names
print(shape_file_names)
# what is the difference?
###########################################################################
### the "if" loop
###########################################################################
# example of a for loop
for(i in shape_file_names){
if (i!="Nord_jylland"){
print(i)}
}
print(shape_file_names[2:15])
# what is the difference?
###########################################################################
### exercise 1.1: reading multiple shape files
###########################################################################
# try to read in mupletiple shapefile by using the different tools above.
# hint: your might want to consider using the assign function. try to this commaned: ?assign
# compare your solutions in your group and sent the best solution to Toke - tepp@ifro.ku.dk
###########################################################################
### writing a shape file
###########################################################################
writeOGR(sea, dsn=results,
layer="sea", driver="ESRI Shapefile")
# open the written file in QGIS and have a look
#####################################################################
## exercise 1.2: create a shapefile
#####################################################################
# try to write a wind turbine shapefile where you have excluded all turbines lower than 46 meters
# hint: your might want to use the command ?subset
#####################################################################
## plot data
#####################################################################
# which colors are available
colors (distinct = FALSE)
# which red colors
colors()[grep("red",colors())]
# check out color scheme
https://docs.google.com/viewer?url=www.cs.uns.edu.ar%2F~jac%2FPHP%2Fdownloads%2F%2FLenguaje+R%2FRcolorstyle.pdf
# different color schemes
n<-5
rainbow(n, s = 1, v = 1, start = 0, end = max(1, n - 1)/n, alpha = 1)
heat.colors(n, alpha = 1)
terrain.colors(n, alpha = 1)
topo.colors(n, alpha = 1)
cm.colors(n, alpha = 1)
# color wheel
pie(rep(1, 5), col = heat.colors(5))
pie(rep(1, 12), col = rainbow(12))
# check out this link
http://www.statmethods.net/advgraphs/parameters.html
http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf
## here is the avialbale shape file (they are called spatialdataframes )
colors()[grep("blue",colors())]
# Adding Aalborg municipality border
plot(Aalborg, col=colors()[c(18)])
# plotting region
plot(Nord_jylland, col=colors()[c(18)], add=TRUE)
# plotting sea
plot(sea, col=colors()[c(26)], add=TRUE)
# plot urban area
plot(urban_area, col="skyblue1", add=TRUE, lty=0)
# highway
plot(highway, col="gray40", add=TRUE, lty=1,lwd=1)
#highway_2
plot(high_way_2, col="gray40", add=TRUE, lty=1,lwd=1)
# large_roads
plot(large_roads, col="gray60", add=TRUE, lty=1,lwd=1)
# railways
plot(railways, col="gray40", add=TRUE, lty=1,lwd=1)
#########################################################################
### plotting windturbines using a color scheme to show hights
########################################################################
str(windturbines$hight)
names(windturbines)
windturbines$hight<-as.numeric(as.character(windturbines$hight))
summary(windturbines$hight)
is.na(windturbines$hight)==FALSE
windturbines<-subset(windturbines, is.na(hight)==FALSE)
windturbines<-subset(windturbines, hight>1)
# didvid hights into intervals
q5 <- classIntervals(windturbines$hight, n=5, style="quantile", dataPrecision =2)
q5
# choosing colors acording to intervals
q5Colours <- findColours(q5, heat.colors(5))
# plotting windturbines
plot(windturbines, axes=TRUE, col=q5Colours, pch=16, cex=1, add=TRUE)
# Add legend
legend("topleft",
fill = attr(q5Colours, "palette"),
border="black",
legend=gsub(",", " - ", names(attr(q5Colours, "table"))),
bg ="white",
title="Hight")
#########################################################################
### plotting northarrow
########################################################################
## Adding north arrow
north.arrow(580426,6337658, 1000, col='black', lab="")
#####################################################################
## exercise 1.3: play with your map
#####################################################################
# change the location of the north arrow
# change the colors of the map
# remove the border of the sea
# add habour to the map
# increase the size of the wind turbines dots
# change the dots to squares
# save the map in 300 dpi to the result folder folder
# in your the group find the best map and sent it to toke: tepp@ifro.ku.dk
#########################################################################
### saving the map to a file
########################################################################
# defining the working directory
setwd(results)
# defining the map
tiff(filename="example_1.tiff", width=1000,height=1000, bg=FALSE, res=300)
# creating the map
plot(Aalborg, col="grey80")
plot(municipality, col="grey40", density=10, add=TRUE)
# saving the map
dev.off()
# defining the map
png(filename="example_2.png", width=1000,height=1000, bg=FALSE, res=300)
# creating the map
plot(Aalborg, col="grey80")
plot(municipality, col="grey40", density=10, add=TRUE)
# saving the map
dev.off()
#####################################################################
## using aerial photos
#####################################################################
# geocide based in an address
e <- geocode('jyllandsvej 5,Aalborg, Denmark')
# changing the extent of geocoded area
e <- (e[5:8] + c(-0.151, 0.151, -0.151, 0.151))
# making the extent into a vector
e<-c(t(e))
# creating a new extent
e<-extent(e)
# creating a map
g <- gmap(e, type = "satellite")
# plotting the map
plot(g)
#####################################################################
## exercise 1.4: using aerial photos
#####################################################################
# exercise plot windturbines on the aerial photo and play with the extent of the aerial photo
# create a map. sent the best map in your group to toke: tepp@ifro.ku.dk
# hint: you may want to look into this function ?spTransform
#####################################################################
## finding the single male/female ratio accross the municipalities in Nordjylland
#####################################################################
setwd(data)
# reading in data
singles<-read.table("single1.csv", header=FALSE , sep=";")
# renaming columns
singles<-as.matrix(singles)
colnames(singles)<-c("","Name","single_male","single_female")
singles<-data.frame(singles)
# choosing relevant columns
singles<-singles[c("Name","single_male","single_female")]
#####################################################################
## exercise 1.5: create a map of the single male/female
## ratio accross the municipalities in Nordjylland
#####################################################################
# Hint this script to merge the shapefile with the single dataframe
municipality<-merge(municipality, singles, by.x=c(""), by.y=(""), all=FALSE)
##############################################################################
### End of scripts
##############################################################################