---
# System prepended metadata

title: R語言做出折線圖_臺灣巢蕨在兩種處理下每月葉面積變化

---

###### tags: R_code
# R語言做出折線圖_臺灣巢蕨在兩種處理下每月葉面積變化
:::danger
:warning: 請尊重著作權，本文僅供教學使用，勿作為利益用途(商業、抄襲)
:thumbsup: 作者還是R和HackMD的初學者，歡迎來信指教：berry15815@gmail.com
:smile: [作者的開放資料庫](https://knb.ecoinformatics.org/profile/http://orcid.org/0009-0003-1196-2272)
:::
## 實驗設計介紹
研究對象為臺灣巢蕨(*Asplenium nidus*)，在溫室內種植，進行給予不同水分和施肥的處理。
處理上分為給予高水組與低水組兩組，兩組再依施肥各分為四小組，分別為控制組(無施肥)、施氮肥、施磷肥、施氮磷肥，共八小組(如下表)。
每一組有七株臺灣巢蕨，共56株。實驗每個月測量一次每株的葉面積，從10月份開始到隔年10月，為期一年。
| 實驗處理  | 高水      | 低水 |
| -------- | -------- | -------- |
| 控制組    | 第一組     | 第五組 |
|施氮      |第二組      |第六組    |
|施磷      |第三組    |第七組    |
|施氮磷    |第四組     |第八組    |
## Raw data frame 
大致如圖所示
:warning:兩圖以高水和低水，分為兩個獨立資料表:warning:
number：每一株臺灣巢蕨的編號，為1~56號
month：紀錄葉面積的月份，從10月到隔年10月，共一整年
treatment：施肥處理
ratio：葉面積比值，可視為葉面積
![](https://i.imgur.com/Nsl0nWj.png =35%x)![](https://i.imgur.com/ibMpUpq.png =35%x)
## 成果圖
![](https://i.imgur.com/QZJAEA9.png =60%x)
## 製圖流程
```mermaid
graph LR

做高水組的折線圖 --> 做低水組的折線圖 --> 合併兩圖 
```
## 第一部分-做高水組的折線圖
### 讀取資料

raw data的路徑，建議資料夾名稱寫英文，不要寫中文或是有空格。
把raw data匯入R裡，取名為highwater_ratio，raw data原本的名稱為highwater_ratio，是csv檔。
```
setwd("C:/Users/user/Desktop/R的資料")
highwater_ratio = read.csv ("highwater_ratio.csv", header = T)
```
看資料的前6項、確認資料項目
```
head(highwater_ratio)
dim(highwater_ratio)
```
### 安裝Package
**如果你已經安裝過了就不用再跑一次**
```
install.packages("Rmisc")
install.packages("ggplot2")
```
### 叫醒Package
```
library(Rmisc)
library(ggplot2)
```
### 分組、橫軸按照你要的排列順序
告訴R你的raw data frame各項是什麼
命名 <- 子集(檔案名,欄位=="欄位的名稱")
```
control <- subset(highwater_ratio, treatment == "control")
N <- subset(highwater_ratio, treatment == "N")
P <- subset(highwater_ratio, treatment == "P")
NP <- subset(highwater_ratio, treatment == "NP")
Oct. <- subset(highwater_ratio, month == "Oct.")
Nov <- subset(highwater_ratio, month == "Nov")
Dec <- subset(highwater_ratio, month == "Dec")
Jan <- subset(highwater_ratio, month == "Jan")
Feb <- subset(highwater_ratio, month == "Feb")
Mar <- subset(highwater_ratio, month == "Mar")
Apr <- subset(highwater_ratio, month == "Apr")
May <- subset(highwater_ratio, month == "May")
Jun <- subset(highwater_ratio, month == "Jun")
Jul <- subset(highwater_ratio, month == "Jul")
Aug <- subset(highwater_ratio, month == "Aug")
Sep <- subset(highwater_ratio, month == "Sep")
Oct <- subset(highwater_ratio, month == "Oct")
```
as.factor:轉換資料型態
levels:排序，先寫的在前，後寫的在後
summarySE:做總結分組
```
highwater_ratio$month <- as.factor(highwater_ratio$month)
highwater_ratio$month <- factor(highwater_ratio$month, levels = c( "Oct.", "Nov", "Dec", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct"))
highwater_ratio$treatment <- as.factor(highwater_ratio$treatment)
highwater_ratio$treatment <- factor(highwater_ratio$treatment, levels = c("control","N", "P", "NP"))
highwater_ratio <- summarySE(data = highwater_ratio, measurevar = "ratio", groupvars = c("month","treatment"))
```
:bulb:總結後的data frame(大致如下圖)：實驗設計的八小組平均和標準誤(差)，才能畫error bar
![](https://i.imgur.com/9UMroGS.png =80%x)

### 畫折線圖
```
highwater_line <- #給圖命名
  ggplot(highwater_ratio, aes(x = month, y = ratio, group = treatment, color = treatment))+#用ggplot製圖，color上色
  theme_bw()+#背景為白色
  geom_errorbar(aes(ymax = ratio + se, ymin = ratio - se),
                width =.1,position = position_dodge())+#加上errorbar，設定寬度、位置
  geom_line(size = 1.3)+#加上折線
  geom_point()+#加上平均數的點
  theme_classic()+#去除圖右側框線
  theme(
    text = element_text(size = 30),
    axis.text.x = element_blank(),
    axis.title.y = element_text(size = 32),
    legend.position = c(0.16, 0.82),
    legend.background = element_rect(fill = "transparent"))+#改Y軸字體大小、去除X軸文字、圖例位置及圖例背景為透明
  scale_colour_discrete(name = "Nutrient adding")+##圖例標題
  labs(x = "", y = "Leaf area ratio")#XY軸的文字內容，X軸為空白的

highwater_line#出圖
```
### 高水組的折線圖
![](https://i.imgur.com/YD2SuKh.png =48%x)

## 第二部分-做低水組的折線圖
跟高水組的code幾乎一樣，在此略過解釋
```
setwd("C:/Users/user/Desktop/R的資料")
lowwater_ratio = read.csv ("lowwater_ratio.csv", header = T)
head(lowwater_ratio)
dim(lowwater_ratio)

install.packages("Rmisc")
library(ggplot2)
library(Rmisc)

control <- subset(lowwater_ratio, treatment == "control")
N <- subset(lowwater_ratio, treatment == "N")
P <- subset(lowwater_ratio, treatment == "P")
NP <- subset(lowwater_ratio, treatment == "NP")
Oct. <- subset(lowwater_ratio, month == "Oct.")
Nov <- subset(lowwater_ratio, month == "Nov")
Dec <- subset(lowwater_ratio, month == "Dec")
Jan <- subset(lowwater_ratio, month == "Jan")
Feb <- subset(lowwater_ratio, month == "Feb")
Mar <- subset(lowwater_ratio, month == "Mar")
Apr <- subset(lowwater_ratio, month == "Apr")
May <- subset(lowwater_ratio, month == "May")
Jun <- subset(lowwater_ratio, month == "Jun")
Jul <- subset(lowwater_ratio, month == "Jul")
Aug <- subset(lowwater_ratio, month == "Aug")
Sep <- subset(lowwater_ratio, month == "Sep")
Oct <- subset(lowwater_ratio, month == "Oct")

#橫軸按照你要的排列順序
lowwater_ratio$month <- as.factor(lowwater_ratio$month)
lowwater_ratio$month <- factor(lowwater_ratio$month, levels = c("Oct.","Nov", "Dec", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct"))
lowwater_ratio$treatment <- as.factor(lowwater_ratio$treatment)
lowwater_ratio$treatment <- factor(lowwater_ratio$treatment, levels = c("control","N", "P", "NP"))
lowwater_ratio <- summarySE(data = lowwater_ratio, measurevar = "ratio", groupvars = c("month","treatment"))

#畫折線圖
lowwater_line <- 
  ggplot(lowwater_ratio, aes(x = month, y = ratio, group = treatment, color = treatment))+
  theme_bw()+
  geom_line()+
  geom_errorbar(aes(ymax = ratio + se, ymin = ratio - se),
                width =.1,position = position_dodge())+
  geom_line(size = 1.3)+
  geom_point()+
  theme_classic()+
  theme(
    text = element_text(size = 30),
    axis.title.x = element_text(size = 32),
    axis.title.y = element_text(size = 32),
    legend.position = "none" #移除圖例)+
  labs(x = "Month", y = "Leaf area ratio")

lowwater_line
```
### 低水組的折線圖
![](https://i.imgur.com/jAu16vt.png =48%x)

## 第三部分-合併兩圖
必須先跑過之前圖的code才能使用這個code
```
library(installr)
library(ggpubr)
figure <- ggarrange(highwater_line, lowwater_line,
                    labels = c("A High water", "B Low water"), #要合併的圖名、分別上標示
                    font.label = list(size = 28),
                    ncol = 1, nrow = 2, align = "v")#標示的字體大小、圖為一列
figure
dev.print(tiff, "new.tif", height = 19, width = 13, res = 300, units="in", compression="lzw")#出圖
```
## 附註 - 寫好的code一開變亂碼怎麼辦
如圖
![](https://i.imgur.com/JC68pjR.png =55%x)

```mermaid
graph LR

點選左上File --> Reopen_with_Encoding --> 選擇UTF-8  --> ok
```
![](https://i.imgur.com/sMUKOLf.png =50%x)








