I have 18 different species with size classes and count of observations for each size class. I am trying to create a for loop that will create a separate histogram for each species (not facet as there are too many species). For loops are my weakest area in R and I have often done more code to avoid them but with 18 species that is no longer an option.
Here is a sample of my formatted data:
Species Size.Class TotalCount
P. porphyreus 35 1
P. porphyreus 20 5
P. porphyreus 25 5
P. insularis 35 2
P. insularis 5 10
P. insularis 10 10
P. insularis 30 12
P. insularis 25 35
P. insularis 15 36
P. insularis 20 36
P. cyclostomus 30 2
P. cyclostomus 35 2
P. cyclostomus 25 4
P. cyclostomus 15 7
P. cyclostomus 20 8
When I create a histogram for one species I get the intended result:
ggplot(subset(Spcount,Species %in% c("P. porphyreus")),aes(x=Size.Class))+
geom_histogram(binwidth=5)+
ggtitle("P. porphyreus Histogram")+
labs(y= "Total Count", x = "Size Class")
But when I try to automate it using this for loop:
FOR (i in Spcount$Species) {
ggplot(subset(Spcount,Species %in% c("i")),aes(x=Size.Class))+
geom_histogram(binwidth=5)+
ggtitle("i Histogram")+
labs(y= "Total Count", x = "Size Class")
}
I get one graph titled "i Histogram" but is blank with no errors or warnings.