Reputation: 1807
The default ggplot axis titles are the variable names:
library(ggplot2)
ggplot(mtcars, aes(cyl, wt)) +
geom_point()
Created on 2021-09-10 by the reprex package (v0.3.0)
I want to change my plot so that the x-axis title is instead the subtitle. I.e., something like + labs(x = NULL, subtitle = "cyl")
. Is there a generalized way to do this?
Upvotes: 0
Views: 711
Reputation: 2783
I am not sure what your reason is to put it as a subtitle. Perhaps it is more worthwhile to simply move the position of the x-axis label?
ggplot(mtcars, aes(cyl, wt)) +
geom_point() +
scale_x_discrete(position = "top")
Upvotes: 0
Reputation: 388797
You may try -
library(ggplot2)
plot_data <- function(data, xvar, yvar) {
name <- deparse(substitute(xvar))
ggplot(data, aes({{xvar}}, {{yvar}})) +
geom_point() +
labs(x = NULL, subtitle = name)
}
plot_data(mtcars, cyl, wt)
Upvotes: 2