My example is:
qplot(mtcars$mpg) +
annotate(geom = "text", x = 30, y = 3, label = "Some text\nSome more text")
How do I get the text here to be left aligned? So that the 'Some's line up with each other.
hjust = 0 or hjust = "left" does what you want. hjust stands for horizontal justification, 0 will be left-justified, 0.5 will be centered, and 1 will be right-justified. You can also use hjust = "inward" or hjust = "outward" to align text in toward the center or out away from the center.
qplot(mtcars$mpg) +
annotate(geom = "text", x = 30, y = 3,
label = "Some text\nSome more text",
hjust = 0)
See also vjust for vertical justification.
In ggplot2, these arguments are present any time text preferences are set. They work for annotate, geom_text, or in element_text when adjusting theme options.
There's a bit of a difference in how text justification works in ggplot compared to how you may be used to in in a GUI document or slides editor. In those GUIs, you draw a textbox, and then you justify the text within that box--left justified goes to the left edge of the box, right justified goes to the right edge, and center in the middle.
In ggplot, there is no text box. You specify a single point for the text, and the justification touches that. You may expect that if you change from left-justified to right-justified, the text will move to the right, but it will actually move to the left, as it is the right margin that is moving. This code illustrates:
hjust_data = data.frame(z = paste("hjust =", c(0, 0.5, 1)))
df = data.frame(x = 1:5, y = 1:5)
df = merge(df, hjust_data, all = TRUE)
text_df = subset(df, x == 3) |>
transform(lab = "Your nice\nmultiple line text\nis here")
ggplot(df, aes(x, y)) +
geom_point() +
facet_wrap(vars(z), ncol = 1) +
geom_text(aes(label = lab), data = text_df[1, ], hjust = 0) +
geom_text(aes(label = lab), data = text_df[2, ], hjust = 0.5) +
geom_text(aes(label = lab), data = text_df[3, ], hjust = 1)
base graphicsThis behavior is similar in many base graphics functions, such as the adj argument for par, used by text(), mtext(), and title(), which can be vector of length 2 for the horizontal and vertical justificatons. Also the hadj and padj arguments to axis() for justifications horizontal to and perpendicular to the axis.
... Other arguments passed on to layer(). These are often aesthetics, used to set an aesthetic to a fixed value, like colour = "red" or size = 3. They may also be parameters to the paired geom/stat. ... which is not very helpful.annotate lets you use most any geom, here geom = "text" was used, so the ?geom_text help page has the relevant details. ?geom_text has a heading for Alignment, which begins "You can modify text alignment with the vjust and hjust aesthetics. These can either be a number between 0 (right/bottom) and 1 (top/left) or a character..."geom_text for text aesthetics related to annotate. It's obvious to those that already know! But to the newcomer it is not at all obvious that that is where you should look. The man would be better to mention (at least some of) the various geom_*() help pages by name.