Bubs
Bubs

Reputation: 105

ggplot2 - two or more lines on y axis title

I need two or more lines of text on my y-axis title. However, the title is cut off if I have multiple lines. Anyway I can work around this?

ggplot(iris,aes(Sepal.Length, Sepal.Width)) +geom_line()+labs(y=expression(paste("line 1 \nline2")))

enter image description here

Upvotes: 2

Views: 2545

Answers (2)

akrun
akrun

Reputation: 887118

We can use ylab

library(ggplot2)
ggplot(iris,aes(Sepal.Length, Sepal.Width)) +
  geom_line()+
  ylab(bquote("Mean Annual \nAir Temperature"~degree*"C")) +   
  theme(plot.margin = margin(1, 1, 0.5, 0.5, "cm"))

-output

enter image description here

Upvotes: 3

Greg
Greg

Reputation: 3660

Play with plot.margin in theme to change the whitespace around your plot

ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
  geom_line() +
  labs(y = expression(paste("line 1\nline2"))) +
  theme(plot.margin = margin(1, 1, 1, 1, "cm")
  )

enter image description here

Upvotes: 3

Related Questions