如何让 geom_text 与 ggplot2 左对齐

How to have the geom_text left-aligned with ggplot2

我想用 ggplot2 左对齐 geom_text 图层。我正在使用以下代码

library(tidyverse)
library(hydroGOF)
library(scales)

summ <- df %>% 
  group_by(Date) %>% 
  summarise(R = cor(x, y, use="pairwise.complete.obs"),
            MBE = me(obs=x, sim=y, na.rm=TRUE),
            RMSE = rmse(obs=x, sim=y, na.rm=TRUE)) %>%
  mutate(across(c(R, RMSE), ~round(., 2)))%>% 
  mutate(lab = paste0("R = ", R, "\nMBE = ", scientific(MBE, digits = 3),
                     "\nRMSE = ", RMSE)) %>% 
  select(Date,lab)

p <- ggplot(data=df, aes(x = x, y = y)) +
  geom_point(fill="lightgray", color = "black",alpha = 1/3) + 
  facet_wrap(Date ~ ., scales="free") +
  geom_smooth(method=lm, fill="lightgray", formula = y ~ x) +
  theme_bw() + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())


p + geom_text(data=summ, aes(x= Inf,y= Inf, label=lab),
            hjust = 1, vjust = 1, size= 4, family = "serif")

从图中可以看出,geom_text 是右对齐的。我想让它左对齐。我从 得知可以使用 hjust = 0 来完成。但是当我在我的代码中给出这个时,文本消失了。现在,我可以让 geom_text 左对齐,如下图所示

df = structure(list(Date = structure(c(18590, 18590, 18590, 18590, 
18590, 18590, 18590, 18590, 18590, 18590, 18590, 18590, 18590, 
18590, 18590, 18590, 18590, 18590, 18590, 18590, 18590, 18590, 
18590, 18590, 18590, 18590, 18590, 18590, 18590, 18590, 18590, 
18590, 18590, 18590, 18590, 18590, 18590, 18590, 18590, 18590, 
18590, 18590, 18590, 18590, 18590, 18590, 18590, 18590, 18590, 
18590, 18590, 18590, 18590, 18590, 18590, 18590, 18590, 18590, 
18590, 18590, 18590, 18590, 18590, 18590, 18590, 18590, 18590, 
18590, 18590, 18590, 18590, 18614, 18614, 18614, 18614, 18614, 
18614, 18614, 18614, 18614, 18614, 18614, 18614, 18614, 18614, 
18614, 18614, 18614, 18614, 18614, 18614, 18614, 18614, 18614, 
18614, 18614, 18614, 18614, 18614, 18614, 18614, 18614, 18614, 
18614, 18614, 18614, 18614, 18614, 18614, 18614, 18614), class = "Date"), 
    x = c(0.0673029521110164, 0.0854163780659409, 0.134210186321464, 
    0.0643945521023818, 0.0534072925548605, 0.120585968026977, 
    0.171766787541948, 0.0529322841140877, 0.0389151124667283, 
    0.065932816556064, 0.0716104225646635, 0.0480367073546261, 
    0.0537788251427076, 0.100325805042952, 0.162239359083627, 
    0.103732829973447, 0.0970357338162893, 0.10603901085665, 
    0.0519899928560691, 0.0881320707200356, 0.0367170059526139, 
    0.121845733235219, 0.122459218176886, 0.0425359089683653, 
    0.0666875634912124, 0.0464243831679725, 0.0664988270937391, 
    0.0777430803707978, 0.0573249856899528, 0.0888259984184133, 
    0.0520454150074231, 0.0633639832058288, 0.0756093060434679, 
    0.0907648362839996, 0.0409180444460348, 0.117772012019805, 
    0.160504051083046, 0.10242517144965, 0.104937932574452, 0.0527662976624684, 
    0.0566003076702597, 0.14336552685814, 0.0338041955729706, 
    0.0364078719337135, 0.0296346837015176, 0.0405151452292794, 
    0.0326617631513927, 0.0383537061987076, 0.0560937860237775, 
    0.0662505348148382, 0.130741306761004, 0.0382284958039881, 
    0.0377009798726168, 0.047621161734172, 0.0356923968905919, 
    0.0516794984004681, 0.0777089536097421, 0.0879614340700801, 
    0.0664604119732582, 0.0951825936041895, 0.0581670432364461, 
    0.0614073175261606, 0.0621596045142233, 0.125973936545876, 
    0.0518735765008042, 0.0582112269727801, 0.0541847270530605, 
    0.0970492801742658, 0.0855089208077924, 0.0900662233655072, 
    0.0553091996406284, 0.045398746284043, 0.0747212626723072, 
    0.0468059595214467, 0.0715252644180638, 0.0392926648647701, 
    0.0401031176420515, 0.0393664661556453, 0.0454529539530989, 
    0.0432804905060738, 0.0928468023207638, 0.0814411322724173, 
    0.0826901703538189, 0.0394121791543224, 0.0846930951222448, 
    0.0851496310212468, 0.0800223696662629, 0.0739057775607218, 
    0.0654718923457129, 0.0597189681733721, 0.0518238375517588, 
    0.0811966642411631, 0.0484268967172088, 0.0398812336217731, 
    0.0408737243973982, 0.0722437508279481, 0.0567807507661876, 
    0.108055811705416, 0.0455713198571915, 0.0911207949375208, 
    0.0562274957486631, 0.0453506947793071, 0.0643281114150967, 
    0.0451974642392855, 0.0583516194132318, 0.0603141601370974, 
    0.0474342976516535, 0.0665490689121699, 0.0656781730798187, 
    0.0809471751886296, 0.0580192064282883), y = c(0.06919, 0.06378, 
    0.1046, 0.0963, 0.08835, 0.14248, 0.13096, 0.09829, 0.04209, 
    0.05926, 0.06029, 0.06975, 0.07061, 0.09682, 0.14988, 0.13158, 
    0.04941, 0.06607, 0.08155, 0.09832, 0.05956, 0.14679, 0.08054, 
    0.04512, 0.0705, 0.05925, 0.08821, 0.09841, 0.08305, 0.07885, 
    0.05957, 0.05032, 0.06938, 0.04775, 0.0528, 0.11204, 0.12149, 
    0.065, 0.09006, 0.05126, 0.05475, 0.11013, 0.06336, 0.04926, 
    0.03237, 0.04511, 0.04268, 0.04304, 0.0537, 0.07389, 0.10452, 
    0.05579, 0.05091, 0.0618, 0.04891, 0.05373, 0.07665, 0.07958, 
    0.05465, 0.08265, 0.06703, 0.04129, 0.05235, 0.11724, 0.06877, 
    0.07079, 0.06365, 0.10306, 0.07579, 0.07818, 0.04264, 0.05456, 
    0.07916, 0.05683, 0.06361, 0.05798, 0.06058, 0.04364, 0.05638, 
    0.05134, 0.07837, 0.08028, 0.07875, 0.06031, 0.09279, 0.07135, 
    0.07104, 0.06089, 0.04861, 0.05516, 0.05096, 0.07485, 0.06711, 
    0.06142, 0.05964, 0.06668, 0.06389, 0.08934, 0.03986, 0.08812, 
    0.05291, 0.04459, 0.04609, 0.05507, 0.04233, 0.04497, 0.05065, 
    0.05746, 0.08027, 0.06171, 0.05248)), row.names = c(NA, 111L
), class = "data.frame")

您可以设置 x = -Infhjust = 0 使其与左侧 y 轴对齐。由于我没有一些必需的包,所以我用一个标准数据集来说明这一点。

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.0.5

ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  facet_wrap(~ cyl) +
  geom_text(
    data = ~ subset(.x, !duplicated(cyl)),
    aes(x = -Inf, y = Inf, label = paste0("Cyl = ", cyl)),
    hjust = 0, vjust = 1, size = 10
  )

reprex package (v1.0.0)

于 2021-07-09 创建

编辑:

位置美学 x 和 y 的 -InfInf 参数表示应将某些东西放置在轴的最极端位置。 -Inf 个位置位于 bottom/left,Inf 个位置位于 top/right,具体取决于使用哪个位置美学。

与常见的误解相反,hjust 不是 文本的水平对齐,而是与文本方向平行的对齐。同样,vjust 不是文本的垂直对齐,而是垂直于文本方向的对齐。当 angle = 0 时,误解是正确的,但对于其他角度则不然。 'justification' 我的意思是特定字符串相对于同一系列中最大 width/height 字符串的放置位置。设置 hjust = 0 对齐文本的开头,而 hjust = 1 对齐文本的结尾。 When other justifications are chosen such as -1 or 2, it just means to place text 1 string-width before the position or 1 string-width after the 'natural' end position.因此,将一系列文本的对齐设置为 0.5,因为它将移动半个字符串宽度(vjust 的字符串高度)。另请参阅 以获得视觉解释。