26  local change

26.1 Install and load the tidyverse and ggplot2 packages if not already installed

if (!require(tidyverse)) {
  install.packages("tidyverse")
  library(tidyverse)
}
Loading required package: tidyverse
Warning: package 'dplyr' was built under R version 4.2.3
Warning: package 'stringr' was built under R version 4.2.3
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.0
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
if (!require(ggplot2)) {
  install.packages("ggplot2")
  library(ggplot2)
}

if (!require(viridis)) {
  install.packages("viridis")
  library(viridis)
}
Loading required package: viridis
Loading required package: viridisLite

26.2 Assuming data, temps, and years are already defined

data <- data <- read_csv('dataset/FAOSTAT_data_1-10-2022.csv')
Rows: 229925 Columns: 14
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (8): Domain Code, Domain, Area, Element, Months, Unit, Flag, Flag Descri...
dbl (6): Area Code (FAO), Element Code, Months Code, Year Code, Year, Value

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

26.3 Filter data based on conditions

country <- 'Indonesia'

filtered_data <- data %>%
  # filter(Area == country, Months == "July" | Months == "December" ) %>%
  filter(Area == country) 


temps <- filtered_data$Value
years <- filtered_data$Year
month <- filtered_data$Months

26.4 Create a data frame with the extracted data

df <- data.frame(Year = years, Temperature = temps, month)
df <- df %>% 
  group_by(Year) %>%
  summarise(Temperature = mean(Temperature))

26.5 Create the ggplot with a bar chart and colormap

plot <- ggplot(df, aes(x = Year, y = Temperature, fill = Temperature)) +
  geom_bar(stat = "identity") +
  scale_fill_viridis(option = "C") +
  labs(title = paste(" Overall temperature change in Indonesia"),
       y = "Δ°C") +
  theme_minimal()