knitr::opts_chunk$set(echo = TRUE, cache = TRUE)

Packages

Loading required packages

library(ggplot2)
library(dplyr)
library(ggthemes)
library(DescTools)
library(forcats)
#install.packages("patchwork")
library(scales)
library(ggpubr)
library(esquisse)
library(patchwork)

Setting global theme

#theme_set(theme_bw())

Data frame for each month

DF_2023 <- data.frame(
  Month= month.name,
  Monthly_used = c(16, 18, 18, 17, 16, 18, 17, 15, 16, 15, 14, 15),
  Cost_per_unit= c(20)
)

DF_2023 <- DF_2023 %>%
  mutate(
    Monthly_expenditure = Monthly_used * Cost_per_unit
  )
DF_2023
##        Month Monthly_used Cost_per_unit Monthly_expenditure
## 1    January           16            20                 320
## 2   February           18            20                 360
## 3      March           18            20                 360
## 4      April           17            20                 340
## 5        May           16            20                 320
## 6       June           18            20                 360
## 7       July           17            20                 340
## 8     August           15            20                 300
## 9  September           16            20                 320
## 10   October           15            20                 300
## 11  November           14            20                 280
## 12  December           15            20                 300
DF_2024 <- data.frame(
  Month= month.name,
  Monthly_used = c(16, 17, 16, 17, 16, 15, 17, 15, 16, 15, 13, 15),
  Cost_per_unit= c(20)
)

DF_2024 <- DF_2024 %>%
  mutate(
    Monthly_expenditure = Monthly_used * Cost_per_unit
  )
DF_2024
##        Month Monthly_used Cost_per_unit Monthly_expenditure
## 1    January           16            20                 320
## 2   February           17            20                 340
## 3      March           16            20                 320
## 4      April           17            20                 340
## 5        May           16            20                 320
## 6       June           15            20                 300
## 7       July           17            20                 340
## 8     August           15            20                 300
## 9  September           16            20                 320
## 10   October           15            20                 300
## 11  November           13            20                 260
## 12  December           15            20                 300
DF_2025 <- data.frame(
  Month= month.name,
  Monthly_used = c(16, 14, 14, 13, 16, 15, 17, 15, 14, 12, 13, 15),
  Cost_per_unit= c(20)
)

DF_2025 <- DF_2025 %>% 
  mutate(
    Monthly_expenditure = Monthly_used * Cost_per_unit
  )
DF_2025
##        Month Monthly_used Cost_per_unit Monthly_expenditure
## 1    January           16            20                 320
## 2   February           14            20                 280
## 3      March           14            20                 280
## 4      April           13            20                 260
## 5        May           16            20                 320
## 6       June           15            20                 300
## 7       July           17            20                 340
## 8     August           15            20                 300
## 9  September           14            20                 280
## 10   October           12            20                 240
## 11  November           13            20                 260
## 12  December           15            20                 300
DF_2026 <- data.frame(
  Month= month.name,
  Monthly_used = c(15, 14, 12, 13, 11, 14, 16, 15, 11, 12, 13, 13),
  Cost_per_unit= c(20)
)

DF_2026 <- DF_2026 %>% 
  mutate(
    Monthly_expenditure = Monthly_used * Cost_per_unit
  )
DF_2025
##        Month Monthly_used Cost_per_unit Monthly_expenditure
## 1    January           16            20                 320
## 2   February           14            20                 280
## 3      March           14            20                 280
## 4      April           13            20                 260
## 5        May           16            20                 320
## 6       June           15            20                 300
## 7       July           17            20                 340
## 8     August           15            20                 300
## 9  September           14            20                 280
## 10   October           12            20                 240
## 11  November           13            20                 260
## 12  December           15            20                 300
DF_2027 <- data.frame(
  Month= c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"),
  Monthly_used = c(15, 14, 11, 13, 16, 14, 15, 13, 11, 12, 13, 12),
  Cost_per_unit= c(20)
)

DF_2027 <- DF_2027 %>% 
  mutate(
    Monthly_expenditure = Monthly_used * Cost_per_unit
  )
DF_2027
##        Month Monthly_used Cost_per_unit Monthly_expenditure
## 1    January           15            20                 300
## 2   February           14            20                 280
## 3      March           11            20                 220
## 4      April           13            20                 260
## 5        May           16            20                 320
## 6       June           14            20                 280
## 7       July           15            20                 300
## 8     August           13            20                 260
## 9  September           11            20                 220
## 10   October           12            20                 240
## 11  November           13            20                 260
## 12  December           12            20                 240
DF_2028 <- data.frame(
  Month= c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"),
  Monthly_used = c(13, 14, 11, 10, 12, 10, 13, 13, 11, 12, 9, 11),
  Cost_per_unit= c(20)
)

DF_2028 <- DF_2028 %>%
  mutate(
    Monthly_expenditure = Monthly_used * Cost_per_unit
  )
DF_2028
##        Month Monthly_used Cost_per_unit Monthly_expenditure
## 1    January           13            20                 260
## 2   February           14            20                 280
## 3      March           11            20                 220
## 4      April           10            20                 200
## 5        May           12            20                 240
## 6       June           10            20                 200
## 7       July           13            20                 260
## 8     August           13            20                 260
## 9  September           11            20                 220
## 10   October           12            20                 240
## 11  November            9            20                 180
## 12  December           11            20                 220

Bar diagram of monthly expenditure

ggplot(DF_2028, aes(x = Month, y = Monthly_expenditure)) +
  geom_bar(stat = "identity", fill = "skyblue") +
  labs(
    title = "Bar diagram of monthly expenditure in the year of 2028",
    caption = "Source: Myself",
    x = "Month",
    y = "Monthly Expenditure"
  ) +
  scale_x_discrete(limits= c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"))+
  theme(
    plot.title = element_text(color= "coral4", size = 15, face = "bold"),
    plot.caption = element_text(color = "grey40", face = "italic"),
    axis.text.x = element_text(angle = 45, hjust = 1)
  )+
  geom_text(aes(label = Monthly_expenditure), vjust= 1.5, color= "black", size = 3)+
  coord_cartesian(expand = FALSE)+
  ylim(0, 300)

Data frame for each year

DF <- data.frame(
   Year= c("YR-2023","YR-2024", "YR-2025", "YR-2026", "YR-2027", "YR-2028"),
   Yearly_Expenditure= c(
     Mean_expenditure_2023 <- mean(DF_2023$Monthly_expenditure),
     Mean_expenditure_2024 <- round(mean(DF_2024$Monthly_expenditure)),
     Mean_expenditure_2025 <- mean(DF_2025$Monthly_expenditure),
     Mean_expenditure_2026 <- mean(DF_2026$Monthly_expenditure),
     Mean_expenditure_2027 <- mean(DF_2027$Monthly_expenditure),
     Mean_expenditure_2028 <- round(mean(DF_2028$Monthly_expenditure))
   )
)
DF
##      Year Yearly_Expenditure
## 1 YR-2023                325
## 2 YR-2024                313
## 3 YR-2025                290
## 4 YR-2026                265
## 5 YR-2027                265
## 6 YR-2028                232

Bar diagram of yearly expenditure

ggplot(DF, aes(x= Year, y= Yearly_Expenditure)) +
  geom_bar(stat = "identity", fill= "skyblue") +
  labs(
    title = "Bar diagram of yearly expenditure by mean of each month expenditure",
    subtitle = "Made by ggplot2",
    caption = "Source: Myself",
    x = "Year",
    y = "Yearly Expenditure"
  )+
  scale_x_discrete(limits= c("YR-2023","YR-2024", "YR-2025", "YR-2026", "YR-2027", "YR-2028" ))+
  theme(
    plot.title = element_text(color= "coral4", size = 15, face = "bold"),
    plot.subtitle = element_text(color= "black", size = 12, face = "bold"),
    plot.caption = element_text(color = "grey40", face = "italic"),
    axis.text.x = element_text(angle = 45, hjust = 1)
  )+
  coord_cartesian(expand = FALSE)+
  ylim(0, 350)

Data frame for each year

df <- data.frame(
   Year= c(2023, 2024, 2025, 2026, 2027, 2028),
   Yearly_Expenditure= c(
     Mean_expenditure_2023 <- mean(DF_2023$Monthly_expenditure),
     Mean_expenditure_2024 <- round(mean(DF_2024$Monthly_expenditure)),
     Mean_expenditure_2025 <- mean(DF_2025$Monthly_expenditure),
     Mean_expenditure_2026 <- mean(DF_2026$Monthly_expenditure),
     Mean_expenditure_2027 <- mean(DF_2027$Monthly_expenditure),
     Mean_expenditure_2028 <- round(mean(DF_2028$Monthly_expenditure))
   )
)
df
##   Year Yearly_Expenditure
## 1 2023                325
## 2 2024                313
## 3 2025                290
## 4 2026                265
## 5 2027                265
## 6 2028                232

Bar diagram of yearly expenditure

ggplot(df, aes(x= Year, y= Yearly_Expenditure)) +
  geom_bar(stat = "identity", fill= "skyblue") +
  geom_point(size = 3, color = "blue") +
  geom_smooth(method = "lm", se = FALSE, color = "tomato")+
  labs(
    title = "Bar diagram of yearly expenditure by mean of each month expenditure",
    subtitle = "Made by ggplot2",
    caption = "Source: Myself",
    x = "Year",
    y = "Yearly Expenditure"
  )+
  scale_x_continuous(breaks = 2023:2028)+
  theme(
    plot.title = element_text(color= "coral4", size = 15, face = "bold"),
    plot.subtitle = element_text(color= "black", size = 12, face = "bold"),
    plot.caption = element_text(color = "grey40", face = "italic"),
    axis.text.x = element_text(angle = 45, hjust = 1)
  )+
  coord_cartesian(expand = FALSE)+
  ylim(0, 350)
## `geom_smooth()` using formula = 'y ~ x'