Trực quan hoá dữ liệu sử dụng dataset Iris trong R

Gọi các packages liên quan

library(knitr)
library(readr)
library(psych)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5     ✓ dplyr   1.0.7
## ✓ tibble  3.1.6     ✓ stringr 1.4.0
## ✓ tidyr   1.1.4     ✓ forcats 0.5.1
## ✓ purrr   0.3.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x ggplot2::%+%()   masks psych::%+%()
## x ggplot2::alpha() masks psych::alpha()
## x dplyr::filter()  masks stats::filter()
## x dplyr::lag()     masks stats::lag()
library(ggplot2)
library(dplyr)
library(ggridges)
  • Có thể dùng lệnh require() để gọi các packages

Load dữ liệu

data(iris)
attach(iris)

Kiểm tra dữ liệu

Chúng ta có thể xem 6 hàng đầu tiên của tập dữ liệu bằng cách sử dụng hàm head ():

head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa

Chúng ta có thể sử dụng hàm Summary () để summary từng biến trong tập dữ liệu:

summary(iris)
##   Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   
##  Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100  
##  1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300  
##  Median :5.800   Median :3.000   Median :4.350   Median :1.300  
##  Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199  
##  3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800  
##  Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  
##        Species  
##  setosa    :50  
##  versicolor:50  
##  virginica :50  
##                 
##                 
## 

Đối với các biến dạng số (numeric) chúng ta thấy các thông tin sau:

  • Min: Giá trị nhỏ nhất.
  • 1st Qu: Bách phân vị 25%.
  • Median: Giá trị trung vị.
  • Mean: Giá trị trung bình.
  • 3rd Qu: Bách phân vị 75%.
  • Max: Giá trị lớn nhất.

Kiểm tra số dòng và số cột của dataframe băng hàm dim()

dim(iris)      
## [1] 150   5

Chúng ta cũng có thể sử dụng hàm names () để hiển thị các tên cột của data frame:

names(iris)
## [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"

Mô tả dữ liệu theo biến phân nhóm

describe.by(Sepal.Length, Species)
## Warning: describe.by is deprecated. Please use the describeBy function
## 
##  Descriptive statistics by group 
## group: setosa
##    vars  n mean   sd median trimmed mad min max range skew kurtosis   se
## X1    1 50 5.01 0.35      5       5 0.3 4.3 5.8   1.5 0.11    -0.45 0.05
## ------------------------------------------------------------ 
## group: versicolor
##    vars  n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 50 5.94 0.52    5.9    5.94 0.52 4.9   7   2.1  0.1    -0.69 0.07
## ------------------------------------------------------------ 
## group: virginica
##    vars  n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 50 6.59 0.64    6.5    6.57 0.59 4.9 7.9     3 0.11     -0.2 0.09

Trực quan hoá dữ liệu

Dùng hàm hist() để tạo biểu đồ cho một biến nhất định:

hist(iris$Sepal.Length,
     col='steelblue',
     main='Histogram',
     xlab='Length',
     ylab='Frequency')

Dùng hàm plot() để tạo biểu đồ scatterplot theo từng cặp biến bất kỳ:

plot(iris$Sepal.Width, iris$Sepal.Length,
     col=c("steelblue", "Red", "Green"),
     main='Scatterplot',
     xlab='Sepal Width',
     ylab='Sepal Length',
     pch=19)

Tuỳ biến màu sắc với packages viridis

library(viridis) # Cần cài packages viridis trước
## Loading required package: viridisLite
cols = viridis(3) # 3 là số đối tượng cần gán màu
# Gán màu vào vd trên
plot(iris$Sepal.Width, iris$Sepal.Length,
     col=cols,
     main='Scatterplot',
     xlab='Sepal Width',
     ylab='Sepal Length',
     pch=19)

Dùnghàm boxplot() để tạo biểu đồ hộp theo nhóm

boxplot(Sepal.Length~Species,
        data=iris,
        main='Sepal Length by Species',
        xlab='Species',
        ylab='Sepal Length',
        col=c("Blue", "Red", "Green"),
        border='black')

Vẽ biểu đồ với packages ggplot2

define a canvas

ggplot(data = iris)

Gán dữ liệu lên trục x và y

ggplot(data = iris ) + 
  aes(x = Petal.Length, y = Petal.Width)  

Trực quan dữ liệu dưới dạng điểm bằng hàm geom_point()

ggplot(data = iris) +
  aes(x = Petal.Length, y = Petal.Width) + 
  geom_point()

Thay đổi màu sắc và ký hiệu

ggplot(data = iris) +
  aes(x = Petal.Length, y = Petal.Width) + 
  geom_point( aes(color = Species, shape = Species) )

Thêm đường xu hướng

ggplot(data = iris) +
  aes(x = Petal.Length, y = Petal.Width) + 
  geom_point( aes(color = Species, shape = Species) ) +
  geom_smooth(method =lm)
## `geom_smooth()` using formula 'y ~ x'

Vẽ biểu đồ hộp

ggplot(data = iris) +
  aes(x = Species, y = Sepal.Length, color = Species) +
  geom_boxplot()

Biểu diễn thêm giá trị các biến lên biểu đồ

ggplot(data = iris) +
  aes(x = Species, y = Sepal.Length, color = Species) +
  geom_boxplot() +
  geom_jitter(position = position_jitter(0.2))

Vẽ density plot chiều dài cánh hoa theo loài

ggplot(data = iris) +
  aes(x = Petal.Length, fill = Species) + 
  geom_density( alpha = 0.3)

Vẽ cùng lúc nhiều biểu đồ density plot chiều dài cánh hoa theo loài bằng hàm facet_wrap()

ggplot(data = iris) +
  aes(x = Petal.Length, fill = Species) + 
  geom_density( alpha = 0.3) +
  facet_wrap(~ Species, nrow = 3)  

Biểu diễn dưới dạng biểu đồ Ridgeline bằng packages ggridges

ggplot(iris, aes(x = Petal.Length, y = Species, fill = Species)) +
  geom_density_ridges() +
  theme_ridges() +                                 # No color on backgroud
  theme(legend.position = "none",                  # No show legend
        axis.title.x = element_text(hjust = 0.5),  # x axis title in the center
        axis.title.y = element_text(hjust = 0.5))  # y axis title in the center
## Picking joint bandwidth of 0.155


Hết: Ngọc Nguyễn