Để có dataset minh họa việc plotting line chart cho trường hợp nhiều biến, mình sẽ subset dữ liệu lúa gạo và dân số cho Việt Nam và Thái Lan từ database FAOSTAT và World Bank.

library(ggpubr)
library(ggplot2)
library(tidyr)
library(reshape2)

rice_full <- readRDS(file = "rice_ready.rds")

vietnam_rice <- rice_full |> subset(area == "Viet Nam")

thailand_rice <- rice_full |> subset(area == "Thailand")

vietnam_rice$yield <- vietnam_rice$production / vietnam_rice$area_harvested

vietnam_rice$production / 1000000 -> vietnam_rice$production 

vietnam_rice$area_harvested / 1000000 -> vietnam_rice$area_harvested

thailand_rice$yield <- thailand_rice$production / thailand_rice$area_harvested

thailand_rice$production / 1000000 -> thailand_rice$production 

thailand_rice$area_harvested / 1000000 -> thailand_rice$area_harvested

########
# install.packages('WDI')
# library(WDI)
# dat <- WDI(indicator = 'SP.POP.TOTL', country = c('all'))
# saveRDS(dat, file = "population_all.rds")

readRDS(file = "population_all.rds") -> dat

###################

vietnam_population <- subset(dat, country == "Vietnam")

row.names(vietnam_population) <- NULL

vietnam_population_ok <- vietnam_population[-c(1, 63), c(1, 4, 5)]

thailand_population <- subset(dat, country == "Thailand")

thailand_population_ok <- thailand_population[-c(1, 63), c(1, 4, 5)]

#########################

vietnam_rice$population <- vietnam_population_ok$SP.POP.TOTL / 1000000

thailand_rice$population <- thailand_population_ok$SP.POP.TOTL / 1000000

rbind(vietnam_rice, thailand_rice) -> df_big

df_big[, c(1, 3, 4, 5, 6, 7)] -> df_big

row.names(df_big) <- NULL

Ta có dataset như sau. Khi bạn muốn vẽ tương tự mình thì cũng áp dụng cùng cấu trúc dataset này.

print(df_big)
##         area year production area_harvested    yield population
## 1   Viet Nam 2021   43.85273       7.219797 6.073956   97.46803
## 2   Viet Nam 2020   42.76500       7.222403 5.921159   96.64869
## 3   Viet Nam 2019   43.49549       7.451544 5.837111   95.77672
## 4   Viet Nam 2018   44.04625       7.570741 5.817958   94.91433
## 5   Viet Nam 2017   42.76368       7.708534 5.547577   94.03305
## 6   Viet Nam 2016   43.11201       7.734722 5.573828   93.12653
## 7   Viet Nam 2015   45.09056       7.828607 5.759717   92.19140
## 8   Viet Nam 2014   44.97421       7.816476 5.753770   91.23550
## 9   Viet Nam 2013   44.03910       7.902813 5.572585   90.26774
## 10  Viet Nam 2012   43.73760       7.761314 5.635334   89.30133
## 11  Viet Nam 2011   42.39834       7.655440 5.538329   88.34912
## 12  Viet Nam 2010   40.00560       7.489400 5.341630   87.41101
## 13  Viet Nam 2009   38.95020       7.437200 5.237213   86.48292
## 14  Viet Nam 2008   38.72980       7.400200 5.233615   85.59724
## 15  Viet Nam 2007   35.94270       7.207400 4.986916   84.76227
## 16  Viet Nam 2006   35.84950       7.324800 4.894263   83.95180
## 17  Viet Nam 2005   35.83290       7.329200 4.889060   83.14209
## 18  Viet Nam 2004   36.14890       7.445300 4.855264   82.31123
## 19  Viet Nam 2003   34.56880       7.452200 4.638738   81.47583
## 20  Viet Nam 2002   34.44720       7.504300 4.590328   80.64231
## 21  Viet Nam 2001   32.10840       7.492700 4.285291   79.81778
## 22  Viet Nam 2000   32.52950       7.666300 4.243181   79.00114
## 23  Viet Nam 1999   31.39380       7.653600 4.101834   78.12371
## 24  Viet Nam 1998   29.40814       7.350801 4.000672   77.12842
## 25  Viet Nam 1997   27.52390       7.099700 3.876769   76.05860
## 26  Viet Nam 1996   26.39670       7.003800 3.768911   74.94645
## 27  Viet Nam 1995   24.96370       6.765600 3.689798   73.75911
## 28  Viet Nam 1994   23.52830       6.598600 3.565650   72.50109
## 29  Viet Nam 1993   22.83660       6.559400 3.481507   71.17641
## 30  Viet Nam 1992   21.59030       6.475400 3.334204   69.78875
## 31  Viet Nam 1991   19.62190       6.302700 3.113254   68.35882
## 32  Viet Nam 1990   19.22510       6.042800 3.181489   66.91261
## 33  Viet Nam 1989   18.99630       5.911200 3.213612   65.46636
## 34  Viet Nam 1988   17.00000       5.740800 2.961260   64.03751
## 35  Viet Nam 1987   15.10260       5.603100 2.695401   62.63079
## 36  Viet Nam 1986   16.00290       5.703100 2.806000   61.22111
## 37  Viet Nam 1985   15.87480       5.718300 2.776140   59.81131
## 38  Viet Nam 1984   15.50560       5.675000 2.732264   58.40686
## 39  Viet Nam 1983   14.74330       5.611700 2.627243   57.01144
## 40  Viet Nam 1982   14.39020       5.711700 2.519425   55.63215
## 41  Viet Nam 1981   12.41520       5.651900 2.196642   54.28039
## 42  Viet Nam 1980   11.64740       5.600200 2.079819   52.96827
## 43  Viet Nam 1979   11.36290       5.485200 2.071556   51.83139
## 44  Viet Nam 1978    9.78990       5.462500 1.792201   50.70146
## 45  Viet Nam 1977   10.59710       5.468700 1.937773   49.41815
## 46  Viet Nam 1976   11.82720       5.297300 2.232685   48.16357
## 47  Viet Nam 1975   10.29360       4.855900 2.119813   46.96962
## 48  Viet Nam 1974   11.02329       5.111920 2.156389   45.89870
## 49  Viet Nam 1973   11.12500       5.030000 2.211730   44.89128
## 50  Viet Nam 1972   10.74820       4.900000 2.193510   43.90602
## 51  Viet Nam 1971   10.44700       4.692100 2.226508   42.91662
## 52  Viet Nam 1970   10.17330       4.724400 2.153353   41.92885
## 53  Viet Nam 1969    8.81500       4.930000 1.788032   41.01586
## 54  Viet Nam 1968    8.36615       4.893800 1.709541   40.14529
## 55  Viet Nam 1967    9.18840       4.795800 1.915926   39.28256
## 56  Viet Nam 1966    8.46350       4.681300 1.807938   38.38821
## 57  Viet Nam 1965    9.36970       4.826300 1.941384   37.46608
## 58  Viet Nam 1964    9.69703       4.987800 1.944150   36.50917
## 59  Viet Nam 1963    9.62267       4.496520 2.140026   35.52673
## 60  Viet Nam 1962    9.74704       4.888860 1.993725   34.53389
## 61  Viet Nam 1961    8.99740       4.744000 1.896585   33.62198
## 62  Thailand 2021   33.58200      11.244000 2.986660   71.60110
## 63  Thailand 2020   30.23102      10.401653 2.906367   71.47566
## 64  Thailand 2019   28.61795       9.812614 2.916445   71.30776
## 65  Thailand 2018   32.34811      10.647941 3.037969   71.12780
## 66  Thailand 2017   32.89890      10.719698 3.069014   70.89820
## 67  Thailand 2016   31.85700      10.734279 2.967782   70.60704
## 68  Thailand 2015   27.70219       9.717975 2.850614   70.29440
## 69  Thailand 2014   32.62016      10.664923 3.058640   69.96094
## 70  Thailand 2013   36.76228      11.684315 3.146293   69.57860
## 71  Thailand 2012   38.10019      11.956781 3.186492   69.15702
## 72  Thailand 2011   38.10272      11.956638 3.186742   68.71285
## 73  Thailand 2010   35.70295      11.932320 2.992121   68.27049
## 74  Thailand 2009   32.39786      11.141447 2.907868   67.81365
## 75  Thailand 2008   32.02299      10.683549 2.997411   67.32824
## 76  Thailand 2007   32.47703      10.668905 3.044083   66.82675
## 77  Thailand 2006   29.99060      10.165155 2.950334   66.31952
## 78  Thailand 2005   30.64825      10.224967 2.997393   65.82136
## 79  Thailand 2004   28.87398       9.992868 2.889458   65.31117
## 80  Thailand 2003   29.82027      10.163878 2.933946   64.77696
## 81  Thailand 2002   28.32114       9.653534 2.933758   64.22258
## 82  Thailand 2001   29.09991      10.125424 2.873945   63.64989
## 83  Thailand 2000   25.84388       9.891200 2.612815   63.06660
## 84  Thailand 1999   24.17141       9.969920 2.424434   62.44265
## 85  Thailand 1998   22.99841       9.511520 2.417954   61.74522
## 86  Thailand 1997   23.58008       9.912790 2.378753   60.98911
## 87  Thailand 1996   22.33164       9.267200 2.409750   60.21110
## 88  Thailand 1995   22.01548       9.112951 2.415845   59.42483
## 89  Thailand 1994   21.11071       8.975229 2.352109   58.61001
## 90  Thailand 1993   18.44726       9.000000 2.049696   57.77608
## 91  Thailand 1992   19.91730       9.159680 2.174454   56.93902
## 92  Thailand 1991   20.40000       9.052960 2.253407   56.09987
## 93  Thailand 1990   17.19322       8.791885 1.955578   55.22841
## 94  Thailand 1989   20.60101       9.879040 2.085325   54.32400
## 95  Thailand 1988   21.26290       9.905932 2.146481   53.41096
## 96  Thailand 1987   18.42827       9.147076 2.014663   52.47918
## 97  Thailand 1986   18.86816       9.194021 2.052221   51.54209
## 98  Thailand 1985   20.26387       9.833074 2.060787   50.59494
## 99  Thailand 1984   19.90482       9.629710 2.067021   49.63672
## 100 Thailand 1983   19.54894       9.606013 2.035074   48.67057
## 101 Thailand 1982   16.87851       8.940017 1.887973   47.70034
## 102 Thailand 1981   17.77432       9.105026 1.952144   46.72729
## 103 Thailand 1980   17.36810       9.200080 1.887820   45.73775
## 104 Thailand 1979   15.75800       8.654000 1.820892   44.76009
## 105 Thailand 1978   17.47000       8.935000 1.955232   43.80671
## 106 Thailand 1977   13.92100       8.750000 1.590971   42.84380
## 107 Thailand 1976   15.06800       8.167000 1.844986   41.88213
## 108 Thailand 1975   15.30000       8.357000 1.830801   40.90847
## 109 Thailand 1974   13.38600       7.333000 1.825447   39.90096
## 110 Thailand 1973   14.89900       7.743000 1.924190   38.87306
## 111 Thailand 1972   12.41300       6.780000 1.830826   37.83438
## 112 Thailand 1971   13.74400       7.096000 1.936866   36.80800
## 113 Thailand 1970   13.85000       6.854000 2.020718   35.79173
## 114 Thailand 1969   13.41000       7.259000 1.847362   34.78758
## 115 Thailand 1968   12.41000       6.940000 1.788184   33.80736
## 116 Thailand 1967   11.19800       6.400000 1.749688   32.83934
## 117 Thailand 1966   13.50000       7.353000 1.835985   31.88375
## 118 Thailand 1965   11.16400       6.270000 1.780542   30.94027
## 119 Thailand 1964   11.60000       6.310000 1.838352   30.01357
## 120 Thailand 1963   12.17100       6.500000 1.872462   29.11412
## 121 Thailand 1962   11.25000       6.540000 1.720183   28.24217
## 122 Thailand 1961   10.15000       6.120000 1.658497   27.39996

Xử lý dataset để thành dạng phù hợp vẽ đồ thị. Code vẽ đồ thị này mình cũng tham khảo mỗi nguồn một ít để tổng hợp thành file hoàn chỉnh như bạn đã thấy.

names(df_big) <- c("area", "year", "d_production", "c_area_harvested", "b_yield", "a_population")

# df_big
###########################

# https://scc.ms.unimelb.edu.au/resources/tips-for-using-r/r-scripts
# https://waterdata.usgs.gov/blog/beyond-basic-plotting/
# https://www.datanovia.com/en/blog/how-to-change-ggplot-facet-labels/

longer_data <- df_big %>%
  pivot_longer(d_production:a_population, names_to = "value_data", values_to = "response")

longer_data$note_1 <- "Khủng hoảng\nkinh tế 1997"

clean_1 <- longer_data |> subset(value_data == "a_population")

clean_1$note_1 <- ""

clean_2 <- longer_data |> subset(value_data == "b_yield")

clean_2$note_1 <- ""

clean_3 <- longer_data |> subset(value_data == "c_area_harvested")

clean_3$note_1 <- ""

clean_4 <- longer_data |> subset(value_data == "d_production")

gop_lai <- rbind(clean_1, clean_2, clean_3, clean_4)

gop_lai -> longer_data

longer_data$area <- factor(longer_data$area,
                           levels = c("Viet Nam", "Thailand"),
                           labels = c("Việt Nam", "Thái Lan"))


supp.labs <- c("Sản lượng \n(triệu tấn)", "Diện tích canh tác \n(triệu ha)", "Năng suất \n(tấn/ha)",
               "Dân số \n(triệu người)")
names(supp.labs) <- names(df_big)[3:6]

rice_plot <- ggplot(data = na.omit(longer_data), aes(x = year, y = response, 
                                                     color = area, group = area)) +
  geom_point(aes(color = area, shape = area), size = 1.5) +
  
  scale_shape_manual(values = c(19, 22)) +
  
  scale_x_continuous(breaks = c(1961, 1970, 1980, 1990, 1997, 1999, 2010, 2021)) +
  
  geom_line(aes(color = area)) +
  
  scale_color_manual(values=c("#ff4b66", "#0b3173")) +
  
  facet_grid(scales = "free_y",
             # cols = vars(value_data),
             rows = vars(value_data),
             labeller = labeller(value_data = supp.labs),
             switch = "both"
  ) +
  labs(x = "", y = "", title = "Tình hình sản xuất lúa gạo ở Việt Nam và Thái Lan | Dữ liệu FAOSTAT | Đồ thị: tuhocr.com") +

  theme_bw() +
  
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank()) +
  
  theme(strip.background = element_blank(), # remove the background
        strip.placement = "outside") +
  theme(legend.position="top",
        legend.title = element_blank(),
        legend.text = element_text(size = 11, colour = "#261d3b", face = "bold")) +
  
  theme(
    # strip.text.x = element_text(
    #   size = 9, color = "red", face = "bold"
    # ),
    strip.text.y = element_text(
      size = 10, color = "darkgreen", face = "bold"
    )
  ) + 
  
  # coord_flip() +
  
  theme(axis.text = element_text(colour = "black",
                                 size = 8,
                                 face = "bold"),
        plot.title = element_text(size = 12, colour = "black", face = "bold", hjust = 0.5)) +
  
  theme(plot.margin = margin(t = 0.5, r = 0.5, b = 0.5, l = 0.5, "cm")) +
  
  annotate("rect", xmin = 1997, xmax = 1999, 
           ymin = -Inf,
           ymax = +Inf,
           alpha = 0.1,
           fill = "blue")

############
# https://stackoverflow.com/questions/26371307/add-text-to-individual-facets-in-ggplot
# rice_plot + geom_text(aes(x = 1997, y = 30, label = note_1), 
#             show.legend = FALSE, size = 4) 


rice_plot + geom_text(data = longer_data,
                      mapping = aes(x = 1997, y = Inf, label = note_1),
                      hjust   = "middle",
                      vjust   = 1.5,
                      show.legend = FALSE,
                      color = "#a10800")

# ggsave("four_plot_rice_v11.png", width = 30, height = 20, units = "cm", dpi = 300)

Sơ kết

Trên đây là hướng dẫn vẽ đồ thị đa biến sử dụng package ggplot2. Để 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