I have a dataframe with 2 factors, called dat.avg
> head(dat.avg)
metal site season Average STDV
1 Al Acre spring 19 4034.7345 222.4645
2 Al Acre summer 5860.1790 2940.4691
3 Al Ashqelon autumn 2148.6943 403.5688
4 Al Ashqelon spring 19 3336.1576 871.4381
5 Al Ashqelon spring 20 2170.9057 589.2840
6 Al Ashqelon summer 918.3047 413.6208
> str(dat.avg)
'data.frame': 351 obs. of 5 variables:
$ metal : chr "Al" "Al" "Al" "Al" ...
$ site : Factor w/ 7 levels "Acre","Ashqelon",..: 1 1 2 2 2 2 2 3 3 3 ...
$ season : Factor w/ 5 levels "autumn","spring 19",..: 2 4 1 2 3 4 5 1 3 5 ...
$ Average: num 4035 5860 2149 3336 2171 ...
$ STDV : num 222 2940 404 871 589 ...
I want to produce a loop that will make a bar plot for each metal using ggplot, where X axis is grouped by sites, and in each site all the seasons are nested (same for all metals so no need to change). Y axis is the Average, scale is different for each metal so its 'scale = free' . I managed to do it for every metal separately, but for some reason, no matter what I tried, face isn't working. This is my code, if someone can point put what am I doing wrong it will be amazing.
> metal.vec
[1] "Al" "As" "Cd" "Co" "Cr" "Cu" "Fe" "Mg" "Mn" "Ni" "Pb" "Se" "V" "Zn"
for (j in 1:length(metal.vec)) {
temp.dat.avg <- dat.avg %>%
filter(site == dat.avg$site,
metal == metal.vec[j],
Average == dat.avg$Average,
season == dat.avg$season)
metal.bar <- ggplot(temp.dat.avg, aes(x = site, y = Average, fill = season)) +
geom_bar(stat='identity',color="black", position=position_dodge())+
theme_minimal() +
#facet_wrap(~ metal.vec[j])+
#facet_grid(vars(metal.vec[j]), scales = "free", space = "free") +
geom_errorbar(aes(ymin=Average-STDV, ymax=Average+STDV), width=.2,
position=position_dodge(.9)) +
ggtitle(metal.vec[j], "Average and SD Concentration")+
ylab("Average Metal Concentration in dry weight (µg/g)")
print(metal.bar + scale_fill_brewer(palette="Spectral"))
}
I've put both the faceting code with "#" just so you can see #facet_wrap(~ metal.vec[j])+ #facet_grid(vars(metal.vec[j]), scales = "free", space = "free") neither one have worked for me
My idea is to produce this graph:this kind of bar plot
with facet on metal that will look something like that : this kind of faceting
I'll appreciate any help! thank a lot!
The comments are generally correct. Ultimately, you should only need facet_wrap(~metal).
You should look at dplyr's group_by and summarise prior to plotting. Use group_by to group by site, season, and metal, then create a new column, say GroupAverage, using summarise, eg:
Then you can get rid of the exterior loop and have a single call to ggplot with the facet_wrap layer.
Again, generally, if you start using explicit loops with R, you're probably doing something inefficiently.
Good luck.