##讀取資料
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
##資料類型
class(VADeaths)
## [1] "matrix"
library(lattice)
##檢視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
##繪圖,圖內不分組
dotplot(VADeaths, groups=FALSE)

dotplot(VADeaths, groups=FALSE, 
        layout=c(1, 4), ##四列一行 
        aspect=0.7, ##圖形寬度
        origin=0, ##起始點
        type=c("p", "h"), ##點、線
        main="Death Rates in Virginia - 1940", 
        xlab="Rate (per 1000)")

##畫出點圖,在右側列出組別名稱
dotplot(VADeaths, type="o",
        auto.key=list(lines=TRUE, space="right"),
        main="Death Rates in Virginia - 1940",
        xlab="Rate (per 1000)")

##長條圖
barchart(VADeaths, groups=FALSE,
         layout=c(1, 4), 
         aspect=0.7, 
         reference=FALSE, 
         main="Death Rates in Virginia - 1940",
         xlab="Rate (per 100)")

##讀取postdoc資料
data(postdoc, package="latticeExtra")

##計算各組所佔的百分比
barchart(prop.table(postdoc, margin=1), ##只計算列
         xlab="Proportion",
         auto.key=list(adj=1))##列出組別名稱

##計算各組所佔的百分比(點圖)
dotplot(prop.table(postdoc, margin=1), 
        groups=FALSE, 
        xlab="Proportion",
        par.strip.text=list(abbreviate=TRUE, minlength=10))##文字縮寫,最多十個字

##計算各組所佔的百分比(點圖),排序資料
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), ...)
        })

##讀取資料
data(Chem97, package="mlmRev")

##製作交叉表
gcsescore.tab <- xtabs(~ gcsescore + gender, Chem97)

##轉換成data.frame
gcsescore.df <- as.data.frame(gcsescore.tab)

##轉換成numeric
gcsescore.df$gcsescore <- as.numeric(as.character(gcsescore.df$gcsescore))

##繪圖
xyplot(Freq ~ gcsescore | gender, 
       data = gcsescore.df, 
       type="h", 
       layout=c(1, 2), 
       xlab="Average GCSE Score")

##製作交叉表(依性別分組)
score.tab <- xtabs(~score + gender, Chem97)

##轉換成data.frame
score.df <- as.data.frame(score.tab)

##繪製長條圖(依性別分組)
barchart(Freq ~ score | gender, score.df, origin=0)

## The End