basic plots in r · 2 overviewofbasegraphics 3 histograms 4 barplots 5 boxplots 6 scatterplots...
TRANSCRIPT
Basic plots in R
****************Osama Mahmoud
***Website: http://osmahmoud.comE-mail: [email protected]
*****14 November 2019
Goals
In this session, we aim to cover:what data visualisation is.why we need visualisation.what the base graphics and their types are.How to produce base plots.How to save your generated graphics.
Osama Mahmoud Session 03: Basic plots in R 1 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Contents
1 Introduction to data visualisation
2 Overview of base graphics
3 Histograms
4 Bar plots
5 Box plots
6 Scatter plots
Osama Mahmoud Session 03: Basic plots in R 2 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Graphics reveal data
“The greatest value of a picture is when it forces us to notice what wenever expected to see.”
JOHN W. TUKEY (1915 - 2000)
Osama Mahmoud Session 03: Basic plots in R 3 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Why do we need visualisations?
Osama Mahmoud Session 03: Basic plots in R 4 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Why do we need visualisations?
Osama Mahmoud Session 03: Basic plots in R 5 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
THE UGLY
Osama Mahmoud Session 03: Basic plots in R 6 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
The ugly
Osama Mahmoud Session 03: Basic plots in R 7 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
The ugly
Osama Mahmoud Session 03: Basic plots in R 8 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
FEW ISSUES
Osama Mahmoud Session 03: Basic plots in R 9 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Few issues
Human perception can be ...
Osama Mahmoud Session 03: Basic plots in R 10 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Few issues
Human perception can be deceiving
Osama Mahmoud Session 03: Basic plots in R 11 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Few issues
Another example:
Osama Mahmoud Session 03: Basic plots in R 12 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Few issues
Another example:
Osama Mahmoud Session 03: Basic plots in R 13 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Few issues
Selective attention
Selective attention test
Osama Mahmoud Session 03: Basic plots in R 14 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Types of R graphics
Base graphics.Grid graphics.Lattice graphics.ggplot2 graphics.
Osama Mahmoud Session 03: Basic plots in R 15 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Base graphics
Osama Mahmoud Session 03: Basic plots in R 16 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Installing course R package: BristolVis
> install.packages("drat")
> drat::addRepo("statcourses")
> install.packages("BristolVis")
Osama Mahmoud Session 03: Basic plots in R 17 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Overview of base graphics
Graphics can be easily created in R and subsequently included inWord, Power Point, LATEX, etc.Let’s plot a simple graphic:
> plot(rate ~ conc, data = Puromycin)This implicitly opens a new graphics window.
0.0 0.2 0.4 0.6 0.8 1.0
5010
015
020
0
conc
rate
Osama Mahmoud Session 03: Basic plots in R 18 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Overview of base graphics
Change settings of active graphic window> par(mfrow = c(1,2), mar = c(4,4,0,0) + 0.1)> x <- seq(-3,3, 0.1)> plot(x, x^2, type = ’l’)> plot(x, x^3, type = ’l’)
−3 −2 −1 0 1 2 3
02
46
8
x
x^2
−3 −2 −1 0 1 2 3
−20
−10
010
20
x
x^3
Osama Mahmoud Session 03: Basic plots in R 19 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Overview of base graphics
Close active graphics window:
> dev.off()
Close all graphics windows:> graphics.off()
Osama Mahmoud Session 03: Basic plots in R 20 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Histograms
Osama Mahmoud Session 03: Basic plots in R 21 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Histograms
Simple histogram (next slide left)
> data(birthweight, package = “BristolVis”)> hist(birthweight$weightgain)
with a fixed set of “bars”: (breaks give number of cut-points)
> hist(birthweight$weightgain, breaks = 3)with a defined scale for the y-axis:
> hist(birthweight$weightgain, breaks = 3, ylim = c(0,10))with colored bars:
> hist(birthweight$weightgain, breaks = 3, ylim = c(0,10), col = gray(4:7 / 7))
with arbitrary text, vertical and double text size (next slide right)
> text(1500, 5, "some text", srt = 90, cex = 2)
Osama Mahmoud Session 03: Basic plots in R 22 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Histograms
Histogram of birthweight$weightgain
birthweight$weightgain
Fre
qu
en
cy
500 1000 1500 2000 2500 3000 3500
01
23
45
Histogram of the weight gain
WG
Fre
qu
en
cy
0 1000 2000 3000 4000
02
46
81
0
so
me
text
Osama Mahmoud Session 03: Basic plots in R 23 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Bar plots
Osama Mahmoud Session 03: Basic plots in R 24 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Bar plots
Simple bar plot (next slide left)
> data(med, package = “BristolVis”)> treatment = table(med$treatment)> barplot(treatment, ylim = c(0, 500))
stacked bar plot:
> health_treat = table(med$health, med$treatment)> barplot(health_treat, ylim = c(0, 500))
with juxtaposed bars:barplot(health_treat, ylim = c(0, 300), beside = TRUE)
with legend (next slide right)
> barplot(health_treat, ylim = c(0, 300), beside = TRUE,legend.text = rownames(health_treat))
Osama Mahmoud Session 03: Basic plots in R 25 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Bar plots
Existing New
01
00
20
03
00
40
05
00
Existing New
Poor
Fair
Good
nu
mb
er
of
pa
tie
nts
05
01
00
15
02
00
25
03
00
Osama Mahmoud Session 03: Basic plots in R 26 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Box plots
Osama Mahmoud Session 03: Basic plots in R 27 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Box plots
Simple box plot (next slide left)
> boxplot(birthweight$weight)
grouped by sex:
> boxplot(weight ~ sex, data = birthweight)
with new labels for sex, main title and a defined y-scale (next slide right)
> boxplot(weight ~ sex, data = birthweight, names =c("girls", "boys"), ylab = "birth weight [g]", ylim =c(2000, 4000), main = "Distribution of birth weight")
with additional line:> abline(h = 3000, col = "gray", lty = 4)
Osama Mahmoud Session 03: Basic plots in R 28 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Box plots2
40
02
60
02
80
03
00
03
20
03
40
03
60
0
girls boys
20
00
25
00
30
00
35
00
40
00
Distribution of birth weight
bir
th w
eig
ht
[g]
Osama Mahmoud Session 03: Basic plots in R 29 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Scatter plots
Osama Mahmoud Session 03: Basic plots in R 30 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Scatter plots
Simple scatter plot (next slide left)
> plot(weightgain ~ weight, data = birthweight)
for a subset:
> plot(weightgain ~ weight, data = birthweight, subset =(sex == "F"))
with colored solid points by sex and add a legend (next slide right)
> plot(weightgain ~ weight, data = birthweight, subset =(sex == "M"), col = "blue", pch = 20)> points(weightgain ~ weight, data = birthweight, subset =(sex == "F"), col = "red", pch = 20) # add girls data> legend("bottomleft", title = "sex", legend = c("girls","boys"), col = c("red","blue"), pch = 20) # add legend
Osama Mahmoud Session 03: Basic plots in R 31 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Scatter plots
2400 2600 2800 3000 3200 3400 3600
10
00
15
00
20
00
25
00
30
00
weight
we
igh
tga
in
3000 3200 3400 3600
15
00
20
00
25
00
30
00
Weight gain (for boys and girls)
weight [g]
we
igh
t g
ain
(g
)
sex
girls
boys
Osama Mahmoud Session 03: Basic plots in R 32 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Examples of the graphic library
An overview of available demos for the built-in R library (graphics):> demo(graphics)
Osama Mahmoud Session 03: Basic plots in R 33 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Saving plots
Osama Mahmoud Session 03: Basic plots in R 34 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Saving plots
Graphics can be saved directly from within R using different devices,e.g. postscript(), pdf(), bmp(), (see ?Devices)For publications vector graphics such as PDFs or postscript files arepreferable to pixel graphics such as jpg, bmp, etc.
> pdf("Fig1.pdf", width = 10, height = 7)> boxplot(weight ~ sex, data = birthweight)> dev.off()
Osama Mahmoud Session 03: Basic plots in R 35 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Useful links
Choice of graphic coloursNames of colours in various formats:http://colorbrewer2.org/
Web-page of the courseThe BristolVis tool for learners of data visualisation using R:https://github.com/statcourses/BristolVis
Osama Mahmoud Session 03: Basic plots in R 36 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
References
888
Osama Mahmoud Session 03: Basic plots in R 37 / 38
Base graphics Introduction Base graphics Histograms Bar plots Box plots Scatter
Thank You
Osama Mahmoud Session 03: Basic plots in R 38 / 38