如何将R中的循环与ggpie函数一起使用,并在数据帧后保存文件名



我已经非常依赖其他几篇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函数来替换循环中的print函数。。。这是我的努力:

ggsave(DZpie.fn, filename=paste("/outputpath/",i,".png",sep=""))

提前感谢您的帮助!!

这适用于我的

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"))
}

或等效,但无环路

purrr::walk(Sample_list, function(name) ggsave(DZpie.fn(get(name), name), 
filename=paste0("temp/",name,".png")))

相关内容

最新更新