我已經嚴重依賴其他幾個 SO 帖子,但似乎無法通過這個帖子。以下是我使用過的參考資料:
在多個資料幀上使用定義的 ggplot 函式回圈
在 R 中回圈以創建和保存具有指定名稱的一系列 ggplot2 圖
我的目標是使用回圈從資料框串列中保存每個餅圖:“Sample_List”(會更長)。不過,我不斷收到此錯誤,并被難住了:
"Error: Aesthetics must be either length 1 or the same as the data (1): fill, y"
資料:
DZmix_SC1:
# A tibble: 3 × 4
Sample_ID Potential_Sources Relative_Contribution Metric
<chr> <chr> <dbl> <chr>
1 SC1_18 Uintas 0 KV
2 SC1_18 Sierra Madre 22 KV
3 SC1_18 CMB 78 KV
DZmix_5_SC:
# A tibble: 3 × 4
Sample_ID Potential_Sources Relative_Contribution Metric
<chr> <chr> <dbl> <chr>
1 5-SC_18 Uintas 0 KV
2 5-SC_18 Sierra Madre 29 KV
3 5-SC_18 CMB 71 KV
DZmix_PL3:
# A tibble: 3 × 4
Sample_ID Potential_Sources Relative_Contribution Metric
<chr> <chr> <dbl> <chr>
1 PL3_18 Uintas 69 KV
2 PL3_18 Sierra Madre 0 KV
3 PL3_18 CMB 31 KV
這是我到目前為止所擁有的:
Sample_list <- c("DZmix_SC1", "DZmix_5_SC", "DZmix_PL3")
DZpie.fn <- function(df,title) {
df <- df %>%
mutate(Relative_Contribution = round(Relative_Contribution,1)) %>%
arrange(desc(Potential_Sources))
ggpie(df,"Relative_Contribution", label = "Relative_Contribution",
fill = "Potential_Sources", color = "white", size = 1.5,
palette = c("#636363", "#cccccc", "#969696"))
lab.pos = c("in"),
lab.font = c(0, "bold", "black"))
theme(legend.position = "none",
panel.background = element_rect(fill = "transparent"),
plot.background = element_rect(fill = "transparent", color = NA))
} #end DZpie.fn
for(i in Sample_list){
print(DZpie.fn(get(i), i))
}
最終我想用一個有效的 ggsave 函式替換回圈中的列印函式......這是我的努力:
ggsave(DZpie.fn, filename=paste("/outputpath/",i,".png",sep=""))
在此先感謝您的幫助!!
uj5u.com熱心網友回復:
這對我有用
library(tibble)
library(dplyr)
library(ggpubr)
DZmix_SC1 <- tibble(
Sample_ID = rep('SC1_18', 3),
Potential_Sources = c('Uintas', 'Sierra Madre', 'CMB'),
Relative_Contribution = c(0,22,78),
Metric = rep('KV', 3)
)
DZmix_5_SC <- tibble(
Sample_ID = rep('5-SC_18', 3),
Potential_Sources = c('Uintas', 'Sierra Madre', 'CMB'),
Relative_Contribution = c(0,29,71),
Metric = rep('KV', 3)
)
DZmix_PL3 <- tibble(
Sample_ID = rep('PL3_18', 3),
Potential_Sources = c('Uintas', 'Sierra Madre', 'CMB'),
Relative_Contribution = c(69,0,31),
Metric = rep('KV', 3)
)
Sample_list <- c("DZmix_SC1", "DZmix_5_SC", "DZmix_PL3")
DZpie.fn <- function(df,title) {
df <- df %>%
mutate(Relative_Contribution = round(Relative_Contribution,1)) %>%
arrange(desc(Potential_Sources))
ggpie(df, "Relative_Contribution", label = "Relative_Contribution",
fill = "Potential_Sources", color = "white", size = 1.5,
palette = c("#636363", "#cccccc", "#969696"),
lab.pos = c("in"),
lab.font = c(0, "bold", "black"))
theme(legend.position = "none",
panel.background = element_rect(fill = "transparent"),
plot.background = element_rect(fill = "transparent", color = NA))
}
for(i in Sample_list){
print(DZpie.fn(get(i), i))
}
你的方法其實是對的。你只是錯過了擺 在面前lab.pos = c("in")。
然后你可以使用保存影像
for (i in Sample_list){
ggsave(DZpie.fn(get(i), i), filename=paste0("temp/",i,".png"))
}
或等效但沒有 for 回圈
purrr::walk(Sample_list, function(name) ggsave(DZpie.fn(get(name), name),
filename=paste0("temp/",name,".png")))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/381231.html
上一篇:從兩個不同的圖表創建一個組合的單個條形圖或直方圖,條形并排第1年和第2年
下一篇:RI繪制對數-指數擬合的置信區間
