library

library(tidyverse)
## ── 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
library(skimr)
library(ggpmisc)
## 載入需要的套件:ggpp
## Registered S3 methods overwritten by 'ggpp':
##   method                  from   
##   heightDetails.titleGrob ggplot2
##   widthDetails.titleGrob  ggplot2
## 
## 載入套件:'ggpp'
## 
## 下列物件被遮斷自 'package:ggplot2':
## 
##     annotate
## 
## Registered S3 method overwritten by 'ggpmisc':
##   method                  from   
##   as.character.polynomial polynom
library(patchwork)
library(ggthemes)
library(showtext)
## 載入需要的套件:sysfonts
## 載入需要的套件:showtextdb
theme_set(theme_economist())
font_add("Microsoft YaHei", "msyh.ttc")
showtext_auto()

資料清理

匯入資料集

儲存為data_raw

data_raw <- read_csv("data/TMU_electricity_data.csv")

資料集檢查

skim(data_raw) |> head(10)
Data summary
Name data_raw
Number of rows 274
Number of columns 151
_______________________
Column type frequency:
character 10
________________________
Group variables None

Variable type: character

skim_variable n_missing complete_rate min max empty n_unique whitespace
日期 1 1 18 18 0 273 0
醫綜_MVCB 校總盤 0 1 1 5 0 50 0
…3 0 1 1 5 0 172 0
…4 0 1 1 5 0 225 0
…5 0 1 1 5 0 39 0
…6 0 1 1 5 0 256 0
醫綜_VCB1 醫綜1樓以下 0 1 1 4 0 50 0
…8 0 1 1 4 0 170 0
…9 0 1 1 4 0 206 0
…10 0 1 1 5 0 38 0

清理資料集

處理欄位名稱和日期格式並儲存於data

data <- data_raw

names <- colnames(data_raw)[seq(2, 151, 5)] |> 
  rep(each = 5)

colnames(data)[-1] <- colnames(data_raw)[seq(2, 151, 5)] |> 
  rep(each = 5) |> 
  paste(data_raw[1, -1], sep = "_")

data <- data |> 
  slice(-1) |> 
  rename(date = 日期) |> 
  mutate(
    date = str_sub(date , end = 10) |> ymd(),
    across(-1, as.numeric)
  ) 

檢查結果

colnames(data)[1:10]
##  [1] "date"                             "醫綜_MVCB 校總盤_尖峰"           
##  [3] "醫綜_MVCB 校總盤_半尖峰"          "醫綜_MVCB 校總盤_離峰"           
##  [5] "醫綜_MVCB 校總盤_週六半尖峰"      "醫綜_MVCB 校總盤_總用電"         
##  [7] "醫綜_VCB1 醫綜1樓以下_尖峰"       "醫綜_VCB1 醫綜1樓以下_半尖峰"    
##  [9] "醫綜_VCB1 醫綜1樓以下_離峰"       "醫綜_VCB1 醫綜1樓以下_週六半尖峰"
class(data$date)
## [1] "Date"

選取關注的部分

選取欄位名是”拇山大樓”者儲存為data_dorm_raw

data_dorm_raw <- data |> 
  select(date, starts_with("拇山大樓"))
glimpse(data_dorm_raw[,1:10])
## Rows: 273
## Columns: 10
## $ date                         <date> 2022-11-01, 2022-11-02, 2022-11-03, 2022…
## $ 拇山大樓_MVCB_尖峰           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ 拇山大樓_MVCB_半尖峰         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ 拇山大樓_MVCB_離峰           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ 拇山大樓_MVCB_週六半尖峰     <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ 拇山大樓_MVCB_總用電         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ 拇山大樓_女生宿舍_尖峰       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ 拇山大樓_女生宿舍_半尖峰     <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ 拇山大樓_女生宿舍_離峰       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ 拇山大樓_女生宿舍_週六半尖峰 <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…

檢查和處理空白值

發現6/26前全部缺失,因此只取七月數據儲存於data_dorm,同時7/26數據也缺失因此移除

is_empty <- ifelse(rowSums(data_dorm_raw[,-1]) > 0, "正常", "缺失")
names(is_empty) <- data_dorm_raw$date
tail(is_empty, 40)
## 2023-06-22 2023-06-23 2023-06-24 2023-06-25 2023-06-26 2023-06-27 2023-06-28 
##     "缺失"     "缺失"     "缺失"     "缺失"     "正常"     "正常"     "正常" 
## 2023-06-29 2023-06-30 2023-07-01 2023-07-02 2023-07-03 2023-07-04 2023-07-05 
##     "正常"     "正常"     "正常"     "正常"     "正常"     "正常"     "正常" 
## 2023-07-06 2023-07-07 2023-07-08 2023-07-09 2023-07-10 2023-07-11 2023-07-12 
##     "正常"     "正常"     "正常"     "正常"     "正常"     "正常"     "正常" 
## 2023-07-13 2023-07-14 2023-07-15 2023-07-16 2023-07-17 2023-07-18 2023-07-19 
##     "正常"     "正常"     "正常"     "正常"     "正常"     "正常"     "正常" 
## 2023-07-20 2023-07-21 2023-07-22 2023-07-23 2023-07-24 2023-07-25 2023-07-26 
##     "正常"     "正常"     "正常"     "正常"     "正常"     "正常"     "缺失" 
## 2023-07-27 2023-07-28 2023-07-29 2023-07-30 2023-07-31 
##     "正常"     "正常"     "正常"     "正常"     "正常"
data_dorm <- data_dorm_raw |> 
  filter(month(date) == 7, !date == "2023-7-26") 

資料分析

各時段分析

尖峰+半尖峰+周六半尖峰+離峰與總用量相關係數

男宿

cor(
  x = data_dorm$拇山大樓_男生宿舍_總用電,
  y = data_dorm$拇山大樓_男生宿舍_週六半尖峰 + 
    data_dorm$拇山大樓_男生宿舍_離峰 + 
    data_dorm$拇山大樓_男生宿舍_尖峰 + 
    data_dorm$拇山大樓_男生宿舍_半尖峰
)
## [1] 1

女宿

cor(
  x = data_dorm$拇山大樓_女生宿舍_總用電,
  y = data_dorm$拇山大樓_女生宿舍_週六半尖峰 + 
    data_dorm$拇山大樓_女生宿舍_離峰 + 
    data_dorm$拇山大樓_女生宿舍_尖峰 + 
    data_dorm$拇山大樓_女生宿舍_半尖峰
)
## [1] 1

各時段相關係數

ggcorrplot::ggcorrplot(
  cor(
    data_dorm |> 
      select(ends_with("總用電"), -contains("PM_MP"))  |> 
      mutate(男女宿相加 = 拇山大樓_女生宿舍_總用電 + 拇山大樓_男生宿舍_總用電) |> 
      rename(
        "MVCB" = 拇山大樓_MVCB_總用電,
        "女生宿舍" = 拇山大樓_女生宿舍_總用電,
        "男生宿舍" = 拇山大樓_男生宿舍_總用電
      )
  ),
  type = "lower", colors = c("#C91D42", "#EBEDFA", "#2E45B8"),
  title = "各總用電量相關係數", tl.cex = 10, lab = TRUE
) +
  theme(
    text = element_text(family = "Microsoft YaHei"),
    plot.background = element_rect(fill = "#d5e4eb"),
    axis.text.x = element_text(hjust = 0.5, angle = 0)
  )

男女差異

男女總用電量差異

ggplot(data_dorm, aes(x = date)) +
  geom_hline(
    aes(yintercept = mean(拇山大樓_女生宿舍_總用電)),
    linetype = "longdash", color = "#E2365B"
  ) +
  annotate(
    geom = "text",
    x = ymd("2023-07-30"), y = mean(data_dorm$拇山大樓_女生宿舍_總用電),
    label = "女宿平均用電量", vjust = -1, size = 3
  ) +
  geom_hline(
    aes(yintercept = mean(拇山大樓_男生宿舍_總用電)),
    linetype = "longdash", color = "#475ED1"
  ) +
  annotate(
    geom = "text",
    x = ymd("2023-07-30"), y = mean(data_dorm$拇山大樓_男生宿舍_總用電),
    label = "男宿平均用電量", vjust = -1, size = 3
  ) +
  geom_line(aes(y = 拇山大樓_女生宿舍_總用電, color = "女生宿舍"), linewidth = 1) +
  geom_line(aes(y = 拇山大樓_男生宿舍_總用電, color = "男生宿舍"), linewidth = 1) +
  stat_peaks(
    aes(y = 拇山大樓_女生宿舍_總用電),
    geom = "point", span = 9, color = "#E2365B"
  ) +
  stat_peaks(
    aes(y = 拇山大樓_女生宿舍_總用電),
    geom = "label", span = 9, color = "#E2365B", angle = 0,
    hjust = -0.1, x.label.fmt = "%Y-%m-%d"
  ) +
  stat_peaks(
    aes(y = 拇山大樓_男生宿舍_總用電),
    geom = "point", span = 9, color = "#475ED1"
  ) +
  stat_peaks(
    aes(y = 拇山大樓_男生宿舍_總用電),
    geom = "label", span = 9, color = "#475ED1", angle = 0,
    hjust = -0.1, x.label.fmt = "%Y-%m-%d"
  ) +
  scale_x_date(date_breaks = "1 week") +
  scale_color_manual(values = c("#C91D42", "#2E45B8")) +
  labs(
    x = "",
    y = "用電量(度)",
    title = "男宿女宿總用電量差異"
  ) +
  guides(color = guide_legend(title = "")) +
  theme(
    legend.position = c(0.9, 0.9),
    text = element_text(family = "Microsoft YaHei")
  )

女宿/男宿各時段比較

標出假日

monday <- data_dorm |>
    filter(wday(date) == 1) |> 
    pull(date)
friday <- data_dorm |>
    filter(wday(date) == 7) |> 
    pull(date)
shade <- tibble(
  x1 = monday[-6],
  x2 = friday,
  min = -Inf, max = Inf
)

女宿/男宿各時段用電量

p1 <- ggplot(data_dorm, aes(x = date)) +
  geom_line(aes(y = 拇山大樓_女生宿舍_尖峰, color = "尖峰"), linewidth = 1) +
  geom_line(aes(y = 拇山大樓_女生宿舍_離峰, color = "離峰"), linewidth = 1) +
  geom_line(aes(y = 拇山大樓_女生宿舍_半尖峰, color = "半尖峰"), linewidth = 1) +
  geom_line(
    aes(y = 拇山大樓_女生宿舍_週六半尖峰, color = "週六半尖峰"),
    linewidth = 1
  ) +
  geom_rect(
    data = shade,
    aes(xmin = x1, xmax = x2, ymin = min, ymax = max, fill = "假日"),
    alpha = 0.2, inherit.aes = FALSE
  ) +
  labs(
    x = "",
    y = "用電量(度)",
    title = "女宿各時段用電量比較"
  ) +
  scale_y_continuous(limits = c(0, 1000)) +
  scale_color_economist() +
  scale_fill_economist() +
  guides(color = guide_legend(title = ""), fill = guide_legend(title = "")) +
  theme(
    legend.position = "bottom",
    text = element_text(family = "Microsoft YaHei"),
    axis.text.x = element_text(size = 7)
  )

p2 <- ggplot(data_dorm, aes(x = date)) +
  geom_line(
    aes(y = 拇山大樓_男生宿舍_尖峰, color = "尖峰"),
    linewidth = 1
  ) +
  geom_line(
    aes(y = 拇山大樓_男生宿舍_離峰, color = "離峰"),
    linewidth = 1
  ) +
  geom_line(
    aes(y = 拇山大樓_男生宿舍_半尖峰, color = "半尖峰"),
    linewidth = 1
  ) +
  geom_line(
    aes(y = 拇山大樓_男生宿舍_週六半尖峰, color = "週六半尖峰"),
    linewidth = 1
  ) +
  geom_rect(
    data = shade,
    aes(xmin = x1, xmax = x2, ymin = min, ymax = max, fill = "假日"),
    alpha = 0.2, inherit.aes = FALSE
  ) +
  labs(x = "", y = "", title = "男宿各時段用電量比較") +
  scale_color_economist() +
  scale_fill_economist() +
  scale_y_continuous(limits = c(0, 1000)) +
  guides(color = guide_legend(title = ""), fill = guide_legend(title = "")) +
  theme(
    legend.position = "bottom",
    text = element_text(family = "Microsoft YaHei"),
    axis.text.x = element_text(size = 7)
  )

p1 + p2 + plot_layout(guides = 'collect')&theme(legend.position = "bottom") 

女宿各時段用電量

ggplot(
  data_dorm |> 
    pivot_longer(contains("女生宿舍"), names_to = "time", values_to = "cost") |> 
    mutate( 
      time = case_match(
        time,
        "拇山大樓_女生宿舍_總用電" ~ "總用電",
        "拇山大樓_女生宿舍_尖峰" ~  "尖峰",
        "拇山大樓_女生宿舍_離峰" ~ "離峰",
        "拇山大樓_女生宿舍_半尖峰" ~ "半尖峰",
        "拇山大樓_女生宿舍_週六半尖峰" ~ "週六半尖峰"
      ) 
    ), 
  aes(x = date, y = cost)
) +
  geom_line(color = "#141F52", linewidth = 1) +
  geom_rect(
    data = shade, aes(xmin = x1, xmax = x2, ymin = min, ymax = max, fill = "假日"),
    alpha = 0.2, inherit.aes = FALSE
  ) +
  guides(fill = guide_legend(title = "")) +
  labs(title = "女宿各時段用電量", x = "", y = "用電量(度)") +
  theme(
    text = element_text(family = "Microsoft YaHei"),
    axis.text.x = element_text(size = 5),
    legend.position = c(0.9, 0),
    plot.margin = unit(c(0.1, 0.5, 0.1, 0.5), "inches")
  ) +
  scale_fill_economist() +
  facet_wrap(~factor(
      time,
      levels = c("總用電", "尖峰", "半尖峰", "週六半尖峰", "離峰")
  ))

男宿各時段用電量

ggplot(
  data_dorm |> 
    pivot_longer(contains("男生宿舍"), names_to = "time", values_to = "cost") |> 
    mutate(
      time = case_match(
        time,
        "拇山大樓_男生宿舍_總用電" ~ "總用電",
        "拇山大樓_男生宿舍_尖峰" ~  "尖峰",
        "拇山大樓_男生宿舍_離峰" ~ "離峰",
        "拇山大樓_男生宿舍_半尖峰" ~ "半尖峰",
        "拇山大樓_男生宿舍_週六半尖峰" ~ "週六半尖峰"
      )
    ), 
  aes(x = date, y = cost)
) +
  geom_line(color = "#141F52", linewidth = 1) +
  geom_rect(
    data = shade, aes(xmin = x1, xmax = x2, ymin = min, ymax = max, fill = "假日"),
    alpha = 0.2, inherit.aes = FALSE
  ) +
  guides(fill = guide_legend(title = "")) +
  labs(title = "男宿各時段用電量", x = "", y = "用電量(度)") +
  theme(
    text = element_text(family = "Microsoft YaHei"),
    axis.text.x = element_text(size = 5),
    legend.position = c(0.9, 0),
    plot.margin = unit(c(0.1, 0.5, 0.1, 0.5), "inches")
  ) +
  scale_fill_economist() +
  facet_wrap(~factor(
    time, 
    levels = c("總用電", "尖峰", "半尖峰", "週六半尖峰", "離峰")
  ))

用電量與氣溫相關性

匯入氣溫資料

data_weather <- read_csv("data/2023-07weather.csv", )
## Rows: 32 Columns: 35
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (35): 觀測時間(day), 測站氣壓(hPa), 海平面氣壓(hPa), 測站最高氣壓(hPa), 測站最高氣壓時間(LST), 測站最低...
## 
## ℹ 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.
colnames(data_weather) <- data_weather |> 
  slice(1)

選取需要的資料

data_temp <- data_weather |> 
  slice(-1) |> 
  select(ObsTime, starts_with("T")) |> 
  mutate(ObsTime = as.numeric(ObsTime))

data_dorm_temp <- data_dorm |> 
  filter(month(date) == 7) |> 
  mutate(day = day(date)) |> 
  inner_join(data_temp, by = join_by(day == ObsTime)) |> 
  mutate(across(c(Temperature, `T Max`, `T Min`), as.numeric))

每日用電量與氣溫

p3 <- ggplot(data_dorm_temp, aes(x = date)) +
  geom_line(aes(y = 拇山大樓_MVCB_總用電), color = "#141F52") +
  geom_point(aes(y = 拇山大樓_MVCB_總用電), color = "#141F52") +
  scale_x_date(date_breaks = "1 week") +
  labs(x = "", y = "用電量(度)",title = "每日MVCB總用電量") +
  scale_color_economist() +
  theme(
    legend.title = element_blank(), legend.position = c(0.9, 0.9)
  )

p4 <- ggplot(data_dorm_temp, aes(x = date)) +
  geom_line(aes(y = Temperature), color = "#01a2d9") +
  geom_point(aes(y = Temperature), color = "#01a2d9") +
  geom_errorbar(
    aes(y = Temperature, ymin = `T Min`, ymax = `T Max`),
    width = 0.2, color = "#01a2d9"
  ) +
  scale_x_date(date_breaks = "1 week") +
  labs(x = "", y = "溫度(攝氏)", title = "每日氣溫") +
  scale_color_economist() +
  theme(
    legend.title = element_blank(), legend.position = c(0.9, 0.9),
    text = element_text(family = "Microsoft YaHei")
  )

p3/p4

氣溫與用電量相關係數

ggcorrplot::ggcorrplot(
  cor(
    data_dorm_temp |> 
      select(
        ends_with("總用電"), Temperature, `T Max`, `T Min`,
        -contains("PM_MP")
      ) |>
      rename(
        "MVCB" = 拇山大樓_MVCB_總用電,
        "女生宿舍" = 拇山大樓_女生宿舍_總用電,
        "男生宿舍" = 拇山大樓_男生宿舍_總用電
      )
  ),
  type = "lower", colors = c("#C91D42", "#EBEDFA", "#2E45B8"),
  title = "氣溫與用電量相關係數", lab = TRUE, tl.cex = 9,
) +
  theme(
    text = element_text(family = "Microsoft YaHei"),
    plot.background = element_rect(fill = "#d5e4eb"),
    axis.text.x = element_text(angle = 0, hjust = 0.5)
  ) 

用電量與人數相關性

匯入暑住床位資料並清理

data_female_raw <- read_csv("data/112拇山宿舍女生暑住床位.csv")
## Rows: 61 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (4): 暑住房, 學號, 暑住日期, 備註
## dbl (1): 住宿費
## 
## ℹ 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.
data_female <- data_female_raw |> 
  separate_wider_delim(暑住日期, delim = "-", names = c("入住", "搬出")) |> 
  mutate(
    入住 = as.Date(入住, format = "%m/%d"),
    搬出 = as.Date(搬出, format = "%m/%d"),
  ) |> 
  filter(month(入住) == 7) |> 
  mutate(區間 = interval(入住, 搬出)) 

data_male_raw <- read_csv("data/112拇山男生暑住床位.csv", skip = 1)
## New names:
## Rows: 37 Columns: 6
## ── Column specification
## ──────────────────────────────────────────────────────── Delimiter: "," chr
## (3): 床位, 學號, 暑住期間 dbl (3): ...1, 週數, 金額
## ℹ 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.
## • `` -> `...1`
data_male <- data_male_raw |> 
  separate_wider_delim(暑住期間, delim = "~", names = c("入住", "搬出")) |> 
  mutate(
    入住 = as.Date(入住, format = "%m/%d"),
    搬出 = as.Date(搬出, format = "%m/%d")
  ) |>  
  filter(month(入住) == 7) |> 
  mutate(區間 = interval(入住, 搬出)) 

計算每日住宿人數

july <- ymd(paste("2023-7-", c(1:25, 27:31), sep = ""))

data_female_i <- data_female |> 
  pull(區間)

data_female_days <- vector()

for (i in 1:30) {
  data_female_days[i] <- sum(july[i] %within% data_female_i)
}

data_male_i <- data_male |> 
  pull(區間)

data_male_days <- vector()

for (i in 1:30) {
  data_male_days[i] <- sum(july[i] %within% data_male_i)
}

data_total_days <- data_male_days + data_female_days

住宿人數與用電量關係

ggplot(data_dorm, aes(x = date)) +
  geom_line(
    aes(y = 拇山大樓_男生宿舍_總用電/data_male_days, color = "男生宿舍"),
    linewidth = 1
  ) +
  geom_line(
    aes(y = 拇山大樓_女生宿舍_總用電/data_female_days, color = "女生宿舍"),
    linewidth = 1
  ) +
  scale_color_manual(values = c("#C91D42", "#2E45B8")) +
  scale_y_continuous(limits = c(0, 20)) +
  guides(
    color = guide_legend(title = "")
  ) +
  theme(
    legend.position = c(0.9, 0.9),
    text = element_text(family = "Microsoft YaHei")
  ) +
  labs(title = "男女宿每人每日平均用電量", x = "", y = "用電量(度/人)")

用電量定量

計算七月總用電量

data_sum <- data_dorm |> 
  select(ends_with("總用電")) |> 
  colSums()

匯入常用電器用電量

data_power_cost_raw <- read_csv("data/power_cost.csv")
## Rows: 19 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (4): 電器名稱, 每天使用時間估計(時), 推估每日使用度數, 備註
## dbl (1): 消費電力(W)
## 
## ℹ 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.

計算相對於常用電器之用電量

target_item <- c("冷氣機", "電暖爐", "吹風機", "乾衣機")
data_power_cost <- data_power_cost_raw |> 
  select(1:2) |> 
  filter(電器名稱 %in% target_item) |> 
  mutate(
    MVCB = data_sum["拇山大樓_MVCB_總用電"]/`消費電力(W)`*1000/24,
    女生宿舍 = data_sum["拇山大樓_女生宿舍_總用電"]/`消費電力(W)`*1000/24,
    男生宿舍 = data_sum["拇山大樓_男生宿舍_總用電"]/`消費電力(W)`*1000/24
  ) |> 
  pivot_longer(
    all_of(c("MVCB", "女生宿舍", "男生宿舍")),
    values_to = "cost", names_to = "where"
  ) 

相對於常用電器之用電量作圖

ggplot(data_power_cost, aes(x = fct_reorder(where, cost), y = cost)) +
  geom_col(fill = "#FCDE83") +
  geom_text(aes(label = round(cost, digits = 0)), hjust = -0.1) +
  scale_y_continuous(limits = c(0, 6000), labels = scales::comma) +
  coord_flip() +
  labs(
    title = "總用電量換算常見電器使用耗電量",
    x = "", y = "使用時間(天)") +
  theme(
    text = element_text(family = "Microsoft YaHei"),
    axis.text.y = element_text(hjust = 1),
    panel.spacing = unit(2, "lines")
  ) +
  facet_wrap(vars(電器名稱)) 

匯入與合併台北各區用電資訊

data_taipei_raw <- read_csv("data/台北各區用電資訊.csv")
## New names:
## Rows: 14 Columns: 6
## ── Column specification
## ──────────────────────────────────────────────────────── Delimiter: "," chr
## (2): 縣市, 鄉鎮市區 num (3): 售電量(度), 日平均度數, 抄表戶數累計 lgl (1): ...6
## ℹ 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.
## • `` -> `...6`
data_taipei <- data_taipei_raw |> 
  slice_head(n = 13) |> 
  select(鄉鎮市區, `售電量(度)`, 抄表戶數累計) |> 
  mutate(每戶用電量 = `售電量(度)`/抄表戶數累計) |> 
  bind_rows(tibble(
    鄉鎮市區 = "MVCB",
    `售電量(度)` = sum(data_dorm$拇山大樓_MVCB_總用電),
    每戶用電量 = sum(data_dorm$拇山大樓_MVCB_總用電/data_total_days)
  )) |> 
  bind_rows(tibble(
    鄉鎮市區 = "女宿",
    `售電量(度)` = sum(data_dorm$拇山大樓_女生宿舍_總用電),
    每戶用電量 = sum(data_dorm$拇山大樓_女生宿舍_總用電/data_female_days)
  )) |> 
  bind_rows(tibble(
    鄉鎮市區 = "男宿",
    `售電量(度)` = sum(data_dorm$拇山大樓_男生宿舍_總用電),
    每戶用電量 = sum(data_dorm$拇山大樓_男生宿舍_總用電/data_male_days)
  )) |> 
  mutate(
    highlight = ifelse(
      鄉鎮市區 %in% c("MVCB", "女宿", "男宿", "信義區"),
      "1", "0"
    )
  )

台北市各地區住宅與宿舍每戶用電量

ggplot(
  data_taipei,
  aes(x = fct_reorder(鄉鎮市區, 每戶用電量), y = 每戶用電量, fill = highlight)
) +
  geom_col() +
  scale_fill_manual(values = c("#abc6d0", "#FCDE83")) +
  theme(
    legend.position = "none",
    axis.text.y = element_text(hjust = 1)
  ) +
  labs(
    title = "台北市各地區住宅每戶用電量與宿舍用電量",
    caption = "宿舍為每人用電量",
    x = "", y = "每戶用電量(度)"
  ) +
  coord_flip()

Session Info

sessionInfo()
## R version 4.3.2 (2023-10-31 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 11 x64 (build 22621)
## 
## Matrix products: default
## 
## 
## locale:
## [1] LC_COLLATE=Chinese (Traditional)_Taiwan.utf8 
## [2] LC_CTYPE=Chinese (Traditional)_Taiwan.utf8   
## [3] LC_MONETARY=Chinese (Traditional)_Taiwan.utf8
## [4] LC_NUMERIC=C                                 
## [5] LC_TIME=Chinese (Traditional)_Taiwan.utf8    
## 
## time zone: Asia/Taipei
## tzcode source: internal
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] showtext_0.9-6  showtextdb_3.0  sysfonts_0.8.8  ggthemes_5.0.0 
##  [5] patchwork_1.1.3 ggpmisc_0.5.5   ggpp_0.5.5      skimr_2.1.5    
##  [9] lubridate_1.9.3 forcats_1.0.0   stringr_1.5.1   dplyr_1.1.4    
## [13] purrr_1.0.2     readr_2.1.4     tidyr_1.3.0     tibble_3.2.1   
## [17] ggplot2_3.4.4   tidyverse_2.0.0
## 
## loaded via a namespace (and not attached):
##  [1] gtable_0.3.4       xfun_0.41          bslib_0.6.0        lattice_0.21-9    
##  [5] tzdb_0.4.0         vctrs_0.6.4        tools_4.3.2        generics_0.1.3    
##  [9] parallel_4.3.2     fansi_1.0.5        highr_0.10         pkgconfig_2.0.3   
## [13] Matrix_1.6-1.1     lifecycle_1.0.4    farver_2.1.1       compiler_4.3.2    
## [17] MatrixModels_0.5-3 munsell_0.5.0      repr_1.1.6         SparseM_1.81      
## [21] quantreg_5.97      htmltools_0.5.7    sass_0.4.7         yaml_2.3.7        
## [25] pillar_1.9.0       crayon_1.5.2       jquerylib_0.1.4    MASS_7.3-60       
## [29] cachem_1.0.8       ggcorrplot_0.1.4.1 tidyselect_1.2.0   digest_0.6.33     
## [33] stringi_1.8.1      reshape2_1.4.4     labeling_0.4.3     splines_4.3.2     
## [37] fastmap_1.1.1      grid_4.3.2         colorspace_2.1-0   cli_3.6.1         
## [41] magrittr_2.0.3     base64enc_0.1-3    survival_3.5-7     utf8_1.2.4        
## [45] withr_2.5.2        scales_1.2.1       bit64_4.0.5        timechange_0.2.0  
## [49] splus2R_1.3-3      rmarkdown_2.25     bit_4.0.5          hms_1.1.3         
## [53] evaluate_0.23      knitr_1.45         rlang_1.1.2        Rcpp_1.0.11       
## [57] glue_1.6.2         polynom_1.4-1      rstudioapi_0.15.0  vroom_1.6.4       
## [61] jsonlite_1.8.7     plyr_1.8.9         R6_2.5.1