Copiare i comandi che seguono nella Console di R

(versione del 12/10/2007)


Scaricare lo script o fare copia e incolla di quanto segue.

Scaricare lo chem.rda.

Scaricare lo ita.rda.

Scaricare lo postdoc.rda.

Scaricare i seguenti file mappe Provincia_poly.dbf, Provincia_poly.prj, Provincia_poly.shp, Provincia_poly.shx.

# lattice graphics

require(lattice)
load("chem.rda")

# chem97
# score: A-level test score in chimica
# gcsescore: score pre test

histogram( ~ gcsescore | factor(score), data = Chem97)

densityplot( ~ gcsescore | factor(score), data = Chem97, ref = TRUE, plot.points=F)

densityplot( ~ gcsescore, data = Chem97, groups=score,
				plot.points=F, ref=TRUE, auto.key=list(columns=3))

# concetto di panel

tp1 <- histogram(~ gcsescore | factor(score), data=Chem97)
tp2 <- densityplot( ~ gcsescore, data = Chem97, groups=score,
				plot.points=F, auto.key=list(space="right", title="score"))
print(tp1)

# combinare 2 panel
# split = c(row, col, nrow, ncol)
plot(tp1, split=c(1,1,1,2))
plot(tp2, split=c(1,2,1,2), newpage=FALSE)

# oppure
plot(tp1, split=c(1,1,1,2),more=TRUE)
plot(tp2, split=c(1,2,1,2))

# position: come split ma in termini proporzionali
# position = c(left, bottom, right, top)
plot(tp1, position=c(0, 0.6, 1, 1), more=TRUE)
plot(tp2, position=c(0, 0, 1, 0.6) )


# layout
densityplot( ~ gcsescore | factor(score), data = Chem97, ref = TRUE,
	plot.points=F, layout=c(1,6))


# ref = aggiunge o meno la base line in 0
densityplot( ~ gcsescore | factor(score), data = Chem97, ref = TRUE,
	plot.points=F, layout=c(1,6))

# esempio di diagramma a barre condizionato
load("postdoc.rda")
# Reasons for Taking First Postdoctoral Appointment, by Field of Doctrate, 1997

barchart( prop.table(postdoc, margin=1), xlab="proportion", auto.key=list(aadj=1))

# dati titanic
data(Titanic)
str(Titanic)
Titanic

# scala libera su ogni panel per mostrare le freq. assolute
barchart(Class ~ Freq | Sex+Age, data=as.data.frame(Titanic),
			groups=Survived, stack=TRUE, layout=c(4,1),
			auto.key=list(title="Survived", columns=2),
			scales = list(x="free"))
			

# stessa scala su ogni panel
barchart(Class ~ Freq | Sex+Age, data=as.data.frame(Titanic),
			groups=Survived, stack=TRUE, layout=c(4,1),
			auto.key=list(title="Survived", columns=2))
			




require(lattice)
load("ita.rda")

xyplot(SCITA05 ~ ITA05PR | factor(IDCLASSE), data=ita)

levels(ita$ITA05PR) -> old.lev
levels(ita$ITA05PR) <- c("I","S","B","D","O")
xyplot(SCITA05 ~ ITA05PR | factor(IDCLASSE), data=ita)

# cambiamo tipo di plot
# reg parametrica
xyplot(SCITA05 ~ ITA05PR | factor(IDCLASSE), data=ita, panel="panel.lmline")

# reg non parametrica
xyplot(SCITA05 ~ ITA05PR | factor(IDCLASSE), data=ita, panel="panel.loess")
 
# stessa cosa
xyplot(SCITA05 ~ ITA05PR | factor(IDCLASSE), data=ita, type="r")
xyplot(SCITA05 ~ ITA05PR | factor(IDCLASSE), data=ita, type="smooth")
 
# e assieme
xyplot(SCITA05 ~ ITA05PR | factor(IDCLASSE), data=ita, type=c("smooth","r"))

# ma se vogliamo di piu' dobbiamo scriverci la nostra funzioe panel

panel.lowess <-
function (x, y, span = 2/3, degree = 1, family = c("symmetric", 
    "gaussian"), evaluation = 50, lwd = plot.line$lwd, lty = plot.line$lty, 
    col, col.line = plot.line$col, type, horizontal = FALSE, 
    ...) 
{
    x <- as.numeric(x)
    y <- as.numeric(y)
    ok <- is.finite(x) & is.finite(y)
    if (sum(ok) < 1) 
        return()
    if (!missing(col)) {
        if (missing(col.line)) 
            col.line <- col
    }
    plot.line <- trellis.par.get("plot.line")
    if (horizontal) {
        smooth <- loess.smooth(y[ok], x[ok], span = span, family = family, 
            degree = degree, evaluation = evaluation)
        panel.lines(x = smooth$y, y = smooth$x, col = col.line, 
            lty = lty, lwd = lwd, ...)
    }
    else {
        smooth <- stats::lowess(x[ok], y[ok], f = span, iter = 3)
		panel.xyplot(x[ok],y[ok],type="p",col="blue")
		panel.xyplot(x[ok],y[ok],type="r",col="black",lwd=2, lty=3)
        panel.lines(x = smooth$x, y = smooth$y, col = "red", lty = 1, lwd = 2)
    }
}

# per gli italiani


xyplot(SCITA05 ~ ITA05PR | factor(IDCLASSE), data=ita,panel="panel.lowess",xlab="giudizi",ylab="punteggi del test",main="italiano")

levels(ita$ITA05PR) <- old.lev

require(maps)
require(maptools)

mp <- read.shape("Provincia_poly.shp")

require(latticeExtra)


# esempio di inclusione di mappe

library(maps)
library(mapproj)
library(latticeExtra)

data(USCancerRates)

mapplot(rownames(USCancerRates) ~ log(rate.male) + log(rate.female),
        data = USCancerRates,
        map = map("county", plot = FALSE, fill = TRUE,
                  projection = "mercator"))

mapplot(rownames(USCancerRates) ~ log(rate.male) + log(rate.female),
        data = USCancerRates,
        map = map("county", plot = FALSE, fill = TRUE,
                  projection = "tetra"),
        scales = list(draw = FALSE))