I want to make a graph for each value of a variable in my dataframe, and then pass that value through to the graph as the title. I think the best way to do this is by using the apply() family of functions, but i'm a bit of a novice and can't figure out how to do that.
For example, say I have this dataframe:
df <- data.frame(month=c("Chair", "Table", "Rug", "Wardrobe", "Chair", "Desk", "Lamp", "Shelves", "Curtains", "Bed"),
cutlery=c("Fork", "Knife", "Spatula", "Spoon", "Corkscrew", "Fork", "Tongs", "Chopsticks", "Spoon", "Knife"),
type=c("bbb", "ccc", "aaa", "bbb", "ccc", "aaa", "bbb", "ccc", "aaa", "bbb"),
count=c(341, 527, 2674, 811, 1045, 4417, 1178, 1192, 4793, 916))
I could manually go through and select on the value of type doing this:
df %>%
filter(type=='aaa') %>%
ggplot() +
geom_col(aes(month, count)) +
labs(title = 'value of {{type}} being plotted')
df %>%
filter(type=='bbb') %>%
ggplot() +
geom_col(aes(month, count)) +
labs(title = 'value of {{type}} being plotted')
df %>%
filter(type=='ccc') %>%
ggplot() +
geom_col(aes(month, count)) +
labs(title = 'value of {{type}} being plotted')
But this quickly becomes a lot of code, with enough levels of type and assuming a fair amount of additional code for each plot. Let's also assume that I don't want to use facet_wrap(~type). As you can see, the values of the x variable vary quite a lot across values of type so facet_wrap() results in lots of missing spaces along the x-axis. Ideally i'd just create a function that takes as an input the x and y variables, and type and then filters on type, makes the plot, and extracts the value of type to use in the title.
Can anyone help me out with this?


