qed
qed

Reputation: 23104

How to adjust title position in ggplot2

Here is the code:

require(ggplot2)
require(grid)
# pdf("a.pdf")
png('a.png')
a <- qplot(date, unemploy, data = economics, geom = "line") + opts(title='A')
b <- qplot(uempmed, unemploy, data = economics) + geom_smooth(se = F) + opts(title='B')
c <- qplot(uempmed, unemploy, data = economics, geom="path") + opts(title='C')
grid.newpage()
pushViewport(viewport(layout = grid.layout(2, 2)))
vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y)
print(a, vp = vplayout(1, 1:2))
print(b, vp = vplayout(2, 1))
print(c, vp = vplayout(2, 2))
dev.off()

And result:

enter image description here

While here is what I would like to have, i.e. to position titles near the top of y-axis:

enter image description here

Upvotes: 19

Views: 43838

Answers (1)

Julius Vainora
Julius Vainora

Reputation: 48211

What you are looking for is theme(plot.title = element_text(hjust = 0)). For example, using the latest version of ggplot2 and theme instead of opts we have

a <- qplot(date, unemploy, data = economics, geom = "line") + ggtitle("A") +
  theme(plot.title = element_text(hjust = 0))

Alternatively, staying with opts

a <- qplot(date, unemploy, data = economics, geom = "line") + 
  opts(title = "A", plot.title = element_text(hjust = 0))

enter image description here

Upvotes: 28

Related Questions