0

I am trying to add title for all my plot in a loop, here is what I have done:

first I split the data with the split() fuction:

sep_team_season_stage <- split(data[cols], 
                              list(data$Season,data$Stage,data$Team),drop = TRUE)

then I want to plot a bunch of radar graph: so I use a loop:

for (i in sep_team_season_stage){
  print(ggRadar(i, aes(group = "Team")))
}

And then I want to add tilte for the graph: I saw "titles" produces by the split function is good so I want to recall them:

plotnames = names(sep_team_season_stage)

and added into the for loop:

for (i in sep_team_season_stage){
  a=1,
  print(ggRadar(i, aes(group = "Team"))+ ggtitle(plotnames[a])),
  a= a+1
}

It doesnt work however, how can I fix it?

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Nathan Chan
  • 147
  • 1
  • 13

1 Answers1

0

No data to test this but in such situations when you want to access the names of the list you can use imap from purrr :

plot_list <- purrr::imap(sep_team_season_stage, 
                        ~ggRadar(.x, aes(group = "Team"))+ ggtitle(.y))

use Map and pass both name and data.

plot_list <- Map(function(x, y) ggRadar(x, aes(group = "Team")) + ggtitle(y), 
                 sep_team_season_stage, names(sep_team_season_stage))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • I used the map function and it worked! but how do I show the graph from plot_list? – Nathan Chan Oct 24 '20 at 14:53
  • You can get individual plots using `plot_list[[1]]`, `plot_list[[2]]` etc. If you want to plot list of plots in one graph check https://stackoverflow.com/questions/10706753/how-do-i-arrange-a-variable-list-of-plots-using-grid-arrange – Ronak Shah Oct 24 '20 at 15:05