0% found this document useful (0 votes)
20 views51 pages

Tugas Pertemuan 8 - Visualisasi Data

Uploaded by

Andi Isna Yunita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views51 pages

Tugas Pertemuan 8 - Visualisasi Data

Uploaded by

Andi Isna Yunita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Tugas Pertemuan 8

Kumputasi Statistika Lanjut

VISUALISASI DATA

Oleh :

Andi Isna Yunita


H062202005

MAGISTER STATISTIKA
FAKULTAS MATEMATIKA DAN ILMU PENGETAHUAN ALAM
UNIVERSITAS HASANUDDIN
MAKASSAR

2021
VISUALISASI DATA

1. Pendahuluan
library(lattice)

## Warning: package 'lattice' was built under R version 3.6.3

demo()
demo(graphics)

##
##
## demo(graphics)
## ---- ~~~~~~~~
##
## > # Copyright (C) 1997-2009 The R Core Team
## >
## > require(datasets)
##
## > require(grDevices); require(graphics)
##
## > ## Here is some code which illustrates some of the differences betwee
n
## > ## R and S graphics capabilities. Note that colors are generally spe
cified
## > ## by a character string name (taken from the X11 rgb.txt file) and t
hat line
## > ## textures are given similarly. The parameter "bg" sets the backgro
und
## > ## parameter for the plot and there is also an "fg" parameter which s
ets
## > ## the foreground color.
## >
## >
## > x <- stats::rnorm(50)
##
## > opar <- par(bg = "white")
##
## > plot(x, ann = FALSE, type = "n")
##
## > abline(h = 0, col = gray(.90))
##
## > lines(x, col = "green4", lty = "dotted")
##
## > points(x, bg = "limegreen", pch = 21)
##
## > title(main = "Simple Use of Color In a Plot",
## + xlab = "Just a Whisper of a Label",
## + col.main = "blue", col.lab = gray(.8),
## + cex.main = 1.2, cex.lab = 1.0, font.main = 4, font.lab = 3)
##
## > ## A little color wheel. This code just plots equally spaced hues
in
## > ## a pie chart. If you have a cheap SVGA monitor (like me) you wil
l
## > ## probably find that numerically equispaced does not mean visually
## > ## equispaced. On my display at home, these colors tend to cluster a
t
## > ## the RGB primaries. On the other hand on the SGI Indy at work the
## > ## effect is near perfect.
## >
## > par(bg = "gray")
##
## > pie(rep(1,24), col = rainbow(24), radius = 0.9)

##
## > title(main = "A Sample Color Wheel", cex.main = 1.4, font.main = 3)
##
## > title(xlab = "(Use this as a test of monitor linearity)",
## + cex.lab = 0.8, font.lab = 3)
##
## > ## We have already confessed to having these. This is just showing o
ff X11
## > ## color names (and the example (from the postscript manual) is prett
y "cute".
## >
## > pie.sales <- c(0.12, 0.3, 0.26, 0.16, 0.04, 0.12)
##
## > names(pie.sales) <- c("Blueberry", "Cherry",
## + "Apple", "Boston Cream", "Other", "Vanilla Cream")
##
## > pie(pie.sales,
## + col = c("purple", "violetred1", "green3", "cornsilk", "cyan",
"white"))
##
## > title(main = "January Pie Sales", cex.main = 1.8, font.main = 1)
##
## > title(xlab="(Don't try this at home kids)", cex.lab=0.8, font.lab=3)
##
## > ## Boxplots: I couldn't resist the capability for filling the "box".
## > ## The use of color seems like a useful addition, it focuses attentio
n
## > ## on the central bulk of the data.
## >
## > par(bg = "cornsilk")
##
## > n <- 10
##
## > g <- gl(n, 100, n*100)
##
## > x <- rnorm(n*100) + sqrt(as.numeric(g))
##
## > boxplot(split(x,g), col = "lavender", notch = TRUE)
##
## > title(main="Notched Boxplots", xlab="Group", font.main=4, font.lab=1)
##
## > ## An example showing how to fill between curves.
## >
## > par(bg = "white")
##
## > n <- 100
##
## > x <- c(0, cumsum(rnorm(n)))
##
## > y <- c(0, cumsum(rnorm(n)))
##
## > xx <- c(0:n, n:0)
##
## > yy <- c(x, rev(y))
##
## > plot(xx, yy, type = "n", xlab = "Time", ylab = "Distance")
##
## > polygon(xx, yy, col = "gray")
##
## > title("Distance Between Brownian Motions")
##
## > ## Colored plot margins, axis labels and titles. You do need to be
## > ## careful with these kinds of effects. It's easy to go completely
## > ## over the top and you can end up with your lunch all over the keybo
ard.
## > ## On the other hand, my market research clients love it.
## >
## > x <- c(0.00, 0.40, 0.86, 0.85, 0.69, 0.48, 0.54, 1.09, 1.11, 1.73, 2.
05, 2.02)
##
## > par(bg = "lightgray")
##
## > plot(x, type = "n", axes = FALSE, ann = FALSE)
##
## > usr <- par("usr")
##
## > rect(usr[1], usr[3], usr[2], usr[4], col="cornsilk", border="black")
##
## > lines(x, col = "blue")
##
## > points(x, pch = 21, bg = "lightcyan", cex = 1.25)
##
## > axis(2, col.axis = "blue", las = 1)
##
## > axis(1, at = 1:12, lab = month.abb, col.axis = "blue")
##
## > box()
##
## > title(main="The Level of Interest in R", font.main=4, col.main="red")
##
## > title(xlab = "1996", col.lab = "red")
##
## > ## A filled histogram, showing how to change the font used for the
## > ## main title without changing the other annotation.
## >
## > par(bg = "cornsilk")
##
## > x <- rnorm(1000)
##
## > hist(x, xlim = range(-4, 4, x), col = "lavender", main ="")

##
## > title(main = "1000 Normal Random Variates", font.main = 3)
##
## > ## A scatterplot matrix
## > ## The good old Iris data (yet again)
## >
## > pairs(iris[1:4], main = "Edgar Anderson's Iris Data", font.main = 4,
pch = 19)
##
## > pairs(iris[1:4], main = "Edgar Anderson's Iris Data", pch = 21,
## + bg = c("red", "green3", "blue")[unclass(iris$Species)])
##
## > ## Contour plotting
## > ## This produces a topographic map of one of Auckland's many volcanic
"peaks".
## >
## > x <- 10*1:nrow(volcano)
##
## > y <- 10*1:ncol(volcano)
##
## > lev <- pretty(range(volcano), 10)
##
## > par(bg = "lightcyan")
##
## > pin <- par("pin")
##
## > xdelta <- diff(range(x))
##
## > ydelta <- diff(range(y))
##
## > xscale <- pin[1]/xdelta
##
## > yscale <- pin[2]/ydelta
##
## > scale <- min(xscale, yscale)
##
## > xadd <- 0.5*(pin[1]/scale - xdelta)
##
## > yadd <- 0.5*(pin[2]/scale - ydelta)
##
## > plot(numeric(0), numeric(0),
## + xlim = range(x)+c(-1,1)*xadd, ylim = range(y)+c(-1,1)*yadd,
## + type = "n", ann = FALSE)
##
## > usr <- par("usr")
##
## > rect(usr[1], usr[3], usr[2], usr[4], col = "green3")
##
## > contour(x, y, volcano, levels = lev, col = "yellow", lty = "solid", a
dd = TRUE)
##
## > box()
##
## > title("A Topographic Map of Maunga Whau", font= 4)
##
## > title(xlab = "Meters North", ylab = "Meters West", font= 3)
##
## > mtext("10 Meter Contour Spacing", side = 3, line = 0.35, outer =
## + FALSE, at = mean(par("usr")[1:2]), cex = 0.7, font = 3)
##
## > ## Conditioning plots
## >
## > par(bg = "cornsilk")
##
## > coplot(lat ~ long | depth, data = quakes, pch = 21, bg = "green3")
##
## > par(opar)

demo(image)

##
##
## demo(image)
## ---- ~~~~~
##
## > # Copyright (C) 1997-2009 The R Core Team
## >
## > require(datasets)
##
## > require(grDevices); require(graphics)
##
## > x <- 10*(1:nrow(volcano)); x.at <- seq(100, 800, by = 100)
##
## > y <- 10*(1:ncol(volcano)); y.at <- seq(100, 600, by = 100)
##
## > # Using Terrain Colors
## >
## > image(x, y, volcano, col = terrain.colors(100), axes = FALSE)
##
## > contour(x, y, volcano, levels = seq(90, 200, by = 5), add = TRUE, co
l = "brown")
##
## > axis(1, at = x.at)
##
## > axis(2, at = y.at)
##
## > box()
##
## > title(main = "Maunga Whau Volcano", sub = "col=terrain.colors(100)",
font.main = 4)
##
## > # Using Heat Colors
## >
## > image(x, y, volcano, col = heat.colors(100), axes = FALSE)
##
## > contour(x, y, volcano, levels = seq(90, 200, by = 5), add = TRUE, co
l = "brown")
##
## > axis(1, at = x.at)
##
## > axis(2, at = y.at)
##
## > box()
##
## > title(main = "Maunga Whau Volcano", sub = "col=heat.colors(100)", fon
t.main = 4)
##
## > # Using Gray Scale
## >
## > image(x, y, volcano, col = gray(100:200/200), axes = FALSE)
##
## > contour(x, y, volcano, levels = seq(90, 200, by = 5), add = TRUE, co
l = "black")
##
## > axis(1, at = x.at)
##
## > axis(2, at = y.at)
##
## > box()
##
## > title(main = "Maunga Whau Volcano \n col = gray(100:200/200)", font.m
ain = 4)
##
## > ## Filled Contours are even nicer sometimes :
## > example(filled.contour)
##
## flld.c> require("grDevices") # for colours
##
## flld.c> filled.contour(volcano, asp = 1) # simple
##
## flld.c> x <- 10*1:nrow(volcano)
##
## flld.c> y <- 10*1:ncol(volcano)
##
## flld.c> filled.contour(x, y, volcano, color = function(n) hcl.colors(n,
"terrain"),
## flld.c+ plot.title = title(main = "The Topography of Maunga Whau",
## flld.c+ xlab = "Meters North", ylab = "Meters West"),
## flld.c+ plot.axes = { axis(1, seq(100, 800, by = 100))
## flld.c+ axis(2, seq(100, 600, by = 100)) },
## flld.c+ key.title = title(main = "Height\n(meters)"),
## flld.c+ key.axes = axis(4, seq(90, 190, by = 10))) # maybe also as
p = 1
##
## flld.c> mtext(paste("filled.contour(.) from", R.version.string),
## flld.c+ side = 1, line = 4, adj = 1, cex = .66)
##
## flld.c> # Annotating a filled contour plot
## flld.c> a <- expand.grid(1:20, 1:20)
##
## flld.c> b <- matrix(a[,1] + a[,2], 20)
##
## flld.c> filled.contour(x = 1:20, y = 1:20, z = b,
## flld.c+ plot.axes = { axis(1); axis(2); points(10, 10) }
)
##
## flld.c> ## Persian Rug Art:
## flld.c> x <- y <- seq(-4*pi, 4*pi, len = 27)
##
## flld.c> r <- sqrt(outer(x^2, y^2, "+"))
##
## flld.c> filled.contour(cos(r^2)*exp(-r/(2*pi)), axes = FALSE)

##
## flld.c> ## rather, the key *should* be labeled:
## flld.c> filled.contour(cos(r^2)*exp(-r/(2*pi)), frame.plot = FALSE,
## flld.c+ plot.axes = {})
demo(lattice)

##
##
## demo(lattice)
## ---- ~~~~~~~
##
## > require(grid)

## Loading required package: grid

##
## > old.prompt <- devAskNewPage(TRUE)
##
## > ## store current settings, to be restored later
## > old.settings <- trellis.par.get()
##
## > ## changing settings to new 'theme'
## > trellis.par.set(theme = col.whitebg())
##
## > ## simulated example, histogram and kernel density estimate superpose
d
## > x <- rnorm(500)
##
## > densityplot(~x)
##
## > histogram(x, type = "density",
## + panel = function(x, ...) {
## + panel.histogram(x, ...)
## + panel.densityplot(x, col = "brown", plot.points = FALSE)
## + })
##
## > ## Using a custom panel function to superpose a fitted normal density
## > ## on a Kernel Density Estimate
## >
## > densityplot( ~ height | voice.part, data = singer, layout = c(2, 4),
## + xlab = "Height (inches)",
## + ylab = "Kernel Density\n with Normal Fit",
## + main = list("Estimated Density", cex = 1.4, col =
## + "DarkOliveGreen"), panel = function(x, ...) {
## + panel.densityplot(x, ...)
## + panel.mathdensity(dmath = dnorm,
## + args = list(mean=mean(x),sd=sd(x)))
## + } )

##
## > ## user defined panel functions and fonts
## >
## > states <- data.frame(state.x77,
## + state.name = dimnames(state.x77)[[1]],
## + state.region = factor(state.region))
##
## > xyplot(Murder ~ Population | state.region, data = states,
## + groups = state.name,
## + panel = function(x, y, subscripts, groups)
## + ltext(x = x, y = y, labels = groups[subscripts],
## + cex=.9, fontfamily = "HersheySans", fontface = "italic"),
## + par.strip.text = list(cex = 1.3, font = 4, col = "brown"),
## + xlab = list("Estimated Population, July 1, 1975", font = 2),
## + ylab = list("Murder Rate (per 100,000 population), 1976", font
= 2),
## + main = list("Murder Rates in US states", col = "brown", font =
4))

##
## > ##graphical parameters for xlab etc can also be changed permanently
## > trellis.par.set(list(par.xlab.text = list(font = 2),
## + par.ylab.text = list(font = 2),
## + par.main.text = list(font = 4, col = "brown")))
##
## > ## Same with some multiple line text
## > levels(states$state.region) <-
## + c("Northeast", "South", "North\n Central", "West")
##
## > xyplot(Murder ~ Population | state.region, data = states,
## + groups = as.character(state.name),
## + panel = function(x, y, subscripts, groups)
## + ltext(x = x, y = y, labels = groups[subscripts], srt = -50,
## + col = "blue", cex=.9, fontfamily = "HersheySans"),
## + par.strip.text = list(cex = 1.3, font = 4, col = "brown",
## + lines = 2), xlab = "Estimated Population\nJuly 1, 1975",
## + ylab = "Murder Rate \n(per 100,000 population)\n 1976",
## + main = "Murder Rates in US states")

##
## > ##setting these back to their defaults
## > trellis.par.set(list(par.xlab.text = list(font = 1),
## + par.ylab.text = list(font = 1),
## + par.main.text = list(font = 2, col = "black")))
##
## > ##levelplot
## >
## > levelplot(volcano, colorkey = list(space = "top"),
## + sub = "Maunga Whau volcano", aspect = "iso")
##
## > ## wireframe
## > wireframe(volcano, shade = TRUE,
## + aspect = c(61/87, 0.4),
## + screen = list(z = -120, x = -45),
## + light.source = c(0,0,10), distance = .2,
## + shade.colors.palette = function(irr, ref, height, w = .5)
## + grey(w * irr + (1 - w) * (1 - (1-ref)^.4)))
##
## > ## 3-D surface parametrized on a 2-D grid
## >
## > n <- 50
##
## > tx <- matrix(seq(-pi, pi, length.out = 2*n), 2*n, n)
##
## > ty <- matrix(seq(-pi, pi, length.out = n) / 2, 2*n, n, byrow = T)
##
## > xx <- cos(tx) * cos(ty)
##
## > yy <- sin(tx) * cos(ty)
##
## > zz <- sin(ty)
##
## > zzz <- zz
##
## > zzz[,1:12 * 4] <- NA
##
## > wireframe(zzz ~ xx * yy, shade = TRUE, light.source = c(3,3,3))
##
## > ## Example with panel.superpose.
## >
## > xyplot(Petal.Length~Petal.Width, data = iris, groups=Species,
## + panel = panel.superpose,
## + type = c("p", "smooth"), span=.75,
## + col.line = trellis.par.get("strip.background")$col,
## + col.symbol = trellis.par.get("strip.shingle")$col,
## + key = list(title = "Iris Data", x = .15, y=.85, corner = c(0,1),
## + border = TRUE,
## + points = list(col=trellis.par.get("strip.shingle")$col[1:3],
## + pch = trellis.par.get("superpose.symbol")$pch[1:3],
## + cex = trellis.par.get("superpose.symbol")$cex[1:3]
## + ),
## + text = list(levels(iris$Species))))

##
## > ## non-trivial strip function
## >
## > barchart(variety ~ yield | year * site, barley, origin = 0,
## + layout = c(4, 3),
## + between = list(x = c(0, 0.5, 0)),
## + ## par.settings = list(clip = list(strip = "on")),
## + strip =
## + function(which.given,
## + which.panel,
## + factor.levels,
## + bg = trellis.par.get("strip.background")$col[which.given],
## + ...) {
## + axis.line <- trellis.par.get("axis.line")
## + pushViewport(viewport(clip = trellis.par.get("clip")$strip,
## + name = trellis.vpname("strip")))
## + if (which.given == 1)
## + {
## + grid.rect(x = .26, just = "right",
## + name = trellis.grobname("fill", type="strip"),
## + gp = gpar(fill = bg, col = "transparent"))
## + ltext(factor.levels[which.panel[which.given]],
## + x = .24, y = .5, adj = 1,
## + name.type = "strip")
## + }
## + if (which.given == 2)
## + {
## + grid.rect(x = .26, just = "left",
## + name = trellis.grobname("fill", type="strip"),
## + gp = gpar(fill = bg, col = "transparent"))
## + ltext(factor.levels[which.panel[which.given]],
## + x = .28, y = .5, adj = 0,
## + name.type = "strip")
## + }
## + upViewport()
## + grid.rect(name = trellis.grobname("border", type="strip"),
## + gp =
## + gpar(col = axis.line$col,
## + lty = axis.line$lty,
## + lwd = axis.line$lwd,
## + alpha = axis.line$alpha,
## + fill = "transparent"))
## + }, par.strip.text = list(lines = 0.4))
##
## > trellis.par.set(theme = old.settings, strict = 2)
##
## > devAskNewPage(old.prompt)

demo(package = .packages(all.available = TRUE))


2. Grafik Dasar
data(iris)
iris

## Sepal.Length Sepal.Width Petal.Length Petal.Width Species


## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
## 7 4.6 3.4 1.4 0.3 setosa
## 8 5.0 3.4 1.5 0.2 setosa
## 9 4.4 2.9 1.4 0.2 setosa
## 10 4.9 3.1 1.5 0.1 setosa
## 11 5.4 3.7 1.5 0.2 setosa
## 12 4.8 3.4 1.6 0.2 setosa
## 13 4.8 3.0 1.4 0.1 setosa
## 14 4.3 3.0 1.1 0.1 setosa
## 15 5.8 4.0 1.2 0.2 setosa
## 16 5.7 4.4 1.5 0.4 setosa
## 17 5.4 3.9 1.3 0.4 setosa
## 18 5.1 3.5 1.4 0.3 setosa
## 19 5.7 3.8 1.7 0.3 setosa
## 20 5.1 3.8 1.5 0.3 setosa
## 21 5.4 3.4 1.7 0.2 setosa
## 22 5.1 3.7 1.5 0.4 setosa
## 23 4.6 3.6 1.0 0.2 setosa
## 24 5.1 3.3 1.7 0.5 setosa
## 25 4.8 3.4 1.9 0.2 setosa
## 26 5.0 3.0 1.6 0.2 setosa
## 27 5.0 3.4 1.6 0.4 setosa
## 28 5.2 3.5 1.5 0.2 setosa
## 29 5.2 3.4 1.4 0.2 setosa
## 30 4.7 3.2 1.6 0.2 setosa
## 31 4.8 3.1 1.6 0.2 setosa
## 32 5.4 3.4 1.5 0.4 setosa
## 33 5.2 4.1 1.5 0.1 setosa
## 34 5.5 4.2 1.4 0.2 setosa
## 35 4.9 3.1 1.5 0.2 setosa
## 36 5.0 3.2 1.2 0.2 setosa
## 37 5.5 3.5 1.3 0.2 setosa
## 38 4.9 3.6 1.4 0.1 setosa
## 39 4.4 3.0 1.3 0.2 setosa
## 40 5.1 3.4 1.5 0.2 setosa
## 41 5.0 3.5 1.3 0.3 setosa
## 42 4.5 2.3 1.3 0.3 setosa
## 43 4.4 3.2 1.3 0.2 setosa
## 44 5.0 3.5 1.6 0.6 setosa
## 45 5.1 3.8 1.9 0.4 setosa
## 46 4.8 3.0 1.4 0.3 setosa
## 47 5.1 3.8 1.6 0.2 setosa
## 48 4.6 3.2 1.4 0.2 setosa
## 49 5.3 3.7 1.5 0.2 setosa
## 50 5.0 3.3 1.4 0.2 setosa
## 51 7.0 3.2 4.7 1.4 versicolor
## 52 6.4 3.2 4.5 1.5 versicolor
## 53 6.9 3.1 4.9 1.5 versicolor
## 54 5.5 2.3 4.0 1.3 versicolor
## 55 6.5 2.8 4.6 1.5 versicolor
## 56 5.7 2.8 4.5 1.3 versicolor
## 57 6.3 3.3 4.7 1.6 versicolor
## 58 4.9 2.4 3.3 1.0 versicolor
## 59 6.6 2.9 4.6 1.3 versicolor
## 60 5.2 2.7 3.9 1.4 versicolor
## 61 5.0 2.0 3.5 1.0 versicolor
## 62 5.9 3.0 4.2 1.5 versicolor
## 63 6.0 2.2 4.0 1.0 versicolor
## 64 6.1 2.9 4.7 1.4 versicolor
## 65 5.6 2.9 3.6 1.3 versicolor
## 66 6.7 3.1 4.4 1.4 versicolor
## 67 5.6 3.0 4.5 1.5 versicolor
## 68 5.8 2.7 4.1 1.0 versicolor
## 69 6.2 2.2 4.5 1.5 versicolor
## 70 5.6 2.5 3.9 1.1 versicolor
## 71 5.9 3.2 4.8 1.8 versicolor
## 72 6.1 2.8 4.0 1.3 versicolor
## 73 6.3 2.5 4.9 1.5 versicolor
## 74 6.1 2.8 4.7 1.2 versicolor
## 75 6.4 2.9 4.3 1.3 versicolor
## 76 6.6 3.0 4.4 1.4 versicolor
## 77 6.8 2.8 4.8 1.4 versicolor
## 78 6.7 3.0 5.0 1.7 versicolor
## 79 6.0 2.9 4.5 1.5 versicolor
## 80 5.7 2.6 3.5 1.0 versicolor
## 81 5.5 2.4 3.8 1.1 versicolor
## 82 5.5 2.4 3.7 1.0 versicolor
## 83 5.8 2.7 3.9 1.2 versicolor
## 84 6.0 2.7 5.1 1.6 versicolor
## 85 5.4 3.0 4.5 1.5 versicolor
## 86 6.0 3.4 4.5 1.6 versicolor
## 87 6.7 3.1 4.7 1.5 versicolor
## 88 6.3 2.3 4.4 1.3 versicolor
## 89 5.6 3.0 4.1 1.3 versicolor
## 90 5.5 2.5 4.0 1.3 versicolor
## 91 5.5 2.6 4.4 1.2 versicolor
## 92 6.1 3.0 4.6 1.4 versicolor
## 93 5.8 2.6 4.0 1.2 versicolor
## 94 5.0 2.3 3.3 1.0 versicolor
## 95 5.6 2.7 4.2 1.3 versicolor
## 96 5.7 3.0 4.2 1.2 versicolor
## 97 5.7 2.9 4.2 1.3 versicolor
## 98 6.2 2.9 4.3 1.3 versicolor
## 99 5.1 2.5 3.0 1.1 versicolor
## 100 5.7 2.8 4.1 1.3 versicolor
## 101 6.3 3.3 6.0 2.5 virginica
## 102 5.8 2.7 5.1 1.9 virginica
## 103 7.1 3.0 5.9 2.1 virginica
## 104 6.3 2.9 5.6 1.8 virginica
## 105 6.5 3.0 5.8 2.2 virginica
## 106 7.6 3.0 6.6 2.1 virginica
## 107 4.9 2.5 4.5 1.7 virginica
## 108 7.3 2.9 6.3 1.8 virginica
## 109 6.7 2.5 5.8 1.8 virginica
## 110 7.2 3.6 6.1 2.5 virginica
## 111 6.5 3.2 5.1 2.0 virginica
## 112 6.4 2.7 5.3 1.9 virginica
## 113 6.8 3.0 5.5 2.1 virginica
## 114 5.7 2.5 5.0 2.0 virginica
## 115 5.8 2.8 5.1 2.4 virginica
## 116 6.4 3.2 5.3 2.3 virginica
## 117 6.5 3.0 5.5 1.8 virginica
## 118 7.7 3.8 6.7 2.2 virginica
## 119 7.7 2.6 6.9 2.3 virginica
## 120 6.0 2.2 5.0 1.5 virginica
## 121 6.9 3.2 5.7 2.3 virginica
## 122 5.6 2.8 4.9 2.0 virginica
## 123 7.7 2.8 6.7 2.0 virginica
## 124 6.3 2.7 4.9 1.8 virginica
## 125 6.7 3.3 5.7 2.1 virginica
## 126 7.2 3.2 6.0 1.8 virginica
## 127 6.2 2.8 4.8 1.8 virginica
## 128 6.1 3.0 4.9 1.8 virginica
## 129 6.4 2.8 5.6 2.1 virginica
## 130 7.2 3.0 5.8 1.6 virginica
## 131 7.4 2.8 6.1 1.9 virginica
## 132 7.9 3.8 6.4 2.0 virginica
## 133 6.4 2.8 5.6 2.2 virginica
## 134 6.3 2.8 5.1 1.5 virginica
## 135 6.1 2.6 5.6 1.4 virginica
## 136 7.7 3.0 6.1 2.3 virginica
## 137 6.3 3.4 5.6 2.4 virginica
## 138 6.4 3.1 5.5 1.8 virginica
## 139 6.0 3.0 4.8 1.8 virginica
## 140 6.9 3.1 5.4 2.1 virginica
## 141 6.7 3.1 5.6 2.4 virginica
## 142 6.9 3.1 5.1 2.3 virginica
## 143 5.8 2.7 5.1 1.9 virginica
## 144 6.8 3.2 5.9 2.3 virginica
## 145 6.7 3.3 5.7 2.5 virginica
## 146 6.7 3.0 5.2 2.3 virginica
## 147 6.3 2.5 5.0 1.9 virginica
## 148 6.5 3.0 5.2 2.0 virginica
## 149 6.2 3.4 5.4 2.3 virginica
## 150 5.9 3.0 5.1 1.8 virginica

str(iris)

## 'data.frame': 150 obs. of 5 variables:


## $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
## $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
## $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
## $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1
1 1 1 1 1 ...
attach(iris)
plot(Petal.Length, Petal.Width)

plot(Petal.Width ~ Petal.Length)
plot(1, 1, xlim = c(1,7.5), ylim = c(0,5), type = "n")
points(1:7, rep(4.5,7), cex = 1:7, col = 1:7, pch = 0:6)
text(1:7, rep(3.5,7), labels = paste(0:6), cex = 1:7, col = 1:7)
points(1:7, rep(2,7), pch = (0:6)+7)
text((1:7)+0.25, rep(2,7), paste((0:6)+7))
points(1:7, rep(1,7), pch = (0:6)+14)
text((1:7)+0.25, rep(1,7), paste((0:6)+14))

plot(Petal.Length, Petal.Width, pch = 20, cex = 1.2, xlab = "Petal length(


cm)", ylab = "Petal Width(cm)", main = "Data Anderson Iris", col = c("slat
eblue","firebrick","darkolivegreen")[as.numeric(Species)])
3. Histogram
faithful

## eruptions waiting
## 1 3.600 79
## 2 1.800 54
## 3 3.333 74
## 4 2.283 62
## 5 4.533 85
## 6 2.883 55
## 7 4.700 88
## 8 3.600 85
## 9 1.950 51
## 10 4.350 85
## 11 1.833 54
## 12 3.917 84
## 13 4.200 78
## 14 1.750 47
## 15 4.700 83
## 16 2.167 52
## 17 1.750 62
## 18 4.800 84
## 19 1.600 52
## 20 4.250 79
## 21 1.800 51
## 22 1.750 47
## 23 3.450 78
## 24 3.067 69
## 25 4.533 74
## 26 3.600 83
## 27 1.967 55
## 28 4.083 76
## 29 3.850 78
## 30 4.433 79
## 31 4.300 73
## 32 4.467 77
## 33 3.367 66
## 34 4.033 80
## 35 3.833 74
## 36 2.017 52
## 37 1.867 48
## 38 4.833 80
## 39 1.833 59
## 40 4.783 90
## 41 4.350 80
## 42 1.883 58
## 43 4.567 84
## 44 1.750 58
## 45 4.533 73
## 46 3.317 83
## 47 3.833 64
## 48 2.100 53
## 49 4.633 82
## 50 2.000 59
## 51 4.800 75
## 52 4.716 90
## 53 1.833 54
## 54 4.833 80
## 55 1.733 54
## 56 4.883 83
## 57 3.717 71
## 58 1.667 64
## 59 4.567 77
## 60 4.317 81
## 61 2.233 59
## 62 4.500 84
## 63 1.750 48
## 64 4.800 82
## 65 1.817 60
## 66 4.400 92
## 67 4.167 78
## 68 4.700 78
## 69 2.067 65
## 70 4.700 73
## 71 4.033 82
## 72 1.967 56
## 73 4.500 79
## 74 4.000 71
## 75 1.983 62
## 76 5.067 76
## 77 2.017 60
## 78 4.567 78
## 79 3.883 76
## 80 3.600 83
## 81 4.133 75
## 82 4.333 82
## 83 4.100 70
## 84 2.633 65
## 85 4.067 73
## 86 4.933 88
## 87 3.950 76
## 88 4.517 80
## 89 2.167 48
## 90 4.000 86
## 91 2.200 60
## 92 4.333 90
## 93 1.867 50
## 94 4.817 78
## 95 1.833 63
## 96 4.300 72
## 97 4.667 84
## 98 3.750 75
## 99 1.867 51
## 100 4.900 82
## 101 2.483 62
## 102 4.367 88
## 103 2.100 49
## 104 4.500 83
## 105 4.050 81
## 106 1.867 47
## 107 4.700 84
## 108 1.783 52
## 109 4.850 86
## 110 3.683 81
## 111 4.733 75
## 112 2.300 59
## 113 4.900 89
## 114 4.417 79
## 115 1.700 59
## 116 4.633 81
## 117 2.317 50
## 118 4.600 85
## 119 1.817 59
## 120 4.417 87
## 121 2.617 53
## 122 4.067 69
## 123 4.250 77
## 124 1.967 56
## 125 4.600 88
## 126 3.767 81
## 127 1.917 45
## 128 4.500 82
## 129 2.267 55
## 130 4.650 90
## 131 1.867 45
## 132 4.167 83
## 133 2.800 56
## 134 4.333 89
## 135 1.833 46
## 136 4.383 82
## 137 1.883 51
## 138 4.933 86
## 139 2.033 53
## 140 3.733 79
## 141 4.233 81
## 142 2.233 60
## 143 4.533 82
## 144 4.817 77
## 145 4.333 76
## 146 1.983 59
## 147 4.633 80
## 148 2.017 49
## 149 5.100 96
## 150 1.800 53
## 151 5.033 77
## 152 4.000 77
## 153 2.400 65
## 154 4.600 81
## 155 3.567 71
## 156 4.000 70
## 157 4.500 81
## 158 4.083 93
## 159 1.800 53
## 160 3.967 89
## 161 2.200 45
## 162 4.150 86
## 163 2.000 58
## 164 3.833 78
## 165 3.500 66
## 166 4.583 76
## 167 2.367 63
## 168 5.000 88
## 169 1.933 52
## 170 4.617 93
## 171 1.917 49
## 172 2.083 57
## 173 4.583 77
## 174 3.333 68
## 175 4.167 81
## 176 4.333 81
## 177 4.500 73
## 178 2.417 50
## 179 4.000 85
## 180 4.167 74
## 181 1.883 55
## 182 4.583 77
## 183 4.250 83
## 184 3.767 83
## 185 2.033 51
## 186 4.433 78
## 187 4.083 84
## 188 1.833 46
## 189 4.417 83
## 190 2.183 55
## 191 4.800 81
## 192 1.833 57
## 193 4.800 76
## 194 4.100 84
## 195 3.966 77
## 196 4.233 81
## 197 3.500 87
## 198 4.366 77
## 199 2.250 51
## 200 4.667 78
## 201 2.100 60
## 202 4.350 82
## 203 4.133 91
## 204 1.867 53
## 205 4.600 78
## 206 1.783 46
## 207 4.367 77
## 208 3.850 84
## 209 1.933 49
## 210 4.500 83
## 211 2.383 71
## 212 4.700 80
## 213 1.867 49
## 214 3.833 75
## 215 3.417 64
## 216 4.233 76
## 217 2.400 53
## 218 4.800 94
## 219 2.000 55
## 220 4.150 76
## 221 1.867 50
## 222 4.267 82
## 223 1.750 54
## 224 4.483 75
## 225 4.000 78
## 226 4.117 79
## 227 4.083 78
## 228 4.267 78
## 229 3.917 70
## 230 4.550 79
## 231 4.083 70
## 232 2.417 54
## 233 4.183 86
## 234 2.217 50
## 235 4.450 90
## 236 1.883 54
## 237 1.850 54
## 238 4.283 77
## 239 3.950 79
## 240 2.333 64
## 241 4.150 75
## 242 2.350 47
## 243 4.933 86
## 244 2.900 63
## 245 4.583 85
## 246 3.833 82
## 247 2.083 57
## 248 4.367 82
## 249 2.133 67
## 250 4.350 74
## 251 2.200 54
## 252 4.450 83
## 253 3.567 73
## 254 4.500 73
## 255 4.150 88
## 256 3.817 80
## 257 3.917 71
## 258 4.450 83
## 259 2.000 56
## 260 4.283 79
## 261 4.767 78
## 262 4.533 84
## 263 1.850 58
## 264 4.250 83
## 265 1.983 43
## 266 2.250 60
## 267 4.750 75
## 268 4.117 81
## 269 2.150 46
## 270 4.417 90
## 271 1.817 46
## 272 4.467 74

attach(faithful)
summary(eruptions)

## Min. 1st Qu. Median Mean 3rd Qu. Max.


## 1.600 2.163 4.000 3.488 4.454 5.100

fivenum(eruptions)

## [1] 1.6000 2.1585 4.0000 4.4585 5.1000


hist(eruptions)

hist(eruptions, seq(1.6,5.2,0.2), prob = TRUE)


lines(density(eruptions, bw = 0.1))
rug(eruptions)
plot(ecdf(eruptions), do.points = FALSE, verticals = TRUE)

long <- eruptions[eruptions>3]


plot(ecdf(long), do.points = FALSE, verticals = TRUE)
x <- seq(3,5.4,0.01)
lines(x, pnorm(x, mean = mean(long), sd = sqrt(var(long))), lty = 3)
## Q-Q(Quantile-Quantile)
par(pty = "s")
qqnorm(long)
qqline(long)

## Boxplot
boxplot(eruptions)
boxplot(eruptions, main = "Plot dengan Boxplot")

boxplot(Petal.Length ~ Species, horizontal=T, col = "lightblue", boxwex =


.5, xlab = "Petal length (cm)", ylab = "Species", main = "BoxPlot Group")
coplot(Petal.Width ~ Petal.Length|Species,col = as.numeric(Species), pch =
as.numeric(Species))

pairs(iris[,1:4], col = as.numeric(Species), main = "Pairwise scatterplot"


)
stars(iris[,1:4], key.loc = c(2,35), mar = c(2,2,10,2), main = "Star plot
of individuals", frame = T)
4. Grafik Trellis
a. Satu Variabel (Univariat)
library(lattice)
densityplot(~Petal.Length, data = iris, main = "SEluruh Spesies")

densityplot(~Petal.Length|Species, data = iris)


b. Dua Variabel (Bivariat)
xyplot(Petal.Width ~ Petal.Length, data = iris,groups = Species, auto.ke
y = T, main = "Seluruh Species")

xyplot(Petal.Width ~ Petal.Length, data = iris,groups = Species, auto.key


= T, main = "Seluruh Spesies")
c. Tiga Variabel (Trivariat)
pl1 <- cloud(Sepal.Length ~ Petal.Length*Petal.Width, groups = Species,
data = iris, pch = 20, main = "Anderson Iris data, all species", screen
= list(z = 30, x = 60))
data(volcano)
pl2 <- wireframe(volcano, shade = TRUE, aspect = c(61/87,0.4), light.sou
rce=c(10,0,10), zoom = 1.1, box = F, scales = list(draw = F), xlab="", y
lab="", zlab = "", main = "Wireframe plot, Maunga Whau Volcano, Auckland
")
pl3 <- levelplot(volcano, col.regions = gray(0:16/16), main = "Levelplot
, Maunga Whau Volcano, Auckland")
pl4 <- contourplot(volcano, at = seq(floor(min(volcano)/10)*10, ceiling(
max(volcano)/10)*10, by = 10), main = "Contourplot, Maunga Whau Volcano,
Auckland", sub = "contour interval 10 m", region = T, col.regions = terr
ain.colors(100))
print(pl1, split = c(1,1,2,2), more = T)
print(pl2, split = c(2,1,2,2), more = T)
print(pl3, split = c(1,2,2,2), more = T)
print(pl4, split = c(2,2,2,2), more = F)

rm(pl1, pl2, pl3, pl4)


str(volcano)

## num [1:87, 1:61] 100 101 102 103 104 105 105 106 107 108 ...

You might also like