1. 設置

setwd("D:/R_dir/fun")

library(tidyverse)

dta <- read.csv("./data/BaoData0430.csv", head = T)

2. 檢視一下資料結構

DT::datatable(dta)
str(dta)
## 'data.frame':    26 obs. of  10 variables:
##  $ 史書    : chr  "史記" "漢書" "後漢書" "三國志" ...
##  $ 主編    : chr  "漢 司馬遷" "漢 班固" "宋 范曄" "晉 陳壽" ...
##  $ 內容體例: chr  "書/禮、樂、律、歷、天官、封禪、河渠、平準" "志/律歷、禮樂、刑法、食貨、郊祀、天文、五行、地理、溝洫、藝文" "志/律歷、禮儀、祭祀、天文、五行、郡國、百官、輿服" "無志" ...
##  $ 禮排序  : int  1 2 2 0 4 3 1 0 0 4 ...
##  $ 總排序  : int  8 10 8 0 10 9 8 0 0 10 ...
##  $ 禮卷數  : int  1 1 3 0 3 5 2 0 0 4 ...
##  $ 總卷數  : int  130 120 130 65 130 100 59 56 36 114 ...
##  $ 起      : int  -2500 -206 25 184 179 420 479 502 557 386 ...
##  $ 迄      : int  -90 23 220 280 420 749 502 557 589 550 ...
##  $ 主編年  : int  -104 58 398 274 646 487 487 629 629 483 ...

3. 相關係數矩陣

表格呈現兩兩連續變數的相關係數。

library(psych)

DT::datatable(round(cor(dta[, -(1:3)]), 4))
# cor.plot(round(cor(dta[, -(1:3)]), 3))

4. 畫圖前整理資料

dta$禮排序[dta$禮排序 == 0] <- NaN
dta$史書排序 <- 1:26
dta$禮pr <- ((dta$總排序 - dta$禮排序) / dta$總排序)*100
dta$禮卷數百分比 <- dta$禮卷數 / dta$總卷數

theme_set(theme_bw())

5.〈禮〉的統計圖

a1.〈禮〉的排序歷代變化

將Y軸翻轉,使得排序為1的會在水平位置最高的地方

ggplot(dta, aes(`史書排序`, `禮排序`))+
  geom_point(size = rel(2), na.rm = TRUE)+
  scale_y_reverse(breaks = 1:9)+
  scale_x_continuous(breaks = 1:26,
                     labels = dta$史書)+
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))+
  labs(x = "史書", y = "〈禮〉的排序")

a2.〈禮〉的排序排序歷代變化

將整個X-Y軸翻轉,使得史書名稱不用旋轉90度

ggplot(dta, aes(`史書排序`, `禮排序`))+
  geom_point(size = rel(2), na.rm = TRUE)+
  scale_x_reverse(breaks = 1:26, labels = dta$史書)+
  scale_y_continuous(breaks = 1:9)+
  coord_flip()+
  labs(x = "史書", y = "〈禮〉的排序")

b1.〈禮〉的排序PR值歷代變化

後來我選擇保留史書,但不呈現其點,且移除連線,僅保留X軸標籤

ggplot(dta, aes(`史書排序`, `禮pr`))+
  geom_point(size = rel(2), na.rm = T)+
  scale_x_continuous(breaks = 1:26,
                     labels = dta$史書)+
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))+
  labs(x = "史書", y = "〈禮〉的排序PR值")

b2.〈禮〉的排序PR值歷代變化

將整個X-Y軸翻轉,使得史書名稱不用旋轉90度

ggplot(dta, aes(`史書排序`, `禮pr`))+
  geom_point(size = rel(2), na.rm = T)+
  scale_x_reverse(breaks = 1:26, labels = dta$史書)+
  coord_flip()+
  labs(x = "史書", y = "〈禮〉的排序PR值")

c.〈禮〉的卷數歷代變化

我改成長條圖,點實在太難看了

ggplot(dta, aes(`史書排序`, `禮卷數`))+
  geom_bar(stat = "identity", fill = "gray", color = "black")+
  scale_x_continuous(breaks = 1:26, labels = dta$史書)+
  scale_y_continuous(breaks = seq(5, 30, 5))+
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))+
  geom_text(aes(label = `禮卷數`), vjust=-0.25)+
  labs(x = "史書", y = "〈禮〉的卷數")

d.〈禮〉的卷數比歷代變化

ggplot(dta, aes(`史書排序`, `禮卷數百分比`))+
  geom_bar(stat = "identity", fill = "gray", color = "black")+
  scale_x_continuous(breaks = 1:26, labels = dta$史書)+
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))+
  labs(x = "史書", y = "〈禮〉的卷數比")