1 Import dataset

Reproducible report

library(readxl)
df<- read_excel("fire data.xlsx", 
                sheet = "ALL",
                range = "A1:D73")

df <- as.data.frame(df)

df$country <- factor(df$country,
                     levels = c("Germany", "France", "Austria",
                                "Switzerland", "Slovenia", "Italy"))

2 Check dataset

dim(df)
## [1] 72  4
str(df)
## 'data.frame':    72 obs. of  4 variables:
##  $ country               : Factor w/ 6 levels "Germany","France",..: 3 3 3 3 3 3 3 3 3 3 ...
##  $ year                  : num  2012 2013 2014 2015 2016 ...
##  $ number_of_forest_fires: num  259 199 146 280 141 278 174 244 234 164 ...
##  $ burnt_area_ha         : num  48 102 92 114 23 25 19 20 60 117 ...
summary(df)
##         country        year      number_of_forest_fires burnt_area_ha      
##  Germany    :12   Min.   :2012   Min.   :  32           Min.   :    18.00  
##  France     :12   1st Qu.:2015   1st Qu.: 118           1st Qu.:    68.25  
##  Austria    :12   Median :2018   Median : 352           Median :   381.50  
##  Switzerland:12   Mean   :2018   Mean   :1547           Mean   : 14920.64  
##  Slovenia   :12   3rd Qu.:2020   3rd Qu.:2761           3rd Qu.:  8807.25  
##  Italy      :12   Max.   :2023   Max.   :8252           Max.   :161987.00
sapply(df, class)
##                country                   year number_of_forest_fires 
##               "factor"              "numeric"              "numeric" 
##          burnt_area_ha 
##              "numeric"

3 Vẽ biểu đồ cột

3.1 Biểu đồ cột đơn giản

library(ggplot2)

df_Austria <- df |> subset(country == "Austria")

p_Austria <- ggplot(data = df_Austria,
       mapping = aes(x = year,
                     y = number_of_forest_fires)) +
  
  # geom_col() +
  
  geom_bar(stat = "identity")


p_Austria

library(ggplot2)

df_Germany <- df |> subset(country == "Germany")

p_Germany <- ggplot(data = df_Germany,
       mapping = aes(x = year,
                     y = number_of_forest_fires)) +
  
  # geom_col() +
  
  geom_bar(stat = "identity")


p_Germany

library(patchwork)

p_Austria + p_Germany

p_Austria / p_Germany

3.2 Facet biểu đồ

ggplot(data = df,
       mapping = aes(x = year,
                     y = number_of_forest_fires)) +
  
  geom_col() +

# facet_wrap(~ country,
#            scales = "free_y", # free cho y
#            nrow = 2,
#            ncol = 3
#            )

facet_wrap(~ country,
           scales = "fixed", # đồng bộ scale x và y
           nrow = 2,
           ncol = 3
           )

3.3 Vẽ cho các nước phía Bắc

df_north <- df |> subset(country == "Austria" | country == "Germany" |
                           country == "France")

p_north <- ggplot(data = df_north,
       mapping = aes(x = year,
                     y = number_of_forest_fires)) +
  
  geom_col() +

# facet_wrap(~ country,
#            scales = "free_y", # free cho y
#            nrow = 2,
#            ncol = 3
#            )

facet_wrap(~ country,
           scales = "fixed", # đồng bộ scale x và y
           nrow = 1,
           ncol = 3
           )

df_north
##    country year number_of_forest_fires burnt_area_ha
## 1  Austria 2012                    259            48
## 2  Austria 2013                    199           102
## 3  Austria 2014                    146            92
## 4  Austria 2015                    280           114
## 5  Austria 2016                    141            23
## 6  Austria 2017                    278            25
## 7  Austria 2018                    174            19
## 8  Austria 2019                    244            20
## 9  Austria 2020                    234            60
## 10 Austria 2021                    164           117
## 11 Austria 2022                    217           550
## 12 Austria 2023                    119            21
## 13  France 2012                   2763          7584
## 14  France 2013                   1510          2735
## 15  France 2014                   1730          4735
## 16  France 2015                   2898          8169
## 17  France 2016                   2775         14101
## 18  France 2017                   3220         23093
## 19  France 2018                   1634          3940
## 20  France 2019                   2949         13394
## 21  France 2020                   2760         10722
## 22  France 2021                   2332         12779
## 23  France 2022                   4215         58123
## 24  France 2023                   2666          5361
## 25 Germany 2012                    701           269
## 26 Germany 2013                    515           199
## 27 Germany 2014                    429           120
## 28 Germany 2015                   1071           526
## 29 Germany 2016                    608           283
## 30 Germany 2017                    424           395
## 31 Germany 2018                   1708          2349
## 32 Germany 2019                   1523          2711
## 33 Germany 2020                   1360           368
## 34 Germany 2021                    548           148
## 35 Germany 2022                   2397          3058
## 36 Germany 2023                   1059          1240

3.4 Vẽ cho các nước phía Nam

df_south <- df |> subset(country == "Switzerland" | country == "Slovenia" |
                           country == "Italy")

p_south <- ggplot(data = df_south,
       mapping = aes(x = year,
                     y = number_of_forest_fires)) +
  
  geom_col() +

# facet_wrap(~ country,
#            scales = "free_y", # free cho y
#            nrow = 2,
#            ncol = 3
#            )

facet_wrap(~ country,
           scales = "fixed", # đồng bộ scale x và y
           nrow = 1,
           ncol = 3
           )

3.5 Combine

library(patchwork)

p_north / p_south

4 Vẽ biểu đồ đường với scale chung

ggplot(data = df,
       mapping = aes(x = year,
                     y = burnt_area_ha,
                     color = country)) +
  
  geom_line(linewidth = 1) +

facet_wrap(~ country,
           scales = "fixed",
           nrow = 2,
           ncol = 3
           ) +
  
  labs(x = "Year",
       y = "Burn area (ha)",
       title = "Forest") +
  
  scale_color_manual(name = "Country",
                     values = c("red",
                                "blue",
                                "darkgreen",
                                "#0a57c0",
                                "brown",
                                "purple")) +
  
  theme_bw(base_size = 15) +
  
  theme(strip.text = element_text(face = "bold")) +
  
  theme(strip.background = element_rect(fill = "lightyellow"))

ggsave(filename = "burn_forest.png",
       width = 10,
       height = 5,
       dpi = 300,
       units = "in")

5 Vẽ biểu đồ đường với scale riêng

ggplot(data = df,
       mapping = aes(x = year,
                     y = burnt_area_ha,
                     color = country)) +
  
  geom_line(linewidth = 1) +

facet_wrap(~ country,
           scales = "free_y",
           nrow = 2,
           ncol = 3
           ) +
  
  labs(x = "Year",
       y = "Burn area (ha)",
       title = "Forest") +
  
  scale_color_manual(name = "Country",
                     values = c("red",
                                "blue",
                                "darkgreen",
                                "#0a57c0",
                                "brown",
                                "purple")) +
  
  theme_bw(base_size = 15) +
  
  theme(strip.text = element_text(face = "bold")) +
  
  theme(strip.background = element_rect(fill = "lightyellow"))

ggsave(filename = "burn_forest_scale_y.png",
       width = 10,
       height = 5,
       dpi = 300,
       units = "in")

6 Vẽ donut

library(tidyverse)

data <- read.csv("FORMAT_DONUT.csv")

library(ggplot2)

### fire june

names(data) <- c("category", "fraction")

data$fraction <- data$fraction / sum(data$fraction)

data$ymax <- cumsum(data$fraction)

data$ymin <- c(0, head(data$ymax, n = -1))

data$labelPosition <- (data$ymax + data$ymin) / 2

data$label <- paste0(round(data$fraction * 100, 1),
                     "%")

data$label[which(data$fraction == 0)] <- ""

fire_july <- ggplot(data,
       aes(ymax = ymax,
           ymin = ymin,
           xmax = 4,
           xmin = 2,
           fill = category)) +

  geom_rect(color = "black",
            show.legend = T) +

geom_text(x = 3,
          aes(y = labelPosition,
              label = label,),
          color = "black",
          size = 5,
          fontface = "bold") +

  scale_fill_manual(name = "Quality Levels",
                      values = c("#e75d56",
                                  "#aee1f4",
                                  "#eac861",
                                  "#139f72",
                                 "yellow")) +


  coord_polar(theta = "y",
              direction = 1) +

  xlim(c(-1, 4)) +

  theme_void() +

  theme(plot.margin = margin(t = 0, b = 0, l = 0, r = 0)) +
  
  # theme(panel.grid.major = element_blank(), 
  #       panel.grid.minor = element_blank(),
  #       panel.background = element_rect(colour = "black", size = 1)) +
  
  labs(title = "July") +
  
  theme(plot.title = element_text(size = "22",
                                  face = "bold",
                                  colour = "black",
                                  angle = 0,
                                  hjust = 0.5,
                                  vjust = -2,
                                  margin = margin(t = 0, r = 0, b = 10, l = 0, unit = "pt")))

fire_july

ggsave(filename = "fire_july.png",
       plot = fire_july,
       width = 10,
       height = 5,
       dpi = 300,
       bg = "white",
       units = "in")
library(tidyverse)

data <- read.csv("FORMAT_DONUT.csv")

library(ggplot2)

### fire june

names(data) <- c("category", "fraction")

data$fraction <- data$fraction / sum(data$fraction)

data$ymax <- cumsum(data$fraction)

data$ymin <- c(0, head(data$ymax, n = -1))

data$labelPosition <- (data$ymax + data$ymin) / 2

data$label <- paste0(round(data$fraction * 100, 1),
                     "%")

data$label[which(data$fraction == 0)] <- ""

fire_aug <- ggplot(data,
       aes(ymax = ymax,
           ymin = ymin,
           xmax = 4,
           xmin = 2,
           fill = category)) +

  geom_rect(color = "black",
            show.legend = T) +

geom_text(x = 3,
          aes(y = labelPosition,
              label = label,),
          color = "black",
          size = 5,
          fontface = "bold") +

  scale_fill_manual(name = "Quality Levels",
                      values = c("#e75d56",
                                  "#aee1f4",
                                  "#eac861",
                                  "#139f72",
                                 "yellow")) +


  coord_polar(theta = "y",
              direction = 1) +

  xlim(c(-1, 4)) +

  theme_void() +

  theme(plot.margin = margin(t = 0, b = 0, l = 0, r = 0)) +
  
  # theme(panel.grid.major = element_blank(), 
  #       panel.grid.minor = element_blank(),
  #       panel.background = element_rect(colour = "black", size = 1)) +
  
  labs(title = "August") +
  
  theme(plot.title = element_text(size = "22",
                                  face = "bold",
                                  colour = "black",
                                  angle = 0,
                                  hjust = 0.5,
                                  vjust = -2,
                                  margin = margin(t = 0, r = 0, b = 10, l = 0, unit = "pt")))

fire_aug

ggsave(filename = "fire_aug.png",
       plot = fire_aug,
       width = 10,
       height = 5,
       dpi = 300,
       bg = "white",
       units = "in")
library(patchwork)

fire_july + fire_aug + plot_layout(guides = "collect") & 
  
  theme(legend.position = "bottom",
        legend.key.size = unit(1, "cm"),
        legend.text = element_text(size = 15),
        legend.key.width = unit(2, "cm"),
        legend.key.spacing.x = unit(0.5, "cm"),
        legend.key.spacing.y = unit(0.5, "cm"),
        legend.title = element_blank())  

ggsave(filename = "donut_gop.png",
       width = 10,
       height = 7,
       dpi = 300,
       bg = "white",
       units = "in")

7 Tham khảo format

https://r-graph-gallery.com/line-chart-dual-Y-axis-ggplot2.html

# Libraries
library(ggplot2)
library(dplyr)
library(patchwork) # To display 2 charts together
library(hrbrthemes)

# Build dummy data
data <- data.frame(
  day = as.Date("2019-01-01") + 0:99,
  temperature = runif(100) + seq(1,100)^2.5 / 10000,
  price = runif(100) + seq(100,1)^1.5 / 10
)

# Most basic line chart
p1 <- ggplot(data, aes(x=day, y=temperature)) +
  geom_line(color="#69b3a2", size=2) +
  ggtitle("Temperature: range 1-10") +
  theme_ipsum()
  
p2 <- ggplot(data, aes(x=day, y=price)) +
  geom_line(color="grey",size=2) +
  ggtitle("Price: range 1-100") +
  theme_ipsum()

# Display both charts side by side thanks to the patchwork package
p1 + p2

8 Tài liệu tham khảo

Các dạng plot cần vẽ https://docs.google.com/document/d/1VFcD0xis9tFeLE7i0gDQIcKjrWznJlXK4xhmQ8hUAnQ/edit?tab=t.0

Hướng dẫn cú pháp R Markdown https://epirhandbook.com/vn/new_pages/rmarkdown.vn.html