Các bạn tham khảo source code từ trang này sau đó render lại đồ thị trên máy tính để hiểu cách code của tác giả.

Nguồn: https://github.com/clauswilke/dataviz

Load package

## Load đủ package này sẽ run được code
library(readr)
# library(here)
library(dplyr)
library(tidyr)
library(lubridate)
library(tidyverse)
library(ggplot2)
library(colorblindr)
# remotes::install_github("wilkelab/cowplot")
# install.packages("colorspace", repos = "http://R-Forge.R-project.org")
# remotes::install_github("clauswilke/colorblindr")
library(pkgsimon)
# install.packages("remotes")
# remotes::install_github("SimonCoulombe/pkgsimon")
library(dviz.supp)
# install.packages("remotes")
# remotes::install_github("clauswilke/dviz.supp")

Dữ liệu này là kết quả theo dõi nhiệt độ ở các bang trong nước Mỹ

Nguồn: https://github.com/clauswilke/dviz.supp/tree/master/data-raw/ncdc_normals

## import dataset
ncdc_normals <- read_fwf("dly-tavg-normal.txt",
                         fwf_positions(c(1, 13, 19 + 7*(0:30)),
                                       c(11, 14, 24 + 7*(0:30)),
                                       c("station_id", "month", paste0("day", 1:31)))) %>%
  gather(day, value, -station_id, -month) %>%
  extract(day, "day", regex = "([[:digit:]]+)") %>%
  extract(value, c("temperature", "flag"), regex = "(-*[[:digit:]]+)([[:alpha:]]*)") %>%
  mutate(temperature = ifelse(temperature == "-8888", NA, as.numeric(temperature))/10) %>%
  na.omit() %>%
  mutate(date = ymd(paste("0000", month, day, sep = '-'))) %>%
  arrange(station_id, as.numeric(month), as.numeric(day))

## kiểm tra dataset
dim(ncdc_normals)
## [1] 2745366       6
str(ncdc_normals)
## tibble [2,745,366 × 6] (S3: tbl_df/tbl/data.frame)
##  $ station_id : chr [1:2745366] "AQW00061705" "AQW00061705" "AQW00061705" "AQW00061705" ...
##  $ month      : chr [1:2745366] "01" "01" "01" "01" ...
##  $ day        : chr [1:2745366] "1" "2" "3" "4" ...
##  $ temperature: num [1:2745366] 82.4 82.4 82.4 82.4 82.4 82.4 82.4 82.4 82.4 82.4 ...
##  $ flag       : chr [1:2745366] "C" "C" "C" "C" ...
##  $ date       : Date[1:2745366], format: "0000-01-01" "0000-01-02" ...
##  - attr(*, "na.action")= 'omit' Named int [1:45006] 2610350 2610362 2610374 2610386 2610398 2610410 2610422 2610434 2610446 2610458 ...
##   ..- attr(*, "names")= chr [1:45006] "2610350" "2610362" "2610374" "2610386" ...

Sau khi trích xuất dữ liệu, sẽ vẽ đồ thị line chart

Nguồn: https://clauswilke.com/dataviz/aesthetic-mapping.html#aesthetics-and-types-of-data

temps_long <- filter(ncdc_normals,
                     station_id %in% c(
                         "USW00014819", # Chicago, IL 60638
                         #"USC00516128", # Honolulu, HI 96813
                         #"USW00027502", # Barrow, AK 99723, coldest point in the US
                         "USC00042319", # Death Valley, CA 92328 hottest point in the US
                         "USW00093107", # San Diego, CA 92145
                         #"USC00427606"  # Salt Lake City, UT 84103
                         "USW00012918" # Houston, TX 77061
                     )) %>%
    mutate(location = fct_recode(factor(station_id),
                                 "Chicago" = "USW00014819",
                                 #"Honolulu, HI" = "USC00516128",
                                 #"Barrow, AK" = "USW00027502",
                                 "Death Valley" = "USC00042319",
                                 "San Diego" = "USW00093107",
                                 #"Salt Lake City, UT" = "USC00427606",
                                 "Houston" = "USW00012918")) %>%
    mutate(location = factor(location, levels = c("Death Valley", "Houston", "San Diego", "Chicago")))

ggplot(temps_long, aes(x = date, y = temperature, color = location)) +
    geom_line(size = 1) +
    scale_x_date(name = "month", limits = c(ymd("0000-01-01"), ymd("0001-01-04")),
                 breaks = c(ymd("0000-01-01"), ymd("0000-04-01"), ymd("0000-07-01"),
                            ymd("0000-10-01"), ymd("0001-01-01")),
                 labels = c("Jan", "Apr", "Jul", "Oct", "Jan"), expand = c(1/366, 0)) + 
    scale_y_continuous(limits = c(19.9, 107),
                       breaks = seq(20, 100, by = 20),
                       name = "temperature (°F)") +
    scale_color_OkabeIto(order = c(1:3, 7), name = NULL) +
    theme_dviz_grid() +
    theme(legend.title.align = 0.5)

Nếu tính trung bình nhiệt độ thì có thể vẽ dạng đồ thị heatmap để mang thông điệp ấn tượng hơn về chênh lệch nhiệt độ giữa các bang.

Nguồn: https://github.com/clauswilke/dataviz/blob/master/aesthetic_mapping.Rmd

month_names <- c("01" = "Jan", "02" = "Feb", "03" = "Mar", "04" = "Apr", "05" = "May", "06" = "Jun",
                 "07" = "Jul", "08" = "Aug", "09" = "Sep", "10" = "Oct", "11" = "Nov", "12" = "Dec")


mean_temps <- temps_long %>%
    group_by(location, month) %>%
    summarize(mean = mean(temperature)) %>%
    ungroup() %>%
    mutate(month = month_names[month]) %>%
    mutate(month = factor(month, levels = unname(month_names)))

p <- ggplot(mean_temps, aes(x = month, y = location, fill = mean)) + 
    geom_tile(width = .95, height = 0.95) + 
    scale_fill_viridis_c(option = "B", begin = 0.15, end = 0.98,
                         name = "temperature (°F)") + 
    scale_y_discrete(name = NULL) +
    coord_fixed(expand = FALSE) +
    theme_dviz_open() +
    theme(axis.line = element_blank(),
          axis.ticks = element_blank(),
          #axis.text.y = element_text(size = 14),
          legend.title = element_text(size = 12)
    )

# fix legend (make it centered)
ggdraw(align_legend(p))

Sơ kết

Trên đây là hướng dẫn vẽ đồ thị từ source code. Để học R bài bản từ A đến Z, thân mời Bạn tham gia khóa học “HDSD R để xử lý dữ liệu” để có nền tảng vững chắc về R nhằm tự tay làm các câu chuyện dữ liệu của riêng mình!

ĐĂNG KÝ NGAY: https://www.tuhocr.com/register