I made a loop to make a plot for every unique value of a variable within a group. To make my code reproducible I used nyflights13 package. Unfortunately, in here my code gives desired result. In my data however I would have flight origins that don't happen in a certain year, giving me an empty plot for that origin in that year. I would like that in one group (in this example year), Only the origins that happened in that year are shown. Could somebody help me out?
library(nycflights13)
library(tidyverse)
plotter_de_plot<-function(origination, YEARR){
eval(substitute(origination), flights)
eval(substitute(YEARR), flights)
flights %>%
subset(year==YEARR)%>%
select(month,origin,hour,year)%>%
group_by(origin, month) %>%
mutate(AMOUNT = (sum(hour, na.rm=TRUE))) %>%
filter(!is.na(hour),
origin==origination,year==YEARR) %>%
ggplot(aes(month,AMOUNT), na.rm = TRUE)+
geom_point() +
labs(title=origination,subtitle=YEARR)
}
for (i in unique(flights$origin)){
plot(plotter_de_plot(i,2013))
}



forloop, addif (with(flights, sum(year == 2022 & origin == "EWR") == 0)) next?