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:
While here is what I would like to have, i.e. to position titles near the top of y-axis:
You can adjust the position of the title with the adj argument, that takes values from 0 (left-justified) to 1 (right-justified). Default value is 0.5. However, if you specify the argument adj inside your plotting function all text will be adjusted. If you only want some texts adjusted use it inside the title function.
position of the theme function allows aliging the caption to the panel ( "panel" , default) or the whole plot (“ plot ”). Note that the default for the caption is right alignment, so you can set hjust = 0 to move the caption to the left of the whole plot.
Directly by specifying the titles to the plotting function (ex : plot() ). In this case titles are modified during the creation of plot. the title() function can also be used. It adds titles on an existing plot.
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))
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