0

私はすでに他のいくつかの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=""))

事前に助けてくれてありがとう!!

4

1 に答える 1