library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.6
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.1 ✔ tibble 3.3.0
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.2.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
kratin <- read_csv("kratin.csv")
## Rows: 32 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): species, province
## dbl (3): Girth, Height, Volume
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
kratin
## # A tibble: 32 × 5
## Girth Height Volume species province
## <dbl> <dbl> <dbl> <chr> <chr>
## 1 8.3 70 10.3 Narong Chiang Mai
## 2 8.6 65 10.3 Narong Chiang Mai
## 3 8.8 63 10.2 Narong Chiang Mai
## 4 10.5 72 16.4 Narong Chiang Mai
## 5 10.7 81 18.8 Narong Chiang Mai
## 6 10.8 83 19.7 Narong Chiang Mai
## 7 11 66 15.6 Narong Chiang Mai
## 8 11 75 18.2 Narong Chiang Mai
## 9 11.1 80 22.6 Narong Lamphun
## 10 11.2 75 19.9 Narong Lamphun
## # ℹ 22 more rows
กราฟจุด (scatter plot) เหมาะกับการแสดงความสัมพันธ์ของข้อมูลที่เป็นตัวแปรต่อเนื่อง (continuous) 2 ตัว เช่นความสัมพันธ์ระหว่างความโต (girth) กับ ปริมาตร (volume) ของต้นไม้
ในโค้ดด้านล่าง
บรรทัดที่ 1: ชื่อ object ของข้อมูลเรา ตามด้วย %>% เพื่อส่งต่อข้อมูล
บรรทัดที่ 2: เราจะกำหนดตัวแปรที่อยู่ในแกน x, y ในวงเล็บของ
aes() ที่อยู่ใน function ggplot() อีกทีหนึ่ง
และตามด้วยเครื่องหมาย + เพื่อเพิ่ม layer ถัดไป
บรรทัดที่ 3: geom_point() เป็นการกำหนดว่าเราจะวาดอะไร
ในกรณีนี้ เราจะวาด “จุด” (point)
kratin %>%
ggplot(aes(x = Girth, y = Volume)) +
geom_point()
หากเราต้องการปรับ “ทุกจุด” ให้มีหน้าตาแบบเดียวกันทั้งหมดให้ใส่รายละเอียด (argument)
เข้าไปในวงเล็บของ geom_point() จะเป็นการปรับลุคทั้งหมดของทุกจุด
ตัวอย่างของ argument เช่น
color = เป็นการระบุสี
size = ขนาดของจุด
alpha = ความทึบแสง (0 = ใสเลย -> 1 =
ทึบสุด)
kratin %>%
ggplot(aes(x = Girth, y = Volume)) +
geom_point(color = "blue", size = 3, alpha = 0.1)
แต่เราต้องการให้แต่ละจุด แตกต่างกันตามข้อมูลในตาราง
เราจะใส่ argument (เช่น color = ในวงเล็บของ
aes())
kratin %>%
ggplot(aes(x = Girth, y = Volume, color = species)) +
geom_point()
ในกรณีที่เรากำหนดสีให้แตกต่างกันใน aes() ไปแล้ว
เราสามารถกำหนดชื่อสีเองได้โดยเพิ่ม layer เข้าไปใช้ function
scale_color_manual()
kratin %>%
ggplot(aes(x = Girth, y = Volume, color = species)) +
geom_point() +
scale_color_manual(values = c("blue","orange"))
วาดกราฟที่แสดงว่า Volume เพิ่มตาม Height (x = Height, y = Volume) และแต่ละจุดเปลี่ยนสีตามจังหวัด และปรับขนาดจุดให้อ่านง่ายขึ้น
หากเรามีตัวแปรต้น (แกน x / สิ่งที่เราคิดว่าเป็นตัวที่ทำให้ต่าง) เป็นข้อมูลแบบ discrete (แบบกลุ่ม ๆ) และตัวแปรตาม (แกน y / สิ่งที่เราวัดเพื่อดูว่าต่างกันไหม) เป็นตัวแปรต่อเนื่อง (continuous) เรามักจะวาด “boxplot” หรือ “box-and-whisker plot” ที่หน้าตาประมาณนี้
kratin %>%
ggplot(aes(x = species, y = Girth)) +
geom_boxplot()
ตรงกลางกล่องคือค่ามัธยฐาน (ค่ากลาง) ขอบกล่องจะเป็น Quartile 1 และ Quartile 3 (25% และ 75%) ของข้อมูล ส่วน หนวด (whisker) จะเป็นขอบของกล่อง - หรือ + ด้วย 1.5 x IQR (Interquantile range) ซึ่งเป็นช่วงที่กว้างพอที่ ควร จะครอบคลุมค่าทั้งหมดในกลุ่มนั้น ถ้าข้อมูลมีการกระจายที่ไม่ประหลาดมาก แต่ถ้ามีข้อมูลนอกเหนือช่วงนี้ จะแสดงเป็นจุดด้านนอก ที่เราเรียกว่า outliers
วิธีการปรับสีของกราฟ เหมือนกันของกราฟจุดด้านบนเลย แต่ boxplot ต่างนิดนึงตรงที่ว่า
color = เป็นสีขอบของกล่อง
fill = เป็นสีที่อยู่ในกล่องอีกที
kratin %>%
ggplot(aes(x = species, y = Girth)) +
geom_boxplot(color = "red", fill = "grey")
ถ้าปรับตามข้อมูลในตารางก็ไปใส่ใน aes() เช่นเดิม
kratin %>%
ggplot(aes(x = species, y = Girth, fill = species)) +
geom_boxplot()
ยังสามารถเลือกสีในกล่องได้เหมือนเดิม แต่คราวนี้จะต้องใข้
scale_fill_manual() แทนครับ
kratin %>%
ggplot(aes(x = species, y = Girth, fill = species)) +
geom_boxplot() +
scale_fill_manual(values = c("#331B3F", "#ACC7B4"))
วาดกราฟเปรียบเทียบ ความสูง (Height) ระหว่าง 2 จังหวัด (province) ให้ปรับสีให้สวยงามด้วย
kratin %>%
ggplot(aes(x = Girth, y = Volume)) +
geom_point() +
labs(title = "Kratin Tree Character", x = "Girth (cm)", y = "Volume (cubic m)")
theme_xx เป็นการกำหนด look โดยรวมของกราฟ
และปรับขนาดฟ้อนต์โดยรวมผ่าน argument base_size =โดยสามารถเลือก
theme ต่าง ๆ ได้ตามนี้ https://ggplot2.tidyverse.org/reference/ggtheme.html
kratin %>%
ggplot(aes(x = Girth, y = Volume)) +
geom_point() +
theme_bw(base_size = 15)
หากเราต้องการวาดกราฟเดียวกัน แต่สำหรับหลาย ๆ ปัจจัย เช่น ทั้ง 2 จังหวัดพร้อม ๆ กัน
เราสามารถใช้ facet_grid() เพื่อแบ่งกราฟเป็นย่อย ๆ (facet)
ตามสั่งได้
kratin %>%
ggplot(aes(x = Girth, y = Volume)) +
geom_point() +
facet_grid(~ province)
ด้วยความเป็นทุกอย่างซ้อนกันเป็นชั้น ๆ (layer) เราสามารถนำทุกอย่างที่เรียนมา มารวมกันได้เลย
kratin %>%
ggplot(aes(x = Girth, y = Volume)) +
geom_point() +
labs(title = "Kratin Tree Character", x = "Girth (cm)", y = "Volume (cubic m)") +
facet_grid(~ province) +
theme_bw(base_size = 15)
วาดกราฟความสัมพันธ์ระหว่าง Height กับ Girth โดยแบ่งกราฟย่อยเป็นของแต่ละชนิด (species)