直方圖(histogram)

上個章節有介紹長條圖(bar chart)運用在累計次數資料,今天若運用在區間資料,直方圖可以清楚表達,首先先建立區間資料,以下為50名學生的英文成績。

score <- sample(1:100,50,replace = T)  #50名學生的英文成績
hist(score,        
     breaks = 10,                        #將區間以10為單位間隔
     col="green"                       #設定顏色為綠色
     )

莖葉圖

一種用圖形來表現數據(或稱資料)的方法,方便且容易畫的圖形,用來說明數據的分佈情形。

score <- sample(1:100,50,replace = T)  #50名學生的英文成績
stem(score)                            #畫出莖葉圖
## 
##   The decimal point is 1 digit(s) to the right of the |
## 
##   0 | 56
##   1 | 036999
##   2 | 126
##   3 | 4445788
##   4 | 55
##   5 | 2357
##   6 | 0355678889
##   7 | 00339
##   8 | 03368
##   9 | 005799

利用stem()函式可畫出莖葉圖,左邊區隔開的為分數的十位數,右邊為對應的個位數。

肩形圖(ogive)

肩形圖(ogive)是累積相對次數(cumulative relative frequency)的圖形表示。

score <- sample(1:100,50,replace = T)                #50名學生的英文成績
breaks=seq(0, 100, by=10)                            #設定X軸分數區間,以10分為一個間隔
score.cut <- cut(score, breaks, right=FALSE)         #將原始分數資料利用breaks區間分類
score.freq = table(score.cut)                        #將分類後資料轉成累積次數資料
cumfreq = c(0, cumsum(score.freq))                   #計算分別累積次數百分比
cumrelfreq = cumfreq / cumfreq[11]                   #計算累積相對次數百分比
options(digits=2)                                    #小數點取到第二位
plot(breaks, cumrelfreq, main="肩形圖", col="red")    #畫出肩形圖
lines(breaks, cumrelfreq)                            #將各點連結
text(breaks, cumrelfreq+0.08, cumrelfreq )           #標記各點累計機率

線圖(line chart)

時間序列通常以線圖(line chart) 來描述,它是對變數在不同時間點的值所繪的圖。

price <- sample(1:200,50,replace = F)  #某上市股票其50週收盤價格
plot(price, col="red")                 #畫出每周所對應其收盤價格
lines(price)                           #連結各點

散佈圖(scatter diagram)

將兩區間變數分別對應到2個維度座標軸上(X,Y軸),並觀察其對應是否有相關性,本次運用R語言內建資料庫,cars此筆數據共記載了兩筆數據,一為汽車速度,一為每次煞車到到靜止所需距離。

plot(cars, col="red")                 #畫出速度與距離散布圖

由散布圖中可觀察出當汽車速度越快,所需要的煞車距離更大,透露出兩著變數間具有正向的線性關係。