##在執行這code前,需先安裝package及調用,不然跑不出來
library(lattice)
library(datasets)
##讀取它
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"
##檢視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),##1行4列 
        aspect=0.7, ##圖形寬度 
        origin=0, ##原點
        type=c("p", "h"),##畫點線
        main="Death Rates in Virginia - 1940", 
        xlab="Rate (per 1000)") ##圖標題名稱及X軸名稱

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

##barchart長條圖 ,一行四列
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和gender的table
gcsescore.tab <- xtabs(~ gcsescore + gender, Chem97)

head(gcsescore.tab)
##          gender
## gcsescore M F
##     0     1 0
##     1     2 0
##     1.5   0 1
##     2     1 0
##     2.083 1 0
##     2.428 1 1
## 轉換成 data frame 的樣式
gcsescore.df <- as.data.frame(gcsescore.tab)
head(gcsescore.df)
##   gcsescore gender Freq
## 1         0      M    1
## 2         1      M    2
## 3       1.5      M    0
## 4         2      M    1
## 5     2.083      M    1
## 6     2.428      M    1
##as.numeric將gcsescore轉換成數字形式
gcsescore.df$gcsescore <- as.numeric(as.character(gcsescore.df$gcsescore))


##畫圖,二列一行,資料為gcsescore.df中的Freq ~ gcsescore和gender,X軸顯示Average GCSE Score
xyplot(Freq ~ gcsescore | gender, 
       data = gcsescore.df, 
       type="h", 
       layout=c(1, 2), 
       xlab="Average GCSE Score")

##一樣,畫表(如head)
score.tab <- xtabs(~score + gender, Chem97)
head(score.tab)
##      gender
## score    M    F
##    0  2198 1490
##    2  2027 1600
##    4  2555 2064
##    6  3103 2636
##    8  3634 3034
##    10 3745 2936
##轉換為data frame(如head)
score.df <- as.data.frame(score.tab)
head(score.df)
##   score gender Freq
## 1     0      M 2198
## 2     2      M 2027
## 3     4      M 2555
## 4     6      M 3103
## 5     8      M 3634
## 6    10      M 3745
##畫長條圖
barchart(Freq ~ score | gender, score.df, origin=0)

## The End