I want to set the line N=xxxas the title of my figure, where xxxis the number of observations in the data frame that I pass as an argument datato ggplot(). In my current code, I explicitly pass this data frame a second time as an argument sprintf(), which I use internally labs():
ggplot(mtcars, aes(mpg, hp)) +
labs(title=sprintf("N=%i", nrow(mtcars))) +
geom_point()
This creates the desired header, but it will not work with more complex tasks: I use the channel dplyrto build the data frame that is being created, and since it is a laborious process, I wouldn’t want to repeat the pipe a second time to get the number of rows, as in the example .
So, how do I access the data frame that was passed as an argument ggplot()from the argument specification of the functions that are used to change the graph?
source
share