Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Place a grey box around chart title in ggplot2

Tags:

r

ggplot2

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??")
like image 936
user08041991 Avatar asked May 11 '16 08:05

user08041991


People also ask

How do you change the color of the title in ggplot2?

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.

How do you put a title in the middle of a Ggplot?

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)) .

Which ggplot2 function is called to add a title to a chart?

You add a chart title with the ggtitle() function.

How do I add a title to a Ggplot in R?

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.


1 Answers

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.

enter image description here

like image 112
coffeinjunky Avatar answered Sep 21 '22 15:09

coffeinjunky