ggplot2 เป็น package สำหรับ data visualisation ในภาษา R และเป็นเครื่องมือสร้างกราฟที่มืออาชีพนิยม ตั้งแต่นักวิจัยในการตีพิมพ์ผลงาน ไปจนถึงสำนักข่าวระดับโลกอย่าง BBC และ Financial Times
ggplot2 มีจุดเด่น 4 ข้อ:
- ใช้งานง่าย
- สร้างกราฟได้หลากหลาย
- ปรับแต่งกราฟได้ดังใจ
- ได้กราฟที่ดูดีและมีคุณภาพ
.
ในบทความนี้ เราจะมาดูวิธีใช้ ggplot2 เพื่อสร้างกราฟแบบมืออาชีพกัน:
ggplot2syntax- Basic plotting
- Plot customisations
.
ถ้าพร้อมแล้ว ไปเริ่มกันเลย
- 🔤 gg for “Grammar of Graphics”
- 🏁 Getting Started With ggplot2
- ✍️ Basic Syntax
- 📊 Basic Plotting: Data, Aesthetics, & Geom
- 🔧 More Customisations
- 🔥 Summary
- 📚 Learn More About ggplot2
- 📃 References
- ✅ R Book for Psychologists: หนังสือภาษา R สำหรับนักจิตวิทยา
🔤 gg for “Grammar of Graphics”
gg ใน ggplot2 ย่อมาจาก “Grammar of Graphics”

Grammar of Graphics เป็นแนวคิดที่มองกราฟเป็นเหมือนภาษา คือ มีโครงสร้างและองค์ประกอบที่ตายตัว ซึ่งเมื่อเรานำมารวมกัน เราก็จะได้กราฟที่ต้องการขึ้นมา
โดยกราฟใน ggplot2 ประกอบด้วย 7 ส่วน หรือ layers ได้แก่:
| No. | Layer | Description |
|---|---|---|
| 1 | Data | ชุดข้อมูลสำหรับสร้างกราฟ |
| 2 | Aesthetics | จับคู่ข้อมูลกับกราฟ (เช่น แกน x และ y) |
| 3 | Geometric objects | ประเภทกราฟ (เช่น กราฟเส้น กราฟแท่ง) |
| 4 | Facets | สร้างกราฟย่อย |
| 5 | Statistical transformations | วิเคราะห์ข้อมูล (เช่น หาค่าเฉลี่ย) |
| 6 | Coordinates | แกนในการสร้างกราฟ |
| 7 | Theme | หน้าตาของกราฟ (เช่น สีพื้นหลัง) |
ในการทำงาน เรามักจะเรียกใช้งาน 3 layers แรกเป็นหลัก ได้แก่:
- Data
- Aesthetics
- Geometric objects
🏁 Getting Started With ggplot2
ในการเริ่มต้นใช้งาน ggplot2 เราต้องทำ 3 อย่างก่อน คือ:
.
ข้อที่ 1. ติดตั้ง ggplot2 บน environment ของเรา:
install.packages("ggplot2")
Note: ถ้าใครเคยติดตั้งแล้ว สามารถข้ามไปขั้นถัดไปได้เลย
.
ข้อที่ 2. เรียกใช้งาน ggplot2:
library(ggplot2)
Note: เราต้องเรียกใช้งาน ggplot2 ทุกครั้งที่เริ่ม session ใหม่
.
ข้อที่ 3. โหลด dataset ที่เราจะใช้สร้างกราฟ
สำหรับบทความนี้ เราจะใช้ penguins dataset ที่มีข้อมูลของเพนกวิน 3 สายพันธุ์ (เช่น สปีชีส์ น้ำหนัก ความยาวปีก) กัน
เราสามารถโหลด dataset ได้ดังนี้:
install.packages("palmerpenguins")
library(palmerpenguins)
จากนั้น เราสามารถ preview ข้อมูลได้ด้วย head():
head(penguins)
ผลลัพธ์:

Note: เราสามารถอ่านคู่มือ penguins ได้ด้วยคำสั่ง ?penguins
.
เมื่อทำครบทั้ง 3 ขั้นตอน เราก็พร้อมที่จะสร้างกราฟใน ggplot2 แล้ว
✍️ Basic Syntax
ก่อนไปดูการสร้างกราฟ เรามาดู syntax ของ ggplot2 กันก่อน:
ggplot(data, aes(x, y, other)) +
geom_*() +
...
- ggplot() คือ การเรียกใช้งาน
ggplot2 - data คือ ชุดข้อมูลในการสร้างกราฟ
- aes() คือ ส่วนจับคู่ข้อมูลกับลักษณะของกราฟ
- x, y คือ ข้อมูลที่แสดงบนแกน x และ y
- other คือ ข้อมูลที่จะแสดงผ่านส่วนอื่น ๆ ของกราฟ เช่น สี ขนาด รูปทรง
- geom_* คือ ประเภทกราฟ
- … คือ function อื่น ๆ ในการตั้งค่ากราฟ (เช่น theme, facet)
📊 Basic Plotting: Data, Aesthetics, & Geom
ในการสร้างกราฟ เรามี 3 input เบื้องต้นที่เราต้องกำหนด ได้แก่:
| No. | Input | Description |
|---|---|---|
| 1 | Data | Dataset ในการสร้างกราฟ |
| 2 | Aesthetics | ลักษณะของกราฟที่ใช้แสดงข้อมูล (เช่น แกน x และ y) |
| 3 | Geom | ประเภทกราฟ (เช่น กราฟเส้น กราฟแท่ง) |
Note: ทั้งสามอย่างสะท้อนถึง 3 layers แรกของ ggplot2
ยกตัวอย่างเช่น
สร้าง scatter plot ที่แสดงความสัมพันธ์ระหว่างน้ำหนักตัว (body_mass_g) และความยาวปีก (flipper_length_mm) ของเพนกวิน:
ggplot(penguins, aes(x = body_mass_g,
y = flipper_length_mm)) +
geom_point()
ผลลัพธ์:

.
🎨 Aesthetics
นอกจากแกน x และ y เราแสดงข้อมูลผ่านลักษณะอื่น ๆ ของกราฟได้ เช่น:
| No. | Parametre | Description |
|---|---|---|
| 1 | color | สีขอบรูปทรง |
| 2 | fill | สีในรูปทรง |
| 3 | shape | รูปทรง |
| 4 | size | ขนาด |
| 5 | alpha | ความโปร่งใส |
ยกตัวอย่างเช่น
เพิ่มสปีชีส์เข้าไปใน scatter plot:
ggplot(penguins, aes(x = body_mass_g,
y = flipper_length_mm,
color = species)) +
geom_point()
ผลลัพธ์:

Note: เราใช้ color แทน fill เพราะ จุด default ของ scatter plot ไม่รองรับการเติมสี
.
⏹️ Geom
เราสามารถเปลี่ยนประเภทกราฟได้ โดยเปลี่ยน geom_*() เช่น:
| No. | Geom | Graph |
|---|---|---|
| 1 | geom_histogram() | Histogram |
| 2 | geom_boxplot() | Box plot |
| 3 | geom_line() | Line plot |
| 4 | geom_col() | Bar plot |
| 5 | geom_density() | Density plot |
Note: ggplot2 มีรูปแบบกราฟกว่า 40+ แบบให้เลือก เราสามารถดูรูปแบบกราฟต่าง ๆ ได้ที่ Function reference และดูตัวอย่างกราฟได้ที่ The R Gallery
.
นอกจากนี้ เรายังสามารถปรับแต่งหน้าตาของกราฟได้ด้วย parametre ต่าง ๆ ใน geom_*() เช่น:
| No. | Parametre | Description |
|---|---|---|
| 1 | color | สีขอบรูปทรง |
| 2 | fill | สีในรูปทรง |
| 3 | shape | รูปทรง |
| 4 | size | ขนาด |
| 5 | alpha | ความโปร่งใส |
Note:
- จะสังเกตว่า
parametreเหล่านี้ (เรียกว่า attribute) เหมือนกับparametresของaes() - ความแตกต่างอยู่ที่ parametre ใน
geom_*()จะคงที่ ในขณะที่ parametre ในaes()จะเปลี่ยนแปลงตามข้อมูล
.
ยกตัวอย่างเช่น
เปลี่ยนจุดข้อมูลให้เป็นกล่องสี่เหลี่ยมใส:
ggplot(penguins, aes(x = body_mass_g,
y = flipper_length_mm,
color = species)) +
geom_point(shape = 22)
ผลลัพธ์:

จะเห็นได้ว่า จุดข้อมูลเปลี่ยนเป็นสี่เหลี่ยมเหมือนกันหมด และไม่เปลี่ยนตามประเภทเพนกวิน เพราะเรากำหนด argument ใน geom_*()
.
Note: เราสามารถดู argument ของ shape และ parametres อื่น ๆ ได้ที่ Aesthetic specifications
🔧 More Customisations
จนถึงตอนนี้ เรารู้วิธีสร้างและปรับแต่งกราฟเบื้องต้นกันแล้ว
เรามาดู 3 วิธีในการปรับแต่งเพิ่มเติม เพื่อให้กราฟของเราดูเป็นมืออาชีพกัน:
- Theme
- Text
- Facet
.
🖼️ Theme
Theme ใน ggplot2 หมายถึง หน้าตากราฟที่ไม่เกี่ยวข้องกับข้อมูล เช่น สีแกน x และ y และสีพื้นหลัง
เราสามารถปรับ theme ได้โดยการเรียกใช้ theme_*() เช่น:
ggplot(penguins, aes(x = body_mass_g,
y = flipper_length_mm,
color = species)) +
geom_point() +
# Apply classic theme
theme_classic()
ผลลัพธ์:

จะเห็นว่า กราฟของเราดูสะอาดตามากขึ้น เมื่อไม่มี gridline
.
Note:
ggplot2 มี built-in themes ให้เลือกใช้ 5 แบบหลัก ได้แก่:
| No. | Theme | Background | Gridline |
|---|---|---|---|
| 1 | theme_gray() | สีเทา | สีขาว |
| 2 | theme_classic() | สีขาว | None |
| 3 | theme_bw() | สีขาว | สีเทา |
| 4 | theme_light() | สีขาว | สีเทา |
| 5 | theme_dark() | สีเทาเข้ม | สีเทาอ่อน |
theme_gray()เป็น default theme ของggplot2- ดู built-in themes อื่น ๆ ได้ที่ Complete themes
- เราสามารถเลือกใช้ themes จาก packages อื่นใน R ได้ เช่น ggthemes และ bbcplot
.
🔤 Text
ข้อความเป็นส่วนสำคัญของกราฟ ซึ่งสามารถช่วยให้คนที่ดูกราฟของเราเข้าใจกราฟได้ง่ายขึ้น
ใน ggplot2 เราสามารถปรับแต่งข้อความได้ 3 วิธี:
| No. | Syntax | Description |
|---|---|---|
| 1 | theme() | กำหนดขนาด และ typeface ของข้อความ |
| 2 | labs() | เพิ่มชื่อกราฟ ชื่อแกน x, y และ legend |
| 3 | annotate() | เพิ่มโน้ตในกราฟ |
ตัวอย่างเช่น:
ggplot(penguins, aes(x = body_mass_g,
y = flipper_length_mm,
color = species)) +
geom_point() +
theme_classic() +
# Adjust text size
theme(plot.title = element_text(size = 16, face = "bold"),
axis.title.x = element_text(size = 14),
axis.title.y = element_text(size = 14),
legend.title = element_text(size = 14),
legend.text = element_text(size = 12),
strip.text = element_text(size = 14, face = "bold")) +
# Add a title, labels, and a legend
labs(title = "Penguin Body Mass vs. Flipper Length",
x = "Body Mass (g)",
y = "Flipper Length (mm)",
color = "Penguin Species") +
# Add annotation
annotate("text",
x = 3000,
y = 225,
label = "Larger penguins tend to \\n have longer flippers",
size = 5,
color = "gray",
hjust = 0)
ผลลัพธ์:

จะเห็นได้ว่า กราฟของเรามีชื่อกราฟ คำประกอบแกน x, y และ legend รวมทั้งโน้ตที่ช่วยในการอ่านกราฟ “Larger penguins tend to have longer flippers” เพิ่มขึ้นมา
.
✌️ Facet
ในบางครั้ง เราอาจต้องการสร้างกราฟย่อย ซึ่งเราสามารถทำได้ด้วย 2 คำสั่งใน ggplot2:
| No. | Function | Dividing Factor |
|---|---|---|
| 1 | facet_wrap() | 1 categorical variable |
| 2 | facet_grid() | 2 categorical variables |
.
ตัวอย่างเช่น
ใช้ facet_wrap() แบ่งกราฟตามชนิดเพนกวิน:
ggplot(penguins, aes(x = body_mass_g,
y = flipper_length_mm,
color = species)) +
geom_point() +
theme_bw() +
# Use facet_wrap()
facet_wrap(~species)
ผลลัพธ์:

.
ใช้ facet_grid() แบ่งตามเพศและชนิดของเพนกวิน:
ggplot(penguins, aes(x = body_mass_g,
y = flipper_length_mm,
color = species)) +
geom_point() +
theme_bw() +
# Use facet_grid()
facet_grid(sex~species)
ผลลัพธ์:

Note: ในกราฟ เรามี row ที่ 3 (NA) เพราะบาง record ไม่มีข้อมูลเพศของเพนกวิน
🔥 Summary
ในบทความนี้ เราได้เรียนรู้วิธีใช้ ggplot2 เพื่อสร้างกราฟอย่างมืออาชีพ
- Syntax ของ
ggplot2 - การตั้งค่า aesthetics
xy- Aesthetics อื่น ๆ
- การตั้งค่า geom
geom_*()- paramatres ของ
geom_*()
- การปรับแต่งกราฟ
- Theme
- Text
- Facet
📚 Learn More About ggplot2
.
😺 GitHub
สำหรับผู้ที่สนใจ สามารถดู code ทั้งหมดในบทความนี้ได้ที่ GitHub
.
📖 Read More About ggplot2
.
📰 Cheat Sheets
📃 References
Articles:
- ggplot2
- รู้จักกับ ggplot2 แพ็คเกต Data Viz ที่ดีที่สุดของ R
- Graphics with ggplot2
- Aesthetic specifications
- What is aesthetics and attributes in ggplot’s world?
- Colour related aesthetics: colour, fill, and alpha
- Summary of Geoms & Grammatical Structure of ggplot()
- Wrap a 1d ribbon of panels into 2d
- Lay out panels in a grid
Courses
✅ R Book for Psychologists: หนังสือภาษา R สำหรับนักจิตวิทยา
📕 ขอฝากหนังสือเล่มแรกในชีวิตด้วยนะครับ 😆
🙋 ใครที่กำลังเรียนจิตวิทยาหรือทำงานสายจิตวิทยา และเบื่อที่ต้องใช้ software ราคาแพงอย่าง SPSS และ Excel เพื่อทำข้อมูล
💪 ผมขอแนะนำ R Book for Psychologists หนังสือสอนใช้ภาษา R เพื่อการวิเคราะห์ข้อมูลทางจิตวิทยา ที่เขียนมาเพื่อนักจิตวิทยาที่ไม่เคยมีประสบการณ์เขียน code มาก่อน
ในหนังสือ เราจะปูพื้นฐานภาษา R และพาไปดูวิธีวิเคราะห์สถิติที่ใช้บ่อยกัน เช่น:
- Correlation
- t-tests
- ANOVA
- Reliability
- Factor analysis
🚀 เมื่ออ่านและทำตามตัวอย่างใน R Book for Psychologists ทุกคนจะไม่ต้องพึง SPSS และ Excel ในการทำงานอีกต่อไป และสามารถวิเคราะห์ข้อมูลด้วยตัวเองได้ด้วยความมั่นใจ
แล้วทุกคนจะแปลกใจว่า ทำไมภาษา R ง่ายขนาดนี้ 🙂↕️
👉 สนใจดูรายละเอียดหนังสือได้ที่ meb:
































