Graphics examplesGraphics
Graphics
Thomas Källman (adopted from slides by M. Kierczak
10/4/2017
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Graphics examples
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
World map with data overlayed
−150 −100 −50 0 50 100 150
−15
0−
100
−50
050
100
150
2
4
6
8
Figure 1: Climate stability (From: M. Pettersson, UU)Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Voronoi tesselations on maps
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●●
●
●
●
●
● ●
●●
●
●
●●
●
●
●●
●
●●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
● ●●
●
●
●
●
●●
●
●
●
● ●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●●
●
●●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●●
●
●●
●
●
●
●●
●
●●
●
●
●
●●
●
●●
●
●●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
Figure 2: Airport coverage poland
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Faces of asia
Figure 3: Use figures to represent data
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Gapminder type of plots
Figure 4: Economy vs expected life spanThomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Circos plots
Figure 5: Human genome with metadataThomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Graphics
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Graphical devices in R
The concept of a graphical device is crucial for understanding Rgraphics. A device can be a screen (default) or a file. Some Rpackages introduce their own devices, e.g. Cairo device.
Create plots:
opening a graphical device (not necessary for plotting onscreen)plotting to the graphical deviceclosing the graphical device (very important!)
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
The most common devices
screenbitmap/raster devices: png(), bmp(), tiff(), jpeg()vector devices: svg(), pdf(),Cairo versions of the above devices – for Windows users theyoffer higher quality graphics
for more information visithttp://stat.ethz.ch/R-manual/Rdevel/library/grDevices/html/Devices.html
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Plotting on screen
plot(x=c(1,2,7), y=c(2,3,5))
1 2 3 4 5 6 7
2.0
3.0
4.0
5.0
c(1, 2, 7)
c(2,
3, 5
)
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Plotting to file
png(filename = 'myplot.png',width = 320,height = 320,antialias = TRUE)
plot(x=c(1,2,7), y=c(2,3,5))dev.off()
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Base R graphics viewport
Figure 6: R plotting areasThomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Changing parameters
For convience we will create a data frame to hold our data
df <- data.frame(x = c(1,2,7),y = c(2,3,5),row.names = c("A","B","C"))
df
## x y## A 1 2## B 2 3## C 7 5
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Changing parameters
plot(df, pch=c(15,17,19),col=c("tomato", "slateblue"),main="Three points")
1 2 3 4 5 6 7
2.0
3.0
4.0
5.0
Three points
x
y
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Plotting altering defaults
There is many parameters one can set in plot().
pch – type of the plotting symbolcol – color of the pointscex – scale for pointsmain – main title of the plotsub – subtitle of the plotxlab – X-axis labelylab – Y-axis labellas – axis labels orientationcex.axis – axis lables scale
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Changing parametersGraphical parameters can be set in two different ways:
As plotting function arguments, e.g. plot(dat, cex=0.5)using the function par().
# read current graphical parameterspar()# first, save the current parameters so that you# can set them back if neededopar <- par() # should work in theory, practise varies :-(# now, modify what you wantpar(cex.axis = 0.8, bg='grey')# do your plottingplot(................)# restore the old parameters if you wantpar(opar)
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
pch - example
# create a grid of coordinatescoords <- expand.grid(1:6,1:6)# make a vector of numerical pch symbolspch.num <- c(0:25)# and a vector of character pch symbolspch.symb <- c('*', '.', 'o', 'O', '0', '-', '+', '|', '%', '#')# plot numerical pchplot(coords[1:26,1], coords[1:26,2], pch=pch.num,bty='n', xaxt='n', yaxt='n', bg='red',xlab='', ylab='')# and character pch'spoints(coords[27:36,1], coords[27:36,2], pch=pch.symb)# label themtext(coords[,1], coords[,2], c(1:26, pch.symb), pos = 1,col='slateblue', cex=.8)
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
pch - example
* o O
0 − + | % #
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 * . o O
0 − + | % #
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Visualising graph parameters
0 5 10 15 20
1
p
1
2
l
2
3 4
b
3
5 6
o
4
pch
col
cex
lty
type
lwd
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Graphic layersElements are added to a plot in the same order you plot them. It islike layers in a graphical program. Think about it! For instance theauxiliary grid lines should be plotted before the actual data points.
# make an empty plotplot(1:5, type='n', las=1,bty='n')# plot gridgrid(col='grey', lty=3)# plot the actual datapoints(1:5, pch=19, cex=3)# plot a lineabline(h = 3.1, col='red')# you see, it overlaps the# data point. It is better# to plot it before points()Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Graphic layers
1 2 3 4 5
1
2
3
4
5
Index
1:5
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
General considerations
raster or vector graphics,colors, e.g. color-blind people, warm vs. cool colors and opticalillusionsavoid complications, e.g. 3D plots, pie charts etcuse black ang greys for things you do not need to emphasize,i.e. basically everything except your main result,avoid 3 axis
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Multiple plots one figure
par(mfrow=c(2,3))plot(1:5)plot(1:5, pch=19, col='red')plot(1:5, pch=15, col='blue')hist(rnorm(100, mean = 0, sd=1))hist(rnorm(100, mean = 7, sd=3))hist(rnorm(100, mean = 1, sd=0.5))par(mfrow=c(1,1))
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Multiple plots one figure
1 2 3 4 5
12
34
5
Index
1:5
1 2 3 4 5
12
34
5Index
1:5
1 2 3 4 5
12
34
5
Index
1:5
Histogram of rnorm(100, mean = 0, sd = 1)
rnorm(100, mean = 0, sd = 1)
Fre
quen
cy
−2 −1 0 1 2 3
05
1015
2025
Histogram of rnorm(100, mean = 7, sd = 3)
rnorm(100, mean = 7, sd = 3)
Fre
quen
cy
0 5 10 15
05
1015
2025
Histogram of rnorm(100, mean = 1, sd = 0.5)
rnorm(100, mean = 1, sd = 0.5)
Fre
quen
cy0.0 0.5 1.0 1.5 2.0
05
1015
20
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Multiple plots one figure
If more complex compositions the layout function can be used
layout(matrix(c(1, 2, 2, 2, 3, 2, 2, 2), nrow = 2,ncol = 4, byrow = T))
plot(1:5, pch = 15, col = "blue")hist(rnorm(100, mean = 0, sd = 1))plot(1:5, pch = 15, col = "red")
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Multiple plots one figure
1 3 5
12
34
5
Index
1:5
Histogram of rnorm(100, mean = 0, sd = 1)
rnorm(100, mean = 0, sd = 1)
Fre
quen
cy
−3 −2 −1 0 1 2
05
1015
20
1 3 5
12
34
5
Index
1:5
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Colors
mycol <- c(rgb(0, 0, 1), "olivedrab", "#FF0000")plot(1:3, c(1, 1, 1), col = mycol, pch = 19, cex = 3)points(2, 1, col = rgb(0, 0, 1, 0.5), pch = 15, cex = 5)
1.0 1.5 2.0 2.5 3.0
0.6
0.8
1.0
1.2
1.4
1:3
c(1,
1, 1
)
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Colors
There are built-in color palettes
mypal <- heat.colors(10)mypal
## [1] "#FF0000FF" "#FF2400FF" "#FF4900FF" "#FF6D00FF" "#FF9200FF"## [6] "#FFB600FF" "#FFDB00FF" "#FFFF00FF" "#FFFF40FF" "#FFFFBFFF"
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Colors
pie(x = rep(1, times = 10), col = mypal)
1
23
4
5
6
78
9
10
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Colors
mypal <- colorRampPalette(c("red", "green", "blue"))pie(x = rep(1, times = 12), col = mypal(12))
1
234
5
6
7
89 10
11
12
Note that cololRampPalette returns a functionThomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Colors
library(RColorBrewer)display.brewer.all()
BrBGPiYG
PRGnPuOrRdBuRdGy
RdYlBuRdYlGnSpectral
AccentDark2Paired
Pastel1Pastel2
Set1Set2Set3
BluesBuGnBuPuGnBu
GreensGreys
OrangesOrRdPuBu
PuBuGnPuRd
PurplesRdPuRedsYlGn
YlGnBuYlOrBrYlOrRd
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Histogram
x <- rnorm(1000) # generate sample from normal dist.hist(x)
Histogram of x
x
Fre
quen
cy
−4 −2 0 2
050
100
150
200
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Histogram
hist(x, breaks = 100)
Histogram of x
x
Fre
quen
cy
−4 −2 0 2
010
2030
40
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Boxplot
boxplot(x)
−4
−2
02
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Hist. vs Box
Histogram of x
x
Fre
quen
cy
−4 −2 0 2
050
100
200
−4 −2 0 2
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Violinplot
library(vioplot)
## Loading required package: sm
## Package 'sm', version 2.2-5.4: type help(sm) for summary information
vioplot(x)
−4
−2
02
1
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Plotting categorical data
library(vcd)data(Titanic) # Load the datamosaic(Titanic,
labeling=labeling_border(rot_labels = c(0,0,0,0)))
Sex
Survived
Cla
ss
Age
Crew
No Yes
Adult
NoYes
Child
3rd Adult
Child
2nd AdultChild
1st
Male Female
AdultChild
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
Heatmaps
heatmap(matrix(rnorm(100, mean = 0, sd = 1), nrow = 10),col=terrain.colors(10))
8 9 1 2 3 10 5 4 7 610694732185
Thomas Källman (adopted from slides by M. Kierczak Graphics
Graphics examplesGraphics
More inspiration
http://www.r-graph-gallery.com
Thomas Källman (adopted from slides by M. Kierczak Graphics