這是我現在擁有的用于創建我想要的圖形的編碼。不過,這僅適用于“aa1”井,我想學習如何制作一個回圈,以便我可以為我的所有井制作此圖。有任何想法嗎?
longer_raindata %>%
select(well, metal, level, smpl_mth) %>%
filter(well == 'aa1') %>%
ggplot(aes(metal, level, fill = smpl_mth))
scale_fill_manual(values = c('plum', 'lightsteelblue', 'darkolivegreen', 'indianred'))
geom_col(position = "dodge")
labs(title = 'Metals in Well AA1',
x = 'Metals',
y = 'μg/l')
uj5u.com熱心網友回復:
實作您想要的結果的一種選擇是將您的代碼放入一個繪圖函式中,該函式接受一個引數.well并調整您的過濾器陳述句以過濾您的資料.well。
然后,您可以使用 eglapply回圈遍歷unique資料集中的井以獲取每個井的繪圖串列。
使用一些虛假的示例資料:
library(ggplot2)
library(dplyr)
longer_raindata <- data.frame(
metal = LETTERS[1:3],
level = 1:6,
well = rep(c("aa1", "aa2"), each = 6),
smpl_mth = rep(letters[1:2], each = 3)
)
plot_fun <- function(.well) {
longer_raindata %>%
select(well, metal, level, smpl_mth) %>%
filter(well == .well) %>%
ggplot(aes(metal, level, fill = smpl_mth))
scale_fill_manual(values = c('plum', 'lightsteelblue', 'darkolivegreen', 'indianred'))
geom_col(position = "dodge")
labs(title = paste0('Metals in Well ', toupper(.well)),
x = 'Metals',
y = 'μg/l')
}
lapply(unique(longer_raindata$well), plot_fun)
#> [[1]]

#>
#> [[2]]

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