我有以下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"))

我希望標簽位于相同的位置(即 x = Inf 和 y = Inf),但我還希望左對齊,以便三行中的每一行都從相同的 x 坐標開始。我怎樣才能做到這一點?
uj5u.com熱心網友回復:
如果您可以選擇使用另一個包,那么您可以使用ggtext::geom_textboxwhich 通過引數halign允許在右對齊文本框中左對齊標簽來實作您想要的結果:
注意:除了一些調整洗掉默認框布局注意,使用時geom_textbox我們必須替換\n為 html 標簽<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)

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/529695.html
標籤:rggplot2
