been an R user for around a year now - so still have tons to learn. Anyways my question is:
- How do I export the ggplots within the list and name them specifically based on their location.
Below is the dummy data I have created with where I am at.
location <- c('obs1', 'obs2', 'obs3', 'obs4', 'obs5', 'obs6')
percentage <- c(40, 30, 20, 40, 20, 30)
mean_percentage <- c(30, 30, 30, 30, 30, 30)
dat1 <- data.frame(location, percentage, mean_percentage)
plot.func <- function(dat) {
dat %>%
pivot_longer(cols = 2:3, names_to = 'place', values_to = 'percent') %>%
ggplot(aes(x = place, y = percent, fill = place)) +
geom_bar(stat = 'identity')
}
myplots <- list()
myplots <- lapply(1:nrow(dat1), function(i) plot.func(dat1[i,]))
The above code produces the list called 'myplots' which contains 6 ggplots.
I would like to export these individually with names based on the location variable.
For example: obs1.barchart.pdf, obs2.barchart.pdf, obs3.barchart.pdf and so on.
I've tried various different ways but going round in circles. Any help much appreciated.
Best, TheBoomerang