I have a dataframe (locs.new) of daily distances traveled by radiotagged birds. I'm trying to create a function that calls a for loop that creates a subset for the ith sparrow (identified by a character variable "anillo") and then ggplot a bar graph with the xAxis=Number of the Location (NoFrec) and yAxisDistance traveled (Distance). This is what I have tried:
   plotDist <- function (x) {
     v<-levels(locs.new$anillo)
     as.vector(v)
     for (i in v) {
       print(i)
       da<-subset(locs.new,anillo==i)
       dat<-subset(da, select = c(NoFrec, Distance))
       plot(dat$NoFrec~ dat$Distance)
       print(ggplot(dat, aes(x=NoFrec, y= Distance))) +geom_col() +    ggtitle(as.character(i))
     }
   }
   plotDist(locs.new)
If I isolate the code from the for loop and individually create each dataset it works perfectly fine:
i<-"XEN010"
da<-subset(locs.new,anillo==i)
dat<-subset(da, select = c(NoFrec, Distance))
plot(dat$NoFrec~ dat$Distance)
print(ggplot(dat, aes(x=NoFrec, y= Distance))) +geom_col() + ggtitle(as.character(i))
resulting in: enter image description here but the problem arises when I insert this code in the for loop. It plots the graphs...but empty of bars and without the Title. Thanks in advance for any suggestions!
 
     
    