我需要長時間觀察兩個處理(WW和WS)下的葉面積,所以我想繪制一個隨時間變化的直方圖比較圖,但我不知道如何將兩個處理的兩個條形圖放在一個圖形。
資料如下:
cd<-structure(list(Pos_heliaphen = c("Y12", "Y13", "Y20", "Y21",
"Y34", "Y35", "Y42", "Y43", "Z06", "Z07", "Z22", "Z23", "Z36",
"Z37", "Z44", "Z45", "Y12", "Y13", "Y20", "Y21", "Y34", "Y35",
"Y42", "Y43", "Z06", "Z07", "Z22", "Z23", "Z36", "Z37", "Z44",
"Z45"), traitement = c("WW", "WS", "WS", "WW", "WS", "WW", "WS",
"WW", "WW", "WS", "WS", "WW", "WS", "WW", "WW", "WS", "WW", "WS",
"WS", "WW", "WS", "WW", "WS", "WW", "WW", "WS", "WS", "WW", "WS",
"WW", "WW", "WS"), Variete = c("Angelica", "Angelica", "Angelica",
"Angelica", "Angelica", "Angelica", "Angelica", "Angelica", "Angelica",
"Angelica", "Angelica", "Angelica", "Angelica", "Angelica", "Angelica",
"Angelica", "Angelica", "Angelica", "Angelica", "Angelica", "Angelica",
"Angelica", "Angelica", "Angelica", "Angelica", "Angelica", "Angelica",
"Angelica", "Angelica", "Angelica", "Angelica", "Angelica"),
Date_obs = structure(c(19135, 19135, 19135, 19135, 19135,
19135, 19135, 19135, 19135, 19135, 19135, 19135, 19135, 19135,
19135, 19135, 19145, 19145, 19145, 19145, 19145, 19145, 19145,
19145, 19145, 19145, 19145, 19145, 19145, 19145, 19145, 19145
), class = "Date"), SF_Plante_Totale = c(0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67589.64, 71868.33, 69883.2,
73892.79, 72106.38, 82228.68, 73393.92, 63867.78, 70127.46,
64710.27, 76991.58, 75486.69, 80237.34, 74250.9, 68804.73,
63714.6)), row.names = c(NA, -32L), class = c("tbl_df", "tbl",
"data.frame"))
我使用的代碼如下(有多余的操作,因為我的原始資料比較“多樣化”):
df<-subset(cd, Variete %in% c("Angelica"))
options(scipen=200)
df$Date_obs <- as.Date(df$Date_obs)
df %>%
select(1,2,3,4,5) %>%
pivot_longer(starts_with("SF")) %>%
ggplot(aes(Date_obs, value,group=Pos_heliaphen))
geom_bar(stat = "identity",fill="steelblue")
scale_x_date(date_labels = "%Y-%m-%d")
facet_wrap(~Pos_heliaphen,drop=TRUE)
labs(y=expression(paste('Original total leaf area (mm'^2,')')))
theme(legend.position="bottom", axis.text.x = element_text(angle = 90, hjust = 1), axis.title.x = element_blank())
我得到了如下圖:

但我想比較在一張圖中圈在一起的兩個條形圖(一個是 WW,另一個是 WS)。就像下面這張圖。然后最后只剩下8張圖。

因為現在只是觀察的開始,所以我們真的沒有時間線。但下面的兩個數字是去年的。我只是不知道如何將同一天的WS和WW放在一個數字中。

uj5u.com熱心網友回復:
我不確定這是你想要的,但是
df %>%
select(1,2,3,4,5) %>%
pivot_longer(starts_with("SF")) %>%
mutate(w = substr(Pos_heliaphen, start = 1, stop = 2)) %>%
ggplot(aes(Date_obs, value,group=traitement, fill = traitement))
geom_bar(stat = "identity", position = "dodge")
scale_x_date(date_labels = "%Y-%m-%d")
facet_wrap(~w,drop=TRUE)
labs(y=expression(paste('Original total leaf area (mm'^2,')')))
theme(legend.position="bottom", axis.text.x = element_text(angle = 90, hjust = 1), axis.title.x = element_blank())

新的
df %>%
select(1,2,3,4,5) %>%
pivot_longer(starts_with("SF")) %>%
mutate(w = substr(Pos_heliaphen, start = 1, stop = 2)) %>%
group_by(Date_obs, w) %>%
filter(!(sum(value) == 0)) %>%
ggplot(aes(Date_obs, value,group=traitement, fill = traitement))
geom_bar(stat = "identity", position = "dodge")
geom_text(aes(label = Pos_heliaphen), position=position_dodge(width=0.9), size = 2, vjust = -0.5)
ylim(c(0, 90000))
scale_x_date(date_labels = "%Y-%m-%d")
facet_wrap(~w,drop=TRUE)
labs(y=expression(paste('Original total leaf area (mm'^2,')')))
theme(legend.position="bottom", axis.text.x = element_text(angle = 45, hjust = 1), axis.title.x = element_blank())
theme(
strip.background = element_blank(),
strip.text.x = element_blank()
)
labs(title = "Angelica")

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/487255.html
