Iris là 1 bộ số liệu đặc biệt. Bộ số liệu này rất được ưa chuộng cho việc thực hành thăm dò dữ liệu và vẽ đồ thị, có lẽ do cấu trúc đặc biệt của nó. Bộ số liệu này có 4 biến kiểu số liên tục, tương quan tuyến tính với nhau và có quan hệ chặt chẽ với biến thứ 5 là 1 biến phân nhóm gồm 3 bậc giá trị. Bản thân 4 biến kiểu số này có thể được kết hợp bằng hàm gather( ) để tạo ra 1 biến phân nhóm nữa.

Như vậy, từ data iris bạn có thể dựng hầu hết các kiểu đồ thị trong ggplot.

Nhưng trong bài này chúng ta không nhắm tới việc thử nghiệm từng đồ thị đơn lẻ, mà mục tiêu là ghép nhiều đồ thị lại với nhau để cung cấp một lượng thông tin nhiều hơn, tổng quát hơn so với khả năng truyền tải của đồ thị 2 chiều quy ước :

Đầu tiên, mình sẽ tạo ra một phong cách trình bày và màu sắc riêng trong ggplot

library(tidyverse)
library(gridExtra)
library(GGally)

data(iris)

my_theme <- function(base_size = 8, base_family = "sans"){
  theme_minimal(base_size = base_size, base_family = base_family) +
    theme(
      axis.text = element_text(size =7),
      axis.text.x = element_text(angle = 0, vjust = 0.5, hjust = 0.5),
      axis.title = element_text(size = 10),
      panel.grid.major = element_line(color = "grey"),
      panel.grid.minor = element_blank(),
      panel.background = element_rect(fill = "white"),
      strip.background = element_rect(fill = "#660033", color = "#660033", size =0.5),
      strip.text = element_text(face = "bold", size = 8, color = "white"),
      legend.position = "bottom",
      legend.justification = "center",
      legend.background = element_blank(),
      panel.border = element_rect(color = "grey30", fill = NA, size = 0.5)
    )
}
theme_set(my_theme())

mycolors=c("#ff6666","#33ccff","#cc66ff","#ffcc00","#ace600")

Cách 1: Sử dụng các hàm facet trong ggplot2

Hàm facet_grid và facet_wrap trong ggplot2 rất hữu ích khi bạn muốn trình bày thông tin theo tổ chức phân nhóm (facet_wrap), hoặc tổ hợp giữa nhiều phân nhóm (facet_grid). Một điểm cần chú ý khi sử dụng hàm facet() đó là nhớ mô tả tùy chỉnh scales cho x và y. Tùy chỉnh số column chỉ dùng cho hàm facet_wrap.

iris%>%gather(Sepal.Length:Petal.Width,key="Metric",value="Value")%>%ggplot(aes(x=Metric,y=Value,fill=Metric))+geom_boxplot()+facet_grid(Species~.,scales="free")+scale_fill_manual(values=mycolors)+coord_flip()

iris%>%gather(Sepal.Length:Petal.Width,key="Metric",value="Value")%>%ggplot(aes(x=Metric,y=Value,fill=Metric))+geom_bar(stat="identity",alpha=0.7)+facet_grid(Species~.,scales="free")+scale_fill_manual(values=mycolors)+coord_flip()

iris%>%gather(Sepal.Length:Petal.Width,key="Metric",value="Value")%>%ggplot(aes(x=Metric,y=Value,fill=Metric,color=Metric))+stat_summary(fun.ymin=min,fun.ymax=max,fun.y="median",shape=21,size=1)+facet_grid(Species~.)+coord_flip()+scale_fill_manual(values=mycolors) +
  scale_color_manual(values=mycolors)

iris%>%gather(Sepal.Length:Petal.Width,key="Metric",value="Value")%>%ggplot(aes(x=Value,fill=Species))+geom_density(alpha=0.7)+facet_wrap(~Metric,scales="free")+scale_fill_manual(values=mycolors)+geom_rug(aes(color=Species))

iris%>%gather(Sepal.Length:Petal.Width,key="Metric",value="Value")%>%ggplot(aes(x=Value,fill=Species))+geom_histogram(alpha=0.7,color="black")+facet_grid(Species~Metric,scales="free")+scale_fill_manual(values=mycolors)

Cách 2: Sử dụng package gridExtra

Package gridExtra cho phép bạn ghép nhiều đồ thị ggplot với nhau theo cấu trúc ma trận. Giải pháp này rất mềm dẻo khi bạn có trong tay nhiều đồ thị rời rạc. Nhược điểm duy nhất của nó là mỗi đồ thị không nên chứa legend, nếu không hiệu ứng trình bày sẽ không được tối ưu

sepal=iris%>%ggplot(aes(x=Sepal.Width,y=Sepal.Length))+stat_density2d(geom="polygon",aes(fill=Species,alpha = ..level..),show.legend = F)+geom_point(shape=21,aes(fill=Species),size=3,color="black",show.legend = F)+geom_rug(aes(color=Species))+scale_fill_manual(values=mycolors)+scale_color_manual(values=mycolors)

petal=iris%>%ggplot(aes(x=Petal.Width,y=Petal.Length))+stat_density2d(geom="polygon",aes(fill=Species,alpha = ..level..),show.legend = F)+geom_point(shape=21,aes(fill=Species),size=3,color="black",show.legend = F)+geom_rug(aes(color=Species))+scale_fill_manual(values=mycolors)+scale_color_manual(values=mycolors)

grid.arrange(sepal,petal,ncol=2)

Cách 3: Sử dụng package GGally

Package GGally hơi khó sử dụng, vì nó đòi hỏi bạn phải viết function cho 3 phân vùng trong ma trận, bao gồm góc trên, góc dưới, và đường chéo. Tuy nhiên hiệu ứng mà nó mang lại cực kì ấn tượng như sau :

plotfuncLow <- function(data,mapping){
  p <- ggplot(data = data,mapping=mapping)+geom_point(aes(fill=iris$Species),shape=21,color="black")+stat_density2d(geom="polygon",aes(fill=iris$Species,alpha = ..level..))+scale_fill_manual(values=mycolors)+geom_rug(aes(color=iris$Species))+scale_color_manual(values=mycolors)
  p
}

plotfuncmid <- function(data,mapping){
  p <- ggplot(data = data,mapping=mapping)+geom_density(aes(fill=iris$Species),alpha=0.5,color="black")+scale_fill_manual(values=mycolors)+geom_rug(aes(color=iris$Species))+scale_color_manual(values=mycolors)
  p
}

library(GGally)

ggpairs(iris,columns=1:4,lower=list(continuous=plotfuncLow),diag=list(continuous=plotfuncmid))

Bài thực hành hôm nay đến đây là hết. Chúc các bạn cuối tuần vui vẻ.