Laboratorio 2
##############################Perecederos#####################
rm(list=ls(all=TRUE))
objects()
##############################################################################
plot(pressure)
text(150, 600,
"Pressure (mm Hg)\nversus\nTemperature (Celsius)
mercury as a function of temperature.")
#############################################################################
postscript(file="myplot.ps")
plot(pressure)
dev.off()
png(file="myplot.png")
plot(pressure)
dev.off()
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
library(graphics)
help(barplot)
#######################################################
require(grDevices) # for colours
tN <- table(Ni <- stats::rpois(100, lambda=5))
r <- barplot(tN, col=rainbow(3))
#- type = "h" plotting *is* 'bar'plot
lines(r, tN, type='h', col='red', lwd=2)
barplot(tN, space = 1.5, axisnames=TRUE,
sub = "barplot(..., space= 1.5, axisnames = FALSE)")
barplot(VADeaths, plot = FALSE)
barplot(VADeaths, plot = FALSE, beside = TRUE)
mp <- barplot(VADeaths) # default
tot <- colMeans(VADeaths)
text(mp, tot + 3, format(tot), xpd = TRUE, col = "blue")
barplot(VADeaths, beside = TRUE,
col = c("lightblue", "mistyrose", "lightcyan",
"lavender", "cornsilk"),
legend = rownames(VADeaths), ylim = c(0, 100))
title(main = "Death Rates in Virginia", font.main = 4)
hh <- t(VADeaths)[, 5:1]
mybarcol <- "gray20"
mp <- barplot(hh, beside = TRUE,
col = c("lightblue", "mistyrose",
"lightcyan", "lavender"),
legend = colnames(VADeaths), ylim= c(0,100),
main = "Death Rates in Virginia", font.main = 4,
sub = "Faked upper 2*sigma error bars", col.sub = mybarcol,
cex.names = 1.5)
segments(mp, hh, mp, hh + 2*sqrt(1000*hh/100), col = mybarcol, lwd = 1.5)
stopifnot(dim(mp) == dim(hh))# corresponding matrices
mtext(side = 1, at = colMeans(mp), line = -2,
text = paste("Mean", formatC(colMeans(hh))), col = "red")
# Bar shading example
barplot(VADeaths, angle = 15+10*1:5, density = 20, col = "black",
legend = rownames(VADeaths))
title(main = list("Death Rates in Virginia", font = 4))
# border :
barplot(VADeaths, border = "dark blue")
# log scales (not much sense here):
barplot(tN, col=heat.colors(12), log = "y")
barplot(tN, col=gray.colors(20), log = "xy")
# args.legend
barplot(height = cbind(x = c(465, 91) / 465 * 100,
y = c(840, 200) / 840 * 100,
z = c(37, 17) / 37 * 100),
beside = FALSE,
width = c(465, 840, 37),
col = c(1, 2),
legend.text = c("A", "B"),
args.legend = list(x = "topleft"))
#####################################################################
par(ask=TRUE)
example(barplot)
#############################Plots of one or two variables###########
y <- rnorm(20)
par(ask=TRUE)
plot(y, type="p")
plot(y, type="l")
plot(y, type="b")
plot(y, type="h")
#############################################################
plot(pressure)
plot(pressure$temperature, pressure$pressure)
plot(pressure ~ temperature, data=pressure)
#################################################
lm.SR <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings)
plot(lm.SR)
################################################
library(cluster)
subset <- sample(1:150, 20)
cS <- as.character(Sp <- iris$Species[subset])
cS[Sp == "setosa"] <- "S"
cS[Sp == "versicolor"] <- "V"
cS[Sp == "virginica"] <- "g"
ai <- agnes(iris[subset, 1:4])
plot(ai, labels = cS)
###############################################
boxplot(decrease ~ treatment, data = OrchardSprays,
log = "y", col="light grey")
boxplot(decrease ~ treatment, data = OrchardSprays,
log = "y", col="light grey",
boxwex=0.5)
###############################################
barplot(VADeaths[1:2,], angle = c(45, 135),
density = 20, col = "grey",
names=c("RM", "RF", "UM", "UF"))
barplot(VADeaths[1:2,], angle = c(45, 135),
density = 20, col = "grey",
names=c("RM", "RF", "UM", "UF"),
horiz=TRUE)
################################################
y <- rnorm(20)
plot(y, type="l", lwd=3)
plot(y, type="l", col="grey")
plot(y, type="l", lty="dashed")
plot(y, type="l", ylim=c(-4, 4))
##############################################
rm(list=ls(all=TRUE))
objects()
library(TSA)
p1<-c(24, 29, 26, 2, 11, 0, 11, 25, 53, 31, 0, 18, 15, 6, 20, 60, 28, 9, 15, 15, 5, 24, 58, 20, 20, 5, 1, 7, 12, 13, 27, 23, 22, 19, 14, 16, 30, 17, 6, 5, 12, 10, 23, 67, 7, 17, 7, 9, 4, 3, 55, 14, 0, 3, 8, 5, 9, 19, 8, 10, 9, 2, 9, 13, 66, 8, 0, 0, 0, 0, 6, 22, 9, 6, 3, 3, 14, 7, 9, 10, 2, 13, 16, 28, 10, 8, 13, 7, 6, 4, 4, 3, 15, 23, 4, 4, 4, 4, 11, 31, 16, 5, 7, 5, 3, 12, 20, 10, 16, 3, 8, 4, 10, 17, 5, 2, 5, 13, 15, 6, 26, 9, 5, 5, 7, 4, 7, 9, 20, 4, 7, 2, 4, 7, 27, 11, 11, 6, 4, 11, 13, 21, 15, 0, 4, 3, 6, 6, 26, 5, 11, 0, 8, 5, 13, 26, 10, 5, 3, 4, 2, 11, 27, 12, 2, 4, 2, 2, 22, 23, 7, 5, 3, 5, 3, 15, 16, 6, 8, 2, 7, 7, 12, 14, 0, 2, 7, 3, 6, 9, 14, 4, 1, 1, 4, 3, 6, 4, 2, 2, 4, 8, 2, 7, 5, 6, 4, 5, 2, 4, 3, 11, 3, 5, 4, 5, 5, 10, 1, 9, 1, 5, 2, 5, 7, 10, 15, 6, 8, 8, 3, 11, 34, 9, 4, 14, 4, 5, 11, 35, 8, 11, 7, 5, 3, 17, 22, 14, 12, 6, 7, 4, 8, 17, 6)
p<-p1[1:35]
p
plot(p1,ylab='Inches',xlab='Year',type='o')
plot(p1,ylab='Inches',xlab='Year',type='b')
plot(p1,ylab='Inches',xlab='Year',type='l')
plot(p1,ylab='Inches',xlab='Year',type='p')data(color)
data(oilfilters)
plot(oilfilters,type='l',ylab='Sales')
Month=c('J','A','S','O','N','D','J','F','M','A','M','J')
points(oilfilters,pch=Month)
plot(p,type='l',ylab='Sales')
Semana=c('Vi','Sa','Do','Lu','Ma','Mi','Ju')
points(p,pch=Semana)
locator(n=2)
################################################
## A function to use identify to select points, and overplot the
## points with another symbol as they are selected
identifyPch <- function(x, y=NULL, n=length(x), pch=19, ...)
{
xy <- xy.coords(x, y); x <- xy$x; y <- xy$y
sel <- rep(FALSE, length(x)); res <- integer(0)
while(sum(sel) < n) {
ans <- identify(x[!sel], y[!sel], n=1, plot=FALSE, ...)
if(!length(ans)) break
ans <- which(!sel)[ans]
points(x[ans], y[ans], pch = pch)
sel[ans] <- TRUE
res <- c(res, ans)
}
res
}
identifyPch(15,40)

Estimados estudiantes, en la práctica del día de hoy he tomado el capítulo 1 & 2 del excelente libro de Murrell, les comparto el código en R para que lo platiquemos en clase
Saludos
Gabriel
#################################Laboratorio 2#############################
##############################Perecederos#####################
rm(list=ls(all=TRUE))
objects()
##############################################################################
plot(pressure)
text(150, 600,
"Pressure (mm Hg)\nversus\nTemperature (Celsius)
mercury as a function of temperature.")
#############################################################################
postscript(file="myplot.ps")
plot(pressure)
dev.off()
png(file="myplot.png")
plot(pressure)
dev.off()
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
library(graphics)
help(barplot)
#######################################################
require(grDevices) # for colours
tN <- table(Ni <- stats::rpois(100, lambda=5))
r <- barplot(tN, col=rainbow(3))
#- type = "h" plotting *is* 'bar'plot
lines(r, tN, type='h', col='red', lwd=2)
barplot(tN, space = 1.5, axisnames=TRUE,
sub = "barplot(..., space= 1.5, axisnames = FALSE)")
barplot(VADeaths, plot = FALSE)
barplot(VADeaths, plot = FALSE, beside = TRUE)
mp <- barplot(VADeaths) # default
tot <- colMeans(VADeaths)
text(mp, tot + 3, format(tot), xpd = TRUE, col = "blue")
barplot(VADeaths, beside = TRUE,
col = c("lightblue", "mistyrose", "lightcyan",
"lavender", "cornsilk"),
legend = rownames(VADeaths), ylim = c(0, 100))
title(main = "Death Rates in Virginia", font.main = 4)
hh <- t(VADeaths)[, 5:1]
mybarcol <- "gray20"
mp <- barplot(hh, beside = TRUE,
col = c("lightblue", "mistyrose",
"lightcyan", "lavender"),
legend = colnames(VADeaths), ylim= c(0,100),
main = "Death Rates in Virginia", font.main = 4,
sub = "Faked upper 2*sigma error bars", col.sub = mybarcol,
cex.names = 1.5)
segments(mp, hh, mp, hh + 2*sqrt(1000*hh/100), col = mybarcol, lwd = 1.5)
stopifnot(dim(mp) == dim(hh))# corresponding matrices
mtext(side = 1, at = colMeans(mp), line = -2,
text = paste("Mean", formatC(colMeans(hh))), col = "red")
# Bar shading example
barplot(VADeaths, angle = 15+10*1:5, density = 20, col = "black",
legend = rownames(VADeaths))
title(main = list("Death Rates in Virginia", font = 4))
# border :
barplot(VADeaths, border = "dark blue")
# log scales (not much sense here):
barplot(tN, col=heat.colors(12), log = "y")
barplot(tN, col=gray.colors(20), log = "xy")
# args.legend
barplot(height = cbind(x = c(465, 91) / 465 * 100,
y = c(840, 200) / 840 * 100,
z = c(37, 17) / 37 * 100),
beside = FALSE,
width = c(465, 840, 37),
col = c(1, 2),
legend.text = c("A", "B"),
args.legend = list(x = "topleft"))
#####################################################################
par(ask=TRUE)
example(barplot)
#############################Plots of one or two variables###########
y <- rnorm(20)
par(ask=TRUE)
plot(y, type="p")
plot(y, type="l")
plot(y, type="b")
plot(y, type="h")
#############################################################
plot(pressure)
plot(pressure$temperature, pressure$pressure)
plot(pressure ~ temperature, data=pressure)
#################################################
lm.SR <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings)
plot(lm.SR)
################################################
library(cluster)
subset <- sample(1:150, 20)
cS <- as.character(Sp <- iris$Species[subset])
cS[Sp == "setosa"] <- "S"
cS[Sp == "versicolor"] <- "V"
cS[Sp == "virginica"] <- "g"
ai <- agnes(iris[subset, 1:4])
plot(ai, labels = cS)
###############################################
boxplot(decrease ~ treatment, data = OrchardSprays,
log = "y", col="light grey")
boxplot(decrease ~ treatment, data = OrchardSprays,
log = "y", col="light grey",
boxwex=0.5)
###############################################
barplot(VADeaths[1:2,], angle = c(45, 135),
density = 20, col = "grey",
names=c("RM", "RF", "UM", "UF"))
barplot(VADeaths[1:2,], angle = c(45, 135),
density = 20, col = "grey",
names=c("RM", "RF", "UM", "UF"),
horiz=TRUE)
################################################
y <- rnorm(20)
plot(y, type="l", lwd=3)
plot(y, type="l", col="grey")
plot(y, type="l", lty="dashed")
plot(y, type="l", ylim=c(-4, 4))
##############################################
rm(list=ls(all=TRUE))
objects()
library(TSA)
p1<-c(24, 29, 26, 2, 11, 0, 11, 25, 53, 31, 0, 18, 15, 6, 20, 60, 28, 9, 15, 15, 5, 24, 58, 20, 20, 5, 1, 7, 12, 13, 27, 23, 22, 19, 14, 16, 30, 17, 6, 5, 12, 10, 23, 67, 7, 17, 7, 9, 4, 3, 55, 14, 0, 3, 8, 5, 9, 19, 8, 10, 9, 2, 9, 13, 66, 8, 0, 0, 0, 0, 6, 22, 9, 6, 3, 3, 14, 7, 9, 10, 2, 13, 16, 28, 10, 8, 13, 7, 6, 4, 4, 3, 15, 23, 4, 4, 4, 4, 11, 31, 16, 5, 7, 5, 3, 12, 20, 10, 16, 3, 8, 4, 10, 17, 5, 2, 5, 13, 15, 6, 26, 9, 5, 5, 7, 4, 7, 9, 20, 4, 7, 2, 4, 7, 27, 11, 11, 6, 4, 11, 13, 21, 15, 0, 4, 3, 6, 6, 26, 5, 11, 0, 8, 5, 13, 26, 10, 5, 3, 4, 2, 11, 27, 12, 2, 4, 2, 2, 22, 23, 7, 5, 3, 5, 3, 15, 16, 6, 8, 2, 7, 7, 12, 14, 0, 2, 7, 3, 6, 9, 14, 4, 1, 1, 4, 3, 6, 4, 2, 2, 4, 8, 2, 7, 5, 6, 4, 5, 2, 4, 3, 11, 3, 5, 4, 5, 5, 10, 1, 9, 1, 5, 2, 5, 7, 10, 15, 6, 8, 8, 3, 11, 34, 9, 4, 14, 4, 5, 11, 35, 8, 11, 7, 5, 3, 17, 22, 14, 12, 6, 7, 4, 8, 17, 6)
p<-p1[1:35]
p
plot(p1,ylab='Inches',xlab='Year',type='o')
plot(p1,ylab='Inches',xlab='Year',type='b')
plot(p1,ylab='Inches',xlab='Year',type='l')
plot(p1,ylab='Inches',xlab='Year',type='p')data(color)
data(oilfilters)
plot(oilfilters,type='l',ylab='Sales')
Month=c('J','A','S','O','N','D','J','F','M','A','M','J')
points(oilfilters,pch=Month)
plot(p,type='l',ylab='Sales')
Semana=c('Vi','Sa','Do','Lu','Ma','Mi','Ju')
points(p,pch=Semana)
locator(n=2)
################################################
## A function to use identify to select points, and overplot the
## points with another symbol as they are selected
identifyPch <- function(x, y=NULL, n=length(x), pch=19, ...)
{
xy <- xy.coords(x, y); x <- xy$x; y <- xy$y
sel <- rep(FALSE, length(x)); res <- integer(0)
while(sum(sel) < n) {
ans <- identify(x[!sel], y[!sel], n=1, plot=FALSE, ...)
if(!length(ans)) break
ans <- which(!sel)[ans]
points(x[ans], y[ans], pch = pch)
sel[ans] <- TRUE
res <- c(res, ans)
}
res
}
identifyPch(15,40)