jzadra
jzadra

Reputation: 4284

ggplot: extend title beyond plot margin

I have a plot with some long labels and a long title. I'm using coord_flip(), so there is a lot of space outside of the plot margins due to the labels. By default the title is constrained to the plot margin.

How can I get the title to extend beyond the plot margin (to the left) so that it can span the entire width? Here is an example:

ggplot(diamonds, aes(x = cut)) + 
  geom_bar() + 
  coord_flip() + 
  scale_x_discrete("Cut", 
                   labels = c("Fair" = "Fair and a very long extra thing",
                              "Good" = "Good  and a very long extra thing",
                              "Very Good" = "VG  and a very long extra thing", 
                              "Premium" = "P and a very long extra thing. A very long label, just because", 
                              "Ideal" = "I and a very long extra thing")) + 
  ggtitle("This is my very long title.  Long long title. Very long.  The Longest. We have the best long titles.")

My plot with long labels/title

Upvotes: 5

Views: 1937

Answers (2)

Boden_Units
Boden_Units

Reputation: 133

Adding

+ theme(plot.title.position = "plot")

also does the trick, and allows the plot to span the whole width of the plot, even if it is just a short title. I thought I'd leave this here because it took me a while to remember this solution after trying to search for it for a long time, and finding this page multiple times in the process.

Upvotes: 7

joran
joran

Reputation: 173537

Maybe try this:

+ theme(plot.title = element_text(hjust = 0.95))

to move the title over.

Upvotes: 6

Related Questions