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
leaf <- read_csv("leaf_characteristics.csv")
## Rows: 161 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): forest_type, elevation
## dbl (5): leaf_length_cm, leaf_width_cm, leaf_thickness_mm, leaf_area_cm2, ch...
##
## ℹ 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.
leaf
## # A tibble: 161 × 7
## leaf_length_cm leaf_width_cm leaf_thickness_mm leaf_area_cm2
## <dbl> <dbl> <dbl> <dbl>
## 1 8.9 2.9 0.38 18.9
## 2 9.1 3.7 0.34 25.4
## 3 10.6 3.4 0.32 23.8
## 4 10.5 4.7 0.32 35.7
## 5 11.5 4.7 0.34 36.4
## 6 11.9 4.1 0.38 33.3
## 7 10.2 4.1 0.28 32.7
## 8 10.3 4.3 0.32 31.5
## 9 9.3 3.5 0.37 22.4
## 10 9.4 3.5 0.27 23.9
## # ℹ 151 more rows
## # ℹ 3 more variables: chlorophyll_content <dbl>, forest_type <chr>,
## # elevation <chr>
leaf %>%
ggplot(aes(x = leaf_length_cm, y = leaf_width_cm)) +
geom_point()
cor(leaf$leaf_length_cm, leaf$leaf_width_cm)
## [1] 0.6587472
cor.test(leaf$leaf_length_cm, leaf$leaf_width_cm)
##
## Pearson's product-moment correlation
##
## data: leaf$leaf_length_cm and leaf$leaf_width_cm
## t = 11.041, df = 159, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.5612600 0.7382051
## sample estimates:
## cor
## 0.6587472
จากการวิเคราะห์นี้ แสดงให้เห็นว่า ความยาวและความกว้างของใบมีสหสัมพันธ์ในเชิงบวกอย่างมีนัยสำคัญ (r = 0.66, p < 0.001)
เลือกลักษณะหนึ่งคู่จากตัวแปรที่เป็นตัวเลข (continuous variables) และวาดกราฟ หาค่าสหสัมพันธ์ p-value และเขียนรายงานผล
ตัวเลือกของตัวแปร
leaf_length_cm
leaf_width_cm
leaf_thickness_mm
leaf_area_cm2
chlorophyll_content
colnames(leaf)
## [1] "leaf_length_cm" "leaf_width_cm" "leaf_thickness_mm"
## [4] "leaf_area_cm2" "chlorophyll_content" "forest_type"
## [7] "elevation"
เราสามารถดู correlation ของทุกตัวแปรพร้อม ๆ กันได้ โดยการทำ correlation matrix
leaf2 <- leaf %>%
select(leaf_length_cm, leaf_width_cm, leaf_thickness_mm)
leaf2
## # A tibble: 161 × 3
## leaf_length_cm leaf_width_cm leaf_thickness_mm
## <dbl> <dbl> <dbl>
## 1 8.9 2.9 0.38
## 2 9.1 3.7 0.34
## 3 10.6 3.4 0.32
## 4 10.5 4.7 0.32
## 5 11.5 4.7 0.34
## 6 11.9 4.1 0.38
## 7 10.2 4.1 0.28
## 8 10.3 4.3 0.32
## 9 9.3 3.5 0.37
## 10 9.4 3.5 0.27
## # ℹ 151 more rows
cor_matrix <- cor(leaf2)
cor_matrix
## leaf_length_cm leaf_width_cm leaf_thickness_mm
## leaf_length_cm 1.0000000 0.6587472 -0.4927972
## leaf_width_cm 0.6587472 1.0000000 -0.5833480
## leaf_thickness_mm -0.4927972 -0.5833480 1.0000000
library(GGally)
ggpairs(leaf2)
library(corrplot)
## corrplot 0.95 loaded
corrplot(cor_matrix)
สามารถปรับสี และ รูปแบบได้ดังนี้
corrplot(cor_matrix, method = "number", type = "upper", col = COL2("BrBG"))
ตัวเลือกสีใน COL2 มีดังต่อไปนี้
ให้เลือกลักษณะมา 4 ลักษณะจากทั้งหมด แล้วให้แสดงผล correlation matrix ตามที่ตนเองชอบ โดยทำขั้นตอนดังนี้