I am trying to automate my bar graphs by writing a function which will take different datasets and x and y axis values.
Since the function has three arguments(dataset, x-axis and y-axis), I am using pmap() from purrr.
The function works fine when I remove the dataset argument and use map2() instead of pmap()
Here's the code I have written:
forest_day <- forest %>% group_by(day) %>% summarise(n())%>%rename(count1 = `n()`)
forest_month <- forest %>% group_by(month) %>% summarise(n())%>%rename(count1 = `n()`)
pbar <- function(data, x, y) {
ggplot(data = data) + aes_string(x = x, y = y) + geom_bar(stat = 'identity')
}
l1 <- list(forest_month, forest_month$month, forest_month$count1)
pmap(l1, pbar)
l2 <- list(forest_day, forest_day$day, forest_day$count1)
pmap(l2,pbar)
The error code I get when using pmap() is this:
"Element 1 of .l
must have length 1 or 12, not 2"
Thanks in advance for help!