After setting ncol = 1 in the facet_wrap()
function, I'm trying to use ggtitle()
function inside the facet_wrap()
function to set a different title for each graph created (there are only two of them).
ggplot(df, aes(x, y))
geom_point()
facet_wrap(~ var, ncol = 1)
ggtitle(function(x) paste("Title for", df$title[df$var == x]))
I'm trying to use the value of the "title" column of the dataframe, where the value of the "var" column matches the value of the current plot's var.
But I get this error:
Error in as.character(x$label) :
cannot coerce type 'closure' to vector of type 'character'
How can I set different titles for each graph in ggplot2 using the facet_wrap() function with ncol=1?
Thanks, Ido
CodePudding user response:
Here is something that might give you what you want. My comment above still stands, but this may be more what you are looking for.
library(ggplot2)
library(dplyr)
df <- mtcars %>%
mutate(strip_title = paste(cyl, "Cylinders"))
ggplot(df, aes(x = mpg, y = wt))
geom_point()
facet_wrap(~strip_title, ncol = 1)