---
# 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)
:::

## 實驗設計介紹
研究對象為臺灣巢蕨，在溫室內種植，進行給予充足的水分，以及缺水的兩組處理。
一組28株，共56株。實驗期間為期一年，每個月進行一次葉片測量。

## Raw data frame 
number：每一株臺灣巢蕨的編號
month：月份，共12個月份
Treatment：高水和低水處理
ratio：葉面積比值(可以視作為葉面積)
![](https://i.imgur.com/2TSFmSX.png =40%x)

## 成果圖
![](https://i.imgur.com/BrKJAGZ.png =80%x)

## 開始寫Code
### 讀取資料
raw data的路徑，建議資料夾名稱寫英文，不要寫中文或是有空格。
把raw data匯入R裡，取名為Ca_value，raw data原本的名稱為Ca，是csv檔。
```
setwd("C:/Users/user/Desktop/R的資料")
water = read.csv ("water.csv", header = T)
```
### 安裝Package
**如果你已經安裝過了就不用再跑一次**
```
install.packages("Rmisc")
install.packages("ggplot2")
```
### 叫醒Package
```
library(Rmisc)
library(ggplot2)
```
### 分組、橫軸按照你要的排列順序
告訴R你的raw data frame各項是什麼。
命名 <- 子集(檔案名,欄位=="欄位的名稱")
```
High <- subset(water, Treatment == "High")
Low <- subset(water, Treatment == "Low")
Nov <- subset(water, month == "Nov")
Dec <- subset(water, month == "Dec")
Jan <- subset(water, month == "Jan")
Feb <- subset(water, month == "Feb")
Mar <- subset(water, month == "Mar")
Apr <- subset(water, month == "Apr")
May <- subset(water, month == "May")
Jun <- subset(water, month == "Jun")
Jul <- subset(water, month == "Jul")
Aug <- subset(water, month == "Aug")
Sep <- subset(water, month == "Sep")
Oct <- subset(water, month == "Oct")
```
as.factor:轉換資料型態
levels:排序，先寫的在前，後寫的在後
summarySE:做總結分組
```
water_treatment <- summarySE(data = water, measurevar = "ratio", groupvars = c("month","Treatment"))
water_treatment$month <- as.factor(water_treatment$month)
water_treatment$month <- factor(water_treatment$month, levels = c("Nov", "Dec", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct"))
water_treatment$Treatment <- as.factor(water_treatment$Treatment)
water_treatment$Treatment <- factor(water_treatment$Treatment, levels = c("High","Low"))
```
:bulb:總結後的data frame：實驗設計的每個月兩組平均和標準誤(差)(大致如下圖)，才能畫error bar
![](https://i.imgur.com/3gqLxTE.png =55%x)
### 畫折線圖
```
water_line <- #取圖名
  ggplot(water_treatment, aes(x = month, y = ratio, group = Treatment, color = Treatment))+#用ggplot製圖
  theme_bw()+#背景為白色
  geom_line()+#加上折線
  labs(x = "Month", y = "Leaf area ratio")+#XY軸的文字內容
  geom_errorbar(aes(ymax = ratio + se, ymin = ratio - se),
                width =.1,position = position_dodge())+#加上errorbar，設定寬度、位置
  geom_point()+#加上平均數的點
  theme_classic()+#去除圖右側框線
  theme(
    text = element_text(size = 16),
    axis.title.x = element_text(size = 23),
    axis.title.y = element_text(size = 23),
    axis.text.x = element_text(size = 16),
    axis.text.y = element_text(size = 17),
    legend.position = c(0.2, 0.85),
    legend.background = element_rect(fill = "transparent"))+#改XY軸字體大小、圖例位置及圖例背景為透明
  scale_colour_discrete(name = "Watering treatment")+#圖例標題
  scale_y_continuous(breaks = seq(0.4, 3, 0.4), limits = c(0.4, 3))#Y值範圍以及間距(範圍0.4~3，每0.4為一間隔)
 
water_line #把圖叫出來
```
:::spoiler 目前圖長這樣
![](https://i.imgur.com/iXQMneL.png =80%x)
:::
### 上顯著符號
可以接續上一code中直接按"+"繼續寫，但我個人偏好分開另外寫
```
wtotal <- water_line + 
  annotate("text", x = 1.95, y = 1.5, label = "***", size = 6)+
  annotate("text", x = 2.95, y = 1.55, label = "***", size = 6)+
  annotate("text", x = 3.95, y = 1.85, label = "***", size = 6)+
  annotate("text", x = 4.95, y = 2.0, label = "***", size = 6)+
  annotate("text", x = 5.95, y = 2.0, label = "***", size = 6)+
  annotate("text", x = 6.95, y = 2.2, label = "***", size = 6)+
  annotate("text", x = 7.95, y = 2.35, label = "***", size = 6)+
  annotate("text", x = 8.95, y = 2.33, label = "***", size = 6)+
  annotate("text", x = 9.95, y = 2.3, label = "***", size = 6)+
  annotate("text", x = 10.95, y = 2.05, label = "***", size = 6)+
  annotate("text", x = 11.95, y = 1.9, label = "***", size = 6)
  
wtotal
```
### 存圖
* Tiff檔
```
dev.print(tiff, "water_high_low_line.tif", height = 4, width = 7.3, res = 300, units="in", compression="lzw")
```
:bulb:這個code一但執行就會直到執行完成才會結束，如果後面還有很多code要跑會打結，或者寫loop的話會停不下來，這時候可以搭配使用`dev.off()`
* Jpg檔
```
ggsave(filename = "FIG. 1.jpg", width = 7.3, height = 4, units = "in", dpi = 300)
```
## 附註-存圖的資料夾路徑

```mermaid
graph TD

點選右側Files --> 點選再右邊的... --> 點選齒輪圖案的More --> 選擇Set_as_working_directory --> 選擇你要存圖的資料夾
```
![](https://i.imgur.com/PWludc2.png)

![](https://i.imgur.com/hc70Mis.png =30%x)

