使用ggplot2畫點圖 (Dot Plot using ggplot2)

套路57: 使用ggplot2畫點圖 (Dot Plot using ggplot2)

1. 使用時機: 拿到數據時對數據的某些基本特徵(集中分散)進行分析了解。

2. 分析類型: 敘述性統計資料視覺化R繪圖。

3. 範例一、單變數一組樣本(資料):
第一步: 資料用小c將資料放入名稱為Lenvector (R最基本資料結構)。用rep函數產生與資料相同數目的(26)大寫F放入名稱為Fishvector再組合成名稱為datdata frame
  Len <- c(14.3, 15.8, 14.6, 16.1, 12.9, 15.1, 17.3, 14.0, 14.5, 13.9, 16.2, 14.3, 14.6, 13.3, 15.5, 11.8, 14.8,  13.5, 16.3, 15.4, 15.5, 13.9, 10.7, 14.8, 12.9, 15.4)
  Fish <- rep("F", 26)
  dat <- data.frame(Len, Fish)
  dat  # 顯示資料可檢查資料格式是否正確
第二步: 安裝ggplot2程式套件。
第三步: 呼叫ggplot2程式套件備用。
  library(ggplot2)
第四步: 畫圖一。
   ggplot(dat, aes(x = Fish, y = Len)) +
     geom_dotplot(binaxis = 'y', stackdir = 'center', fill = "#FFAAD4", stackratio = 1.5, dotsize = 0.5) +
     stat_summary(fun.y = mean, geom = "point", shape = 18, size = 3, color = "blue")
# 同時畫x-y散布(粉紅色點)圖及平均值(藍色點)
結果:
第四步: 畫圖二。
   ggplot(dat, aes(x = Fish, y = Len)) +
     geom_dotplot(binaxis = 'y', stackdir = 'center', fill = "#FFAAD4", stackratio = 1.5, dotsize = 0.5) +
     stat_summary(fun.y = median, geom = "point", shape = 18, size = 3, color = "blue")
# 同時畫x-y散布(粉紅色點)圖及中數(藍色點)
結果:

第四步: 畫圖三。
   安裝Hmisc  # 安裝Hmisc程式套件。
   library(Hmisc)   # 呼叫ggplot2程式套件備用。
   ggplot(dat, aes(x = Fish, y = Len)) +
     geom_dotplot(binaxis = 'y', stackdir = 'center', fill = "#FFAAD4", stackratio = 1.5, dotsize = 0.5) +
     stat_summary(fun.data = "mean_sdl", fun.args = list(mult=1), geom = "pointrange", width = 0.5, color = "blue")
# 同時畫x-y散布(粉紅色點)及標示平均值 +/- 標準差(藍色線)
結果:
第四步: 畫圖四。
   安裝Hmisc  # 安裝Hmisc程式套件。
   library(Hmisc)   # 呼叫ggplot2程式套件備用。
   ggplot(dat, aes(x = Fish, y = Len)) +
     geom_dotplot(binaxis = 'y', stackdir = 'center', fill = "#FFAAD4", stackratio = 1.5, dotsize = 0.5) +
     stat_summary(fun.data = "mean_sdl", fun.args = list(mult=1), geom = "crossbar", width = 0.5, color = "blue")
#同時畫x-y散布(粉紅色點) 及標示平均值 +/- 標準差(藍色線)
結果:
4. 範例二、單變數兩組樣本(資料):
第一步: 資料咪路調查高一和大一學生體重(kg)資料如下:
高一
41
35
33
36
40
46
31
37
34
30
38
大一
52
57
62
55
64
57
56
55
60
59

用小c將資料放入名稱為h1u1vector (R最基本資料結構)。用rep函數產生與資料相同數目的(1110)大寫HU放入名稱為h2u2vector再組合成名稱為datdata frame
  h1 <- c(41, 35, 33, 36, 40, 46, 31, 37, 34, 30, 38)
  u1 <- c(52, 57, 62, 55, 64, 57, 56, 55, 60, 59)
  h2 <- rep("H", 11)
  u2 <- rep("U", 10)
  Weight <- c(h1, u1)
  School <- c(h2, u2)
  dat <- data.frame(Weight, School)
  dat  # 顯示資料可檢查資料格式是否正確
第二步: 安裝ggplot2程式套件。
第三步: 呼叫ggplot2程式套件備用。
  library(ggplot2)
第四步: 畫圖一。
   ggplot(dat, aes(x = School, y = Weight)) +
     geom_dotplot(binaxis = 'y', stackdir = 'center', fill = "#FFAAD4", stackratio = 1.5, dotsize = 0.5) +
     stat_summary(fun.y = mean, geom = "point", shape = 18, size = 3, color = "blue")
# 同時畫x-y散布(粉紅色點)圖及平均值(藍色點)
結果:
第四步: 畫圖二。
   ggplot(dat, aes(x = School, y = Weight)) +
     geom_dotplot(binaxis = 'y', stackdir = 'center', fill = "#FFAAD4", stackratio = 1.5, dotsize = 0.5) +
     stat_summary(fun.y = median, geom = "point", shape = 18, size = 3, color = "blue")
# 同時畫x-y散布(粉紅色點)圖及中數(藍色點)
結果:
第四步: 畫圖三。
   安裝Hmisc  # 安裝Hmisc程式套件。
   library(Hmisc)   # 呼叫ggplot2程式套件備用。
   ggplot(dat, aes(x = School, y = Weight)) +
     geom_dotplot(binaxis = 'y', stackdir = 'center', fill = "#FFAAD4", stackratio = 1.5, dotsize = 0.5) +
     stat_summary(fun.data = "mean_sdl", fun.args = list(mult=1), geom = "pointrange", width = 0.5, color = "blue")
# 同時畫x-y散布(粉紅色點)及標示平均值 +/- 標準差(藍色線)
結果:
第四步: 畫圖四。
   安裝Hmisc  # 安裝Hmisc程式套件。
   library(Hmisc)   # 呼叫ggplot2程式套件備用。
   ggplot(dat, aes(x = School, y = Weight)) +
     geom_dotplot(binaxis = 'y', stackdir = 'center', fill = "#FFAAD4", stackratio = 1.5, dotsize = 0.5) +
     stat_summary(fun.data = "mean_sdl", fun.args = list(mult=1), geom = "crossbar", width = 0.5, color = "blue")
#同時畫x-y散布(粉紅色點) 及標示平均值 +/- 標準差(藍色線)
結果:
5. 範例三、單變數多組樣本(資料):
第一步: 資料咪路調查餵食不同飼料的肉雞體重(g),資料如下:
飼料1
飼料2
飼料3
飼料4
61.8
78.8
70.5
60.3
65.1
79.5
72.6
63.8
61.7
76.0
71.7
64.1
63.3
73.4
72.0
61.4

77.3
71.1
60.9
使用基本模組(base)read.table函數輸入建立資料儲存到變數m
m <- read.table(header = TRUE, text = "
Feed Weight
F1 61.8
F1 65.1
F1 61.7
F1 63.3
F2 78.8
F2 79.5
F2 76.0
F2 73.4
F2 77.3
F3 70.5
F3 72.6
F3 71.7
F3 72.0
F3 71.1
F4 60.3
F4 63.8
F4 64.1
F4 61.4
F4 60.9")  # 資料間以空白間隔,F1-F4: 餵食不同飼料。
attach(m)  # 告知R使用資料m
names(m)  # 指定資料標題。

第二步: 安裝ggplot2程式套件。
第三步: 呼叫ggplot2程式套件備用。
  library(ggplot2)
第四步: 畫圖一。
   ggplot(m, aes(x = Feed, y = Weight)) +
     geom_dotplot(binaxis = 'y', stackdir = 'center', fill = "#FFAAD4", stackratio = 1.5, dotsize = 0.5) +
     stat_summary(fun.y = mean, geom = "point", shape = 18, size = 3, color = "blue")
# 同時畫x-y散布(粉紅色點)圖及平均值(藍色點)
結果:
第四步: 畫圖二。
   ggplot(m, aes(x = Feed, y = Weight)) +
     geom_dotplot(binaxis = 'y', stackdir = 'center', fill = "#FFAAD4", stackratio = 1.5, dotsize = 0.5) +
     stat_summary(fun.y = median, geom = "point", shape = 18, size = 3, color = "blue")
# 同時畫x-y散布(粉紅色點)圖及中數(藍色點)
結果:
第四步: 畫圖三。
   安裝Hmisc  # 安裝Hmisc程式套件。
   library(Hmisc)   # 呼叫ggplot2程式套件備用。
   ggplot(m, aes(x = Feed, y = Weight)) +
     geom_dotplot(binaxis = 'y', stackdir = 'center', fill = "#FFAAD4", stackratio = 1.5, dotsize = 0.5) +
     stat_summary(fun.data = "mean_sdl", fun.args = list(mult=1), geom = "pointrange", width = 0.5, color = "blue")
# 同時畫x-y散布(粉紅色點)及標示平均值 +/- 標準差(藍色線)
結果:
第四步: 畫圖四。
   安裝Hmisc  # 安裝Hmisc程式套件。
   library(Hmisc)   # 呼叫ggplot2程式套件備用。
   ggplot(m, aes(x = Feed, y = Weight)) +
     geom_dotplot(binaxis = 'y', stackdir = 'center', fill = "#FFAAD4", stackratio = 1.5, dotsize = 0.5) +
     stat_summary(fun.data = "mean_sdl", fun.args = list(mult=1), geom = "crossbar", width = 0.5, color = "blue")
#同時畫x-y散布(粉紅色點) 及標示平均值 +/- 標準差(藍色線)
結果:
6. 範例四變數樣本(資料):
第一步: 資料咪路調查人類血漿中鉀離子濃度(mg/100 ml)資料如下:
沒注射賀爾蒙
注射賀爾蒙
16.3
15.3
38.1
34.0
20.4
17.4
26.2
22.8
12.4
10.9
32.3
27.8
15.8
10.3
35.8
25.0
9.5
6.7
30.2
29.3
使用基本模組(base)read.table函數輸入建立資料儲存到變數m
m <- read.table(header = TRUE, text = "
Hor Sex Conc
N_H F 16.3
N_H F 20.4
N_H F 12.4
N_H F 15.8
N_H F 9.5
N_H M 15.3
N_H M 17.4
N_H M 10.9
N_H M 10.3
N_H M 6.7
A_H F 38.1
A_H F 26.2
A_H F 32.3
A_H F 35.8
A_H F 30.2
A_H M 34
A_H M 22.8
A_H M 27.8
A_H M 25
A_H M 29.3")  # 資料間以空白間隔N_H: 沒注射賀爾蒙A_H: 有注射賀爾蒙
attach(m)  # 告知R使用資料m
names(m)  # 指定資料標題

: 安裝程式套件ggplot2
第三步: 呼叫ggplot2
  library(ggplot2)
第四步: 畫圖一。
  ggplot(m, aes(x = Hor, y = Conc, fill = Sex)) +
    geom_dotplot(binaxis='y', stackdir='center', position=position_dodge(0.8), dotsize = 0.5)
結果:
第四步: 畫圖二。
  ggplot(m, aes(x = Hor, y = Conc, fill = Sex)) +
    geom_boxplot(position = position_dodge(0.8))+
    geom_dotplot(binaxis = 'y', stackdir = 'center', position = position_dodge(0.8), dotsize = 0.5)
# 加上box plot
結果:
7. 範例五三因子樣本(資料):
第一步: 資料咪路研究溫度對孔雀魚(Poecilia reticulata)、寶蓮燈魚(Paracheirodon axelrodi)及斑馬魚(Danio rerio)生長的影響。在20°C24°C 28°C培養下,三種魚身長變化(cm)資料如下:
孔雀魚(Poecilia reticulata)
20°C
24°C
28°C
0.9
0.8
1.15
1.2
1.45
1.5
0.8
0.7
1.05
1.35
1.4
1.55
0.6
0.4
1.0
1.2
1.7
1.5
0.4
0.5
1.0
1.3
1.6
1.35

斑馬魚(Danio rerio)
20°C
24°C
28°C
1.05
1.15
1.2
1.0
1.8
1.55
1.0
1.0
1.3
1.15
1.55
1.5
0.9
0.95
1.35
1.05
1.7
1.4
1.1
0.85
1.15
1.2
1.6
1.6

寶蓮燈魚(Paracheirodon axelrodi)
20°C
24°C
28°C
0.55
0.7
1.0
1.2
1.45
1.6
0.6
0.5
1.05
1.3
1.4
1.45
0.5
0.65
0.95
1.15
1.5
1.4
0.7
0.6
1.1
1.1
1.55
1.45
使用基本模組(base)read.table函數輸入建立資料儲存到變數m
m <- read.table(header = TRUE, text = "
Fish Temp Sex Growth
F1 t20 M 0.95
F1 t20 M 0.9
F1 t20 M 0.8
F1 t20 M 0.7
F1 t20 F 0.9
F1 t20 F 0.85
F1 t20 F 0.7
F1 t20 F 0.75
F1 t24 M 1.15
F1 t24 M 1.05
F1 t24 M 1
F1 t24 M 1.3
F1 t24 F 1.2
F1 t24 F 1.35
F1 t24 F 1.2
F1 t24 F 1.3
F1 t28 M 1.45
F1 t28 M 1.4
F1 t28 M 1.7
F1 t28 M 1.6
F1 t28 F 1.5
F1 t28 F 1.55
F1 t28 F 1.5
F1 t28 F 1.35
F2 t20 M 1.05
F2 t20 M 1
F2 t20 M 0.9
F2 t20 M 1.1
F2 t20 F 1.15
F2 t20 F 1
F2 t20 F 0.95
F2 t20 F 0.85
F2 t24 M 1.2
F2 t24 M 1.3
F2 t24 M 1.35
F2 t24 M 1.15
F2 t24 F 1
F2 t24 F 1.15
F2 t24 F 1.05
F2 t24 F 1.2
F2 t28 M 1.8
F2 t28 M 1.55
F2 t28 M 1.7
F2 t28 M 1.6
F2 t28 F 1.55
F2 t28 F 1.5
F2 t28 F 1.4
F2 t28 F 1.6
F3 t20 M 0.55
F3 t20 M 0.6
F3 t20 M 0.5
F3 t20 M 0.7
F3 t20 F 0.7
F3 t20 F 0.5
F3 t20 F 0.65
F3 t20 F 0.6
F3 t24 M 1
F3 t24 M 1.05
F3 t24 M 0.95
F3 t24 M 1.1
F3 t24 F 1.2
F3 t24 F 1.3
F3 t24 F 1.15
F3 t24 F 1.1
F3 t28 M 1.45
F3 t28 M 1.4
F3 t28 M 1.5
F3 t28 M 1.55
F3 t28 F 1.6
F3 t28 F 1.45
F3 t28 F 1.4
F3 t28 F 1.45")  # 資料以空白間隔F1~F3是不同魚t20~t28是不同溫度FM是性別
attach(m)  # 告知R使用資料m
names(m)  # 指定資料標題

: 安裝程式套件ggplot2
第三步: 呼叫ggplot2
  library(ggplot2)
第四步: 畫圖一
  ggplot(m, aes(x = Temp, y = Growth, fill = Sex)) +
    geom_dotplot(binaxis = 'y', stackdir = 'center', position = position_dodge(1), dotsize = 0.5) +
    facet_grid(. ~ Fish)
# 三種魚畫三格畫成3 x 1排列
結果:
第四步: 畫圖二
  ggplot(m, aes(x = Temp, y = Growth, fill = Sex)) +
    geom_boxplot(position = position_dodge(0.8))+
    geom_dotplot(binaxis = 'y', stackdir = 'center', position = position_dodge(1), dotsize = 0.5) +
    facet_grid(. ~ Fish)
# 三種魚畫三格畫成1 x 3排列加上box plot
結果:
來勁了嗎? 想知道更多?? 補充資料(連結): 關於R繪圖快速入門

留言

  1. Thunder Titanium Lights | The TITanium Art
    › tag keith titanium › thunder-ti-lights › tag › thunder-ti-lights titanium dab tool Dec 24, 2021 — Dec 24, titanium car 2021 Thunder Titanium Lights, aka the Thunder TFTs, is a series of highly dynamic LED lighting, produced titanium exhaust tips in an attempt to titanium nose stud offer a true

    回覆刪除

張貼留言

這個網誌中的熱門文章

統計不球人 目錄 (Table of Contents)

如何選擇統計方法 1

如何檢查資料是否接近常態分布 (Normality Test using R)