when we use the facet
option in ggplot, we get a beautiful grey box around the headings (3,4,5).
library(ggplot2)
data(mtcars)
ggplot(mtcars, aes(cyl)) + geom_bar() + facet_wrap(~gear) + theme_bw()
How can we place a similiar grey box around the chart title when we do no use the facet
option?
ggplot(mtcars, aes(cyl)) + geom_bar() + theme_bw() +
ggtitle("How do you put a grey box around me??")
To change the color of X-axis label using ggplot2, we can use theme function that has axis. title. x argument which can be used for changing the color of the label values.
The default ggplot title alignment is not centered. It is on the left. It's possible to put the title in the middle of the chart by specifying the argument hjust = 0.5 in the function element_text() : p + theme(plot. title = element_text(hjust = 0.5)) .
You add a chart title with the ggtitle() function.
To add a title to your plot, add the code +ggtitle("Your Title Here") to your line of basic ggplot code. Ensure you have quotation marks at the start and end of your title.
Since you are asking how to do it without facets, this is strictly speaking not an answer, but just to point it out, one quick and dirty way would be to "cheat" and to use a facet after all. For instance,
mtcars$title <- "How do you put a grey box around me??"
ggplot(mtcars, aes(cyl)) + geom_bar() + theme_bw() +
facet_grid(. ~ title)
does the trick.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With