# 4 Plotting

Every research paper or presentation should include some nice visualisations. R is a powerful software for plotting, especially the ggplot2 package. Ggplot2 is based on the book The Grammer of Graphics by Leland Wilkinson.

I will teach you first the ggplot2 notations and then the base plots.

## 4.1 ggplot2

First, you must understand the layer structure of a ggplot. Each ggplot can have a maximum of 7 layers. The first three layers are necessary to produce a plot! So to get a plot you must specify the following layers:

• Data: Where is the data you want to plot?
• Aesthetics: What should be on the x and y axis?
• Geometries: Do you want to have lines or points? Figure 4.1: The 7 layers of a ggplot

### 4.1.1 Layer 1-3: Data, aesthetics & geometries

Lets give it a try by only specifing the first 3 layers.

``````# install.packages("ggplot2")
require(ggplot2)
# get some data
df <- iris

p1<-ggplot(data=df,aes(x=Sepal.Length,y=Sepal.Width ))+
geom_point()
p1`````` Now we want to add a color for each species to the plot. Iris data contains four different species.

``````p2<-ggplot(data=df,aes(x=Sepal.Length,y=Sepal.Width,col=Species))+
geom_point()

p2`````` It is important that the colour (col) argument is within the aesthetics bracket! Otherwise, R does not choose the colour based on the Species.

### 4.1.2 Layer 7: Themes

The theme defines how your plot will look like (e.g. background, axis, ect.). Making an own theme can take quite a while - the ggthemes package is easy to use and provides several themes that you can use. Simply add to your existing plot the theme.

``````# install.packages("ggthemes")
require(ggthemes)``````
``## Loading required package: ggthemes``
``p2 +theme_dark()`` Here are some of my own themes that you might like:

``````theme_Rclass <- theme(panel.border = element_rect(colour = "black", fill=NA, size=2),
panel.background = element_rect(fill = "white"),
panel.grid.major = element_line(colour="grey90"),
text = element_text(size=15),
axis.text=element_text(size=15),
axis.title=element_text(size=15,face="bold"),
legend.text=element_text(size=10))

#Bloomberg theme
theme_Bloomberg <-   theme(plot.background=element_rect(fill="#000000"),
panel.background=element_rect(fill="grey75"),
panel.grid.minor=element_line(linetype=3,size=0.1),
panel.grid.major=element_line(linetype=3,size=0.1),
panel.border = element_rect(colour = "white", fill=NA, size=0.5),
plot.title = element_text(colour = "white",size=15,face="bold"),
axis.text=element_text(size=15, colour="white"),
axis.title=element_text(size=15,face="bold",colour="white"),
legend.text=element_text(size=10))``````
``p2 + theme_Bloomberg`` ## 4.3 Spatial data

Spatial data - also known as geospatial data - is data that contains geographic information (e.g. coordinates). It is well known, that location strongly affects real estate prices. Houses close to the lake of Zurich are on average more expensive than a compareable house not located at the lake. Hence, if you want to analyse house prices, you will be often encountered with spatial information. Other examples of spatial information are crime rates, amenities, weather information, trading flows and financial networks.

In this section I will teach you how to visualise spatial information. First, you will need a shapefile of the region you are interested in and you want to plot. You can get the shapefiles for most countries in the world from this webpage. In R you can also access the information with the getData function in the raster package. More information on shapefiles and visualisations can be found here.

### 4.3.1 Simple base plot

First, we get the shapefile: In the getData function the level argument specifies the level of administrative subdivision (level=0: only the country, level=1: cantons,level=3: municipalities).

``````# you will need the following package
require(raster)

# download the map of switerland: level 3 means that you want to have all   # municipalities.

You can plot the shapefile with the plot function.

``````# now you can just plot the map easily:
plot(CH,col="darkgrey")`````` Assuming that you have spatial data, you might want to visualise it in the shapefile. Hence, you must know how a shapefile is structured. The shapefile contains a lot of information (e.g. name of the municipal, country name, coordinates, ect.)

``head(CH\$NAME_3)``
``````##  "Aarau"      "Biberstein" "Buchs"      "Densbüren"  "Erlinsbach"
##  "Gränichen"``````
``````setwd("C:/Users/lliebi/Dropbox/Daten Luca/PhD/GSERM/Spatial Data/Regression Analysis for Spatial Data")

require(maptools)
require(spdep)
require(rgdal)
require(leaflet)
require(leaflet.extras)
require(mapview)
require(leaflet)
require(spatialreg)
library(expss)
require(sp)
library(GISTools)
require(maps)
library(xtable)
require(stargazer)
require(DT)

plot(CH)`````` ### 4.3.2 Interactive spatial plots

``````library(rgdal)
library(leaflet)
library(leaflet.extras)
library(mapview)

setwd("C:/Users/lliebi/Dropbox/Daten Luca/PhD/GSERM/Spatial Data/Regression Analysis for Spatial Data")
# the simple map above was plottet on a generic white background so we didnt have to
# deal with a coordinate reference system (CRS). If we would like to have a geographic map
# as background we need to tranform the CRS into the usual longitude-latitude CRS:
CH_map <- rgdal::readOGR(dsn = ".", "MS_Gebiete")``````
``````## OGR data source with driver: ESRI Shapefile
## Source: "C:\Users\lliebi\Dropbox\Daten Luca\PhD\GSERM\Spatial Data\Regression Analysis for Spatial Data", layer: "MS_Gebiete"
## with 106 features
## It has 4 fields
## Integer64 fields read as strings:  ge11_ms_id``````
``````CH_map <- spTransform(CH_map, CRS("+proj=longlat +datum=WGS84"))

col.tmp <- rev(heat.colors(106, alpha = NULL))
col.tmp2 <- col.tmp[rank(dat1\$hp)]

m <- leaflet(data = CH_map)
m <- addPolygons(m, fillColor = col.tmp2, stroke = TRUE, weight = 1,
fillOpacity = 0.7, color = "black")
m``````
``````# read the data
setwd("C:/Users/lliebi/Dropbox/Daten Luca/PhD/GSERM/Spatial Data/Regression Analysis for Spatial Data")