RNDr. Marie Forbelsk´a, Ph.D.
1
ˇen´ı : M6120cv03 (Z´ M6120 – 3. cvic aklady grafiky v R) ´ A. Urovnˇ e grafiky: High-level – je vhodn´ a pro tvorbu jednoduch´ ych graf˚ u, patˇr´ı sem funkce jako plot\verb, hist, dotplot atd. Low-level – umoˇzn ˇ uje mnohem v´ıce nastaven´ı jednotliv´ ych prvk˚ u grafu, dokreslov´an´ı r˚ uzn´ ych objekt˚ u do grafu (kruhy, text, body, matematick´e kˇrivky atd.) Interaktivn´ı grafika – dovoluje interaktivnˇe pˇrid´ avat data do grafu a zpˇetnˇe informace extrahovat.
Pomoc´ı pˇr´ıkazu demo(graphics) (popˇr. demo(image)) pod´ıvejte se na p´ ar pˇr´ıklad˚ u. V pˇr´ıpadˇe, ˇze jsou tyto grafy nedostaˇcuj´ıc´ı, existuje cel´a ˇrada dalˇs´ıch knihoven, kter´e obsahuj´ı velk´e mnoˇzstv´ı dalˇs´ıch typ˚ u graf˚ u (napˇr. bal´ıˇcky grid, lattice, iplots, misc3D, rgl, scatterplot3d. Existuje cel´a ˇrada bal´ıˇck˚ u pro pr´ aci s mapami, napˇr. sp, maps, maptools, rgdal, RgoogleMaps atd.
B.
Na adrese http://www.math.muni.cz/~kolacek/vyuka/vypsyst/navod_R.pdf. m´ ate k dispozici materi´ al, ve kter´em si prostudujte pˇredevˇs´ım 8. kapitolu.
C. Pˇ r´ıklady: ˇ´ıklad 1 Pr Popis dat je v souboru toxic.txt, samotn´a data jsou uloˇzena v souboru toxic.dat. Nejprve naˇcteme popisn´ y soubor pomoc´ı pˇr´ıkazu readLines. D´ıky tomu, ˇze je pˇr´ıkaz v z´avork´ach, ihned se zobraz´ı obsah promˇenn´e popis. > fileTxt <- paste(data.library, "toxic.txt", sep = "") > con <- file(fileTxt) > (popis <- readLines(con)) [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]
"Toxic Chemical Production Data" "==============================" "Artificial data record the volume of a toxic chemical " "that is produced as a by-product in a certain industrial" "manufacturing process." "" "The file toxic.dat contains these variables:" "" "VOL The volume of toxic by-product produced (in litres)" "TEMP The temperature of the manufacturing process (in^oC)" "CAT The weight of catalyst (in kg)" "METHOD The method used to produce the chemical (qualitative)"
> close(con)
2
Line´arn´ı statistick´e modely II (3. cviˇcen´ı) Nyn´ı naˇcteme datov´ y soubor toxic.dat pomoc´ı pˇr´ıkazu read.table. Pˇr´ıkazem str vyp´ıˇseme strukturu datov´eho r´ amce a nakonec i jeho obsah: > fileDat <- paste(data.library, "toxic.dat", sep = "") > data <- read.table(fileDat, header = TRUE) > str(data)
’data.frame’: 8 obs. of 4 variables: $ VOL : int 30 39 26 36 22 18 32 26 $ TEMP : int 90 85 70 80 80 85 90 85 $ CAT : num 1.5 1 1.5 2 1 2.5 1 2 $ METHOD: Factor w/ 2 levels "A","B": 1 1 2 1 2 2 1 2
> data
1 2 3 4 5 6 7 8
VOL TEMP CAT METHOD 30 90 1.5 A 39 85 1.0 A 26 70 1.5 B 36 80 2.0 A 22 80 1.0 B 18 85 2.5 B 32 90 1.0 A 26 85 2.0 B
Jazyk R m´ a k dispozici celou ˇradu graf˚ u, pomoc´ı kter´ ych lze data zn´ azornit. Vytvoˇr´ıme nejprve dva grafy vedle sebe (viz parametr mfrow=c(1,2)), a to vˇzdy pomoc´ı high-level pˇr´ıkazu plot. Kromˇe toho pouˇzijeme low-level pˇr´ıkazy points, grid a legend. > > > > > > > >
> > > > >
LA <- data$METHOD == "A" LB <- data$METHOD == "B" par(mfrow = c(1, 2)) plot(data$VOL ~ data$TEMP, type = "n", ylim = c(0, 50), xlab = "Temperature (in^oC)", ylab = "Volume of toxic by-product", main = "Toxic Chemical Production Data") points(data$TEMP[LA], data$VOL[LA], pch = 21, col = "darkred", bg = "red", cex = 1.5) points(data$TEMP[LB], data$VOL[LB], pch = 22, col = "darkgreen", bg = "green", cex = 1.5) grid(nx = NULL, ny = NULL, col = "grey", lty = 2) legend("bottomleft", paste("METHOD", c("A", "B")), col = c("darkred", "darkgreen"), pch = 21:22, ncol = 2, cex = 1, pt.bg = c("red", "green"), pt.cex = 1.5) plot(data$VOL ~ data$CAT, type = "n", ylim = c(0, 50), xlab = "The weight of catalyst (in kg)", ylab = "Volume of toxic by-product", main = popis[1]) points(data$CAT[LA], data$VOL[LA], pch = 21, col = "darkred", bg = "red", cex = 1.5) points(data$CAT[LB], data$VOL[LB], pch = 22, col = "darkgreen", bg = "green", cex = 1.5) grid(nx = NULL, ny = NULL, col = "grey", lty = 2) legend("bottomleft", paste("METHOD", c("A", "B")), col = c("darkred", "darkgreen"), pch = 21:22, ncol = 2, cex = 1, pt.bg = c("red", "green"), pt.cex = 1.5)
RNDr. Marie Forbelsk´a, Ph.D.
3
70
75
40 30 20
METHOD A
0
METHOD B
10
Volume of toxic by−product
40 30 20 10
METHOD A
0
Volume of toxic by−product
50
Toxic Chemical Production Data
50
Toxic Chemical Production Data
80
85
90
1.0
METHOD B 1.5
Temperature (in^oC)
2.0
The weight of catalyst (in kg)
Obr´azek 1: Dva bodov´e grafy vedle sebe pomoc´ı pˇr´ıkazu plot. Vˇsimnˇete si, jak´ y graf dostaneme, pouˇzijeme–li jednoduch´ y pˇr´ıkaz > plot(data)
75
80
85
90
1.0
1.4
1.8
30
35
70
85
90
20
25
VOL
2.0
2.5
70
75
80
TEMP
1.8
1.0
1.5
CAT
1.0
1.4
METHOD
20
25
30
35
1.0
1.5
2.0
2.5
Obr´azek 2: Maticov´e bodov´e grafy pomoc´ı pˇr´ıkazu plot.
2.5
4
Line´arn´ı statistick´e modely II (3. cviˇcen´ı) Dalˇs´ı moˇznost´ı je vykreslit maticov´ y bodov´ y graf pomoc´ı pˇr´ıkazu pairs. D´ıky volbˇe pch=21 (vyplnˇen´ y znak) a pˇredevˇs´ım s vyuˇzit´ım bg=as.numeric(data$METHOD) se n´ am podaˇrilo barevnˇe odliˇsit obˇe dvˇe metody.
> pairs(VOL ~ TEMP + CAT, data = data, pch = 21, bg = as.numeric(data$METHOD), cex = 1.5, main = popis[1])
Toxic Chemical Production Data 75
80
85
90
30
35
70
85
90
20
25
VOL
2.0
2.5
70
75
80
TEMP
1.0
1.5
CAT
20
25
30
35
1.0
1.5
2.0
2.5
Obr´azek 3: Maticov´e bodov´e grafy pomoc´ı pˇr´ıkazu pairs.
Pˇr´ıkaz plot lze pouˇz´ıt i k vykreslen´ı krabicov´ ych graf˚ u, a to d´ıky faktu, ˇze ˇze jedna z uvaˇzovan´ ych promˇenn´ ych je typu faktor.
> plot(data$VOL ~ data$METHOD, main = popis[1], horizontal = TRUE, ylab = substr(popis[9], 9, nchar(popis[9])), xlab = "Methods", col = "steelblue1")
RNDr. Marie Forbelsk´a, Ph.D.
5
A
Methods
B
Toxic Chemical Production Data
20
25
30
35
The volume of toxic by−product produced (in litres)
Obr´azek 4: Krabiˇckov´ y graf pro VOL dle metod pomoc´ı pˇr´ıkazu plot.
ˇ´ıklad 2 Pr Z˚ ustaneme u datov´eho souboru z pˇredeˇsl´eho pˇr´ıkladu. Uk´ aˇzeme si vykreslen´ı bodov´eho grafu pomoc´ı speci´ aln´ıch graf˚ u z knihovy lattice, kter´a obsahuje velmi sofistikovan´e grafy vhodn´e pro pr´ aci s datov´ ymi soubory, kter´e obsahuj´ı vedle spojit´ ych promˇenn´ ych tak´e celou ˇradu kategori´ aln´ıch promˇenn´ ych.
Vykresl´ıme nyn´ı bodov´ y graf pomoc´ı pˇr´ıkazu splom z knihovny lattice. Prostudujte si tento pˇr´ıkaz pomoc´ı n´ apovˇedy, tj. napˇr´ıklad pomoc´ı pˇr´ıkazu ?splom.
Chceme-li u tˇechto sofistikovan´ ych graf˚ u pˇridat legendu (d´ıky specifikac´ım uveden´ ych u parametru key), je tˇreba oˇsetˇrit, aby se tiskly stejn´e symboly ve stejn´ ych barv´ach jak v legendˇe, tak v jednotliv´ ych panelech (viz grafick´a syst´emov´a promˇenn´ a superpose.symbol).
> > > > > >
library(lattice) trellis.par.set(col.whitebg()) super.sym <- trellis.par.get("superpose.symbol") super.sym$pch <- c(21, 22) super.sym$cex <- rep(1.5, length(super.sym$pch)) graf <- splom(~data[1:3], groups = METHOD, data = data, pch = c(21, 22), cex = 1.5, bg = c(1, 2), panel = panel.superpose, as.table = T, key = list(title = popis[1], columns = 2, cex = 1.5, points = Rows(super.sym, 1:2), text = list(paste("METHOD", levels(data$METHOD))))) > print(graf)
6
Line´arn´ı statistick´e modely II (3. cviˇcen´ı)
Toxic Chemical Production Data METHOD A
METHOD B 2.5
2.0
2.5
2.0
CAT 1.5
1.0 90
80
85
1.5
1.0
90
85
TEMP
80
80
75 70 30
75
80
70
35
35 30
VOL 25
20
25
20
Scatter Plot Matrix
Obr´azek 5: Maticov´e bodov´e grafy pomoc´ı pˇr´ıkazu splom z knihovny lattice.
Velmi frekventovan´ ym typem grafu z knihovny lattice je graf vytvoˇren´ y pˇr´ıkazem xyplot. Uk´ aˇzeme jeho r˚ uzn´e vyuˇzit´ı.
Nejprve vytvoˇr´ıme graf, kter´ y pro kaˇzdou metodu vytvoˇr´ı samostatn´ y panel. Body (volba "p" u parametru type) nav´ıc proloˇz´ıme regregresn´ı pˇr´ımkou (volba "r" u parametru type). Volba "g" u parametru type znamen´a nastaven´ı parametru grid (mˇr´ıˇzka). D´ale si vˇsimnˇeme, ˇze n´ azev kaˇzd´eho panelu jsme rozˇs´ıˇrili o slovo Method (viz nastaven´ı parametr˚ u u volby strip). > library(lattice) > trellis.par.set(col.whitebg()) > graf <- xyplot(VOL ~ TEMP | METHOD, data = data, as.table = T, type = c("g", "p", "r"), main = popis[1], cex = 1.15, strip = strip.custom(factor.levels = paste("Method", levels(data$METHOD)))) > print(graf)
RNDr. Marie Forbelsk´a, Ph.D.
7
Toxic Chemical Production Data 70
Method A
75
80
85
90
Method B
VOL
35
30
25
20
70
75
80
85
90
TEMP
Obr´azek 6: Dvoupanelov´ y bodov´ y graf (spolu s proloˇzenou regresn´ı pˇr´ımkou) pomoc´ı pˇr´ıkazu xyplot z knihovny lattice.
Nyn´ı vytvoˇr´ıme graf pouze s jedn´ım panelem, ale obˇe metody rozliˇs´ıme graficky, a to tak, ˇze pˇrid´ ame volbu group=METHOD. Body (volba "p" u parametru type) opˇet proloˇz´ıme regregresn´ı pˇr´ımky (volba "r" u parametru type).
> > > > > >
library(lattice) trellis.par.set(col.whitebg()) super.sym <- trellis.par.get("superpose.symbol") super.sym$pch <- c(0, 1) super.sym$cex <- rep(1.15, length(super.sym$pch)) graf <- xyplot(VOL ~ TEMP, group = METHOD, data = data, type = c("g", "p", "r"), pch = c(0, 1), cex = 1.5, bg = c(1, 2), panel = panel.superpose, key = list(title = popis[1], columns = 2, cex = 1.15, points = Rows(super.sym, 1:2), text = list(paste("Method", levels(data$METHOD))))) > print(graf)
8
Line´arn´ı statistick´e modely II (3. cviˇcen´ı)
Toxic Chemical Production Data Method A
Method B
35
VOL
30
25
20
70
75
80
85
90
TEMP
Obr´azek 7: Jednopanelov´ y bodov´ y graf (spolu s proloˇzenou regresn´ı pˇr´ımkou) pomoc´ı pˇr´ıkazu xyplot z knihovny lattice. Dalˇs´ım zaj´ımav´ ym grafem je tzv. dotplot. Volba "a" (average) u parametru type zp˚ usob´ı, ˇze do grafu se zakresl´ı u ´ seˇcka spojuj´ıc´ı v´ ybˇerov´e pr˚ umˇery promˇenn´e VOL obou metod. > library(lattice) > graf <- dotplot(VOL ~ METHOD, data = data, type = c("p", "a"), main = popis[1]) > print(graf) Toxic Chemical Production Data
35
VOL
30
25
20
A
B
Obr´azek 8: Bodov´ y graf (spolu se spojnic´ı skupinov´ ych pr˚ umˇer˚ u) pomoc´ı pˇr´ıkazu dotplot z knihovny lattice.
RNDr. Marie Forbelsk´a, Ph.D.
9
Vzhledem k tomu, ˇze u metody B dva body spl´ yvaj´ı, provedeme jejich n´ ahodn´e posunut´ı (rozm´ıtnut´ı) pomoc´ı parametru jitter.x. > library(lattice) > graf <- dotplot(VOL ~ METHOD, data = data, type = c("p", "a"), main = popis[1], jitter.x = TRUE) > print(graf) Toxic Chemical Production Data
35
VOL
30
25
20
A
B
Obr´azek 9: Rozm´ıtnut´ y (jittered) bodov´ y graf (spolu se spojnic´ı skupinov´ ych pr˚ umˇer˚ u) pomoc´ı pˇr´ıkazu dotplot z knihovny lattice.
´ D. Ukol : Napiˇste pˇr´ıkazy v jazyku R tak, aby vznikl n´ asleduj´ıc´ı graf. Otazn´ıky nahrad’te skuteˇcn´ ymi hodnotami. N´avod: text t´ ykaj´ıc´ı se procent doplˇ nte pomoc´ı pˇr´ıkazu mtext.
0.0
0.1
0.2
0.3
0.4
Hustota N(0,1)
?%
−4
?%
?%
−2
?%
?%
0
?%
?%
2
?%
4
Obr´azek 10: Hustota standardizovan´eho norm´aln´ıho rozdˇelen´ı