In_class exercise1

## retrieve the datasets
library(datasets)
library(lattice)
VADeaths
##       Rural Male Rural Female Urban Male Urban Female
## 50-54       11.7          8.7       15.4          8.4
## 55-59       18.1         11.7       24.3         13.6
## 60-64       26.9         20.3       37.0         19.3
## 65-69       41.0         30.9       54.6         35.1
## 70-74       66.0         54.3       71.1         50.0
## examine the class of the datasets, which is a matrix
class(VADeaths)
## [1] "matrix"
## examine methods available  for dotplot 
methods("dotplot")
## [1] dotplot.array*   dotplot.default* dotplot.formula* dotplot.matrix* 
## [5] dotplot.numeric* dotplot.table*  
## see '?methods' for accessing help and source code
## draw a dotplot without grouping
dotplot(VADeaths, groups=FALSE)

## draw a dotplot with one column, four rows layout.
## disable the grouping
## set the panel ratio with aspect argument.
## type argument set the point to be joined with the baseline
dotplot(VADeaths, groups=FALSE, 
        layout=c(1, 4), 
        aspect=0.7, 
        origin=0, 
        type=c("p", "h"), 
        main="Death Rates in Virginia - 1940", #title
        xlab="Rate (per 1000)") ##title for x-axis

## a dotplot with grouping for all the population groups in one panel
## auto.key sets the labels on the right
dotplot(VADeaths, type="o",
        auto.key=list(lines=TRUE, space="right"),
        main="Death Rates in Virginia - 1940",
        xlab="Rate (per 1000)")

## draw a barchart disabling grouping with a single column 4 rows layout.
barchart(VADeaths, groups=FALSE,
         layout=c(1, 4), 
         aspect=0.7, 
         reference=FALSE, 
         main="Death Rates in Virginia - 1940",
         xlab="Rate (per 100)")

## retrieve data"postdoc" from package"latticeExtra"
data(postdoc, package="latticeExtra")

##draw a barchart computed by pro.table() in a single panel
##auto.key display labels for each group.
##a barchart  
barchart(prop.table(postdoc, margin=1), 
         xlab="Proportion",
         auto.key=list(adj=1))

## draw a dotplot in a multiple panel 
## enable abbreviation for the lable with
dotplot(prop.table(postdoc, margin=1), 
        groups=FALSE, 
        xlab="Proportion",
        par.strip.text=list(abbreviate=TRUE, minlength=10))

## draw a dotplot that the order of the fields are changed by panel and prepanel. 
dotplot(prop.table(postdoc, margin=1), 
        groups=FALSE, 
        index.cond=function(x, y) median(x),
        xlab="Proportion", 
        layout=c(1, 5), 
        aspect=0.6,
        scales=list(y=list(relation="free", rot=0)),
        prepanel=function(x, y) {
            list(ylim=levels(reorder(y, x)))
        },
        panel=function(x, y, ...) {
            panel.dotplot(x, reorder(y, x), ...)
        })

## retrieve the data
data(Chem97, package="mlmRev")

## build a two-way table of gcsescore and gender from Chem97
gcsescore.tab <- xtabs(~ gcsescore + gender, Chem97)

## convert it into a data frame 
gcsescore.df <- as.data.frame(gcsescore.tab)

## return "gcsescore" to numeric value.
gcsescore.df$gcsescore <- as.numeric(as.character(gcsescore.df$gcsescore))

## draw a xyplot by fregency against gcsescore under the condition of gender
xyplot(Freq ~ gcsescore | gender, 
       data = gcsescore.df, 
       type="h", 
       layout=c(1, 2), 
       xlab="Average GCSE Score")

## build a two-way table of score and gender from Chem97
score.tab <- xtabs(~score + gender, Chem97)

## convert it into a data frame 
score.df <- as.data.frame(score.tab)

##draw a bar chart displaying the frequency distribution of final score by gender.
barchart(Freq ~ score | gender, score.df, origin=0)

## The End