I have a function that takes a dataset, filters for a given element, and creates a plot. I want to take a vector and use map to filter for each element of the vector and create a plot. Applying map(vector, function)
doesn't work--I get the warning
"longer object length is not a multiple of shorter object length".
Say I have this dataframe names
, based on the babynames dataset.
names <- babynames%>%
pivot_wider(names_from = sex, values_from = n, values_fill=0) %>%
group_by(year,name) %>%
summarize(Female=sum(F), Male=sum(M)) %>%
mutate(percent = Female/(Male+Female)*100)
and a vector made up of `"Ollie"`, `"Verna"`, and `"Ava"`. I want a graph for each of `Ollie`, `Verna`, and `Ava`.
fun_name <- function(name_1) {
names %>%
filter(name==name_1) %>%
ggplot(aes(x=year, y=percent)) +
geom_line()
}
map(vector, fun_name)
The issue I'm specifically having now is that all of the graphs are combined into one.