-1

I am working with R+RStudio and I want to save for different files a ggplot but for each of those, a different filter so I want a plot per subtype I have available. This should be in my opinion something very easy, I know how to save files and I've done it before. But I don't know why, when I iterate through the different subtypes to save those plots (that if I execute one by one, I can see that they actually plot) do not plot at my destination folder, just a white file.

So my code to show a bit what I am doing is this:

shoton  <- incidents %>% filter(type == 'shoton')
data <- unique(shoton$subtype1)
for(i in data){
  fileName <- paste0("factorsImages/fouls/",i, ".png")
  png(filename = fileName, bg="transparent")
  shoton %>% 
    filter(subtype1 == i) %>%
    ggplot(mapping = aes(x = lon, y = lat)) +
    geom_jitter(mapping = aes(colour = subtype1), alpha = 0.3, size = 2, stroke = 0) +
    scale_color_viridis(discrete = T) +
    guides(colour = guide_legend(override.aes = list(alpha = 1))) +
    facet_wrap(~half) +
    theme_minimal()
  shoton
  dev.off()
}

and maybe is useful to see how the data looks:

> head(shoton,10)
# A tibble: 10 x 13
       id subtype2 subtype1     player1 player2 team    lon   lat elapsed elapsed_plus game_id  half half_elapsed
    <int> <fct>    <fct>        <chr>   <chr>   <chr> <int> <int>   <dbl>        <dbl>   <int> <dbl>        <dbl>
 1 378828 <NA>     blocked_shot 24154   <NA>    10260    NA    NA       3            0    1729     1            3
 2 378866 <NA>     header       24157   <NA>    10260    NA    NA       7            0    1729     1            7
 3 378922 <NA>     shot         30829   <NA>    10260    NA    NA      14            0    1729     1           14
 4 378923 <NA>     shot         30373   <NA>    10260    NA    NA      14            0    1729     1           14
 5 378951 <NA>     distance     30373   <NA>    10260    NA    NA      17            0    1729     1           17
 6 379204 <NA>     blocked_shot 24154   <NA>    10260    NA    NA      43            0    1729     1           43
 7 379363 <NA>     distance     37799   <NA>    10261    NA    NA      50            0    1729     2           50
 8 379401 <NA>     distance     24157   <NA>    10260    NA    NA      58            0    1729     2           58
 9 379406 <NA>     blocked_shot 24157   <NA>    10260    NA    NA      58            0    1729     2           58
10 379414 <NA>     blocked_shot 30829   <NA>    10260    NA    NA      60            0    1729     2           60

I am pretty sure that has to be something very silly, but to be honest, I feel like I have been spending too much time on this and my research did not help in solving the issue. Any kind of feedback would be appreciated,

Thanks!

user123
  • 175
  • 4
  • 16

1 Answers1

1

print the plot in for loop :

library(dplyr)
library(ggplot2)

shoton  <- incidents %>% filter(type == 'shoton')
data <- unique(shoton$subtype1)

for(i in data){
  fileName <- paste0("factorsImages/fouls/",i, ".png")
  png(filename = fileName, bg="transparent")
  plot <- shoton %>% 
    filter(subtype1 == i) %>%
    ggplot(mapping = aes(x = lon, y = lat)) +
    geom_jitter(mapping = aes(colour = subtype1), alpha = 0.3, size = 2, stroke = 0) +
    scale_color_viridis(discrete = T) +
    guides(colour = guide_legend(override.aes = list(alpha = 1))) +
    facet_wrap(~half) +
    theme_minimal()
  print(plot)
  dev.off()
}
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • ok, yes. In my defense, i had before the assignment ot a variable but did not print xd, never thought that would need of the print for it. Thank you! – user123 Sep 16 '20 at 03:44