I have the following ggplot.
library(ggplot2)
ggplot()
geom_point(aes(x= c(5,2), y = c(3,4)))
geom_text(aes(x = Inf, y = Inf, hjust = 1, vjust = 1, label = "Label \nexample \n1"))
I want the label to be in the same position (i.e., x = Inf and y = Inf) but I also want to left justify so that each of the three lines start at the same x coordinate. How can I do this?
CodePudding user response:
If using another package is an option for you then you could achieve your desired result using ggtext::geom_textbox
which via the argument halign
allows to left justify the label inside a right justified text box:
Note: Besides some adjustments to remove the default box layout note that when using geom_textbox
we have to replace \n
by the html tag <br>
.
library(ggplot2)
base <- ggplot()
geom_point(aes(x= c(5,2), y = c(3,4)))
base
ggtext::geom_textbox(aes(x = Inf, y = Inf, hjust = 1, vjust = 1, label = "Label<br>example<br>1"),
halign = 0, valign = 1, width = NULL,
box.padding = unit(rep(0, 4), "pt"),
box.size = 0,
fill = NA)