Search code examples
rggplot2geom-text

How to have the geom_text left-aligned with ggplot2


I want to left-justify geom_text layer with ggplot2. I am using the following code

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")

enter image description here

As you can see from the plot that the geom_text is right-aligned. I want to make it left-aligned. I came to know from this post that it can be done using hjust = 0. But when I am giving this in my code the text disappears. Now, can I have the geom_text left aligned like the following plot enter image description here

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")

Solution

  • You can just set x = -Inf and hjust = 0 to make it aligned to the left y-axis. As I didn't have some of the required packages, I've illustrated this with a standard dataset.

    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
      )
    

    Created on 2021-07-09 by the reprex package (v1.0.0)

    EDIT:

    The -Inf and Inf argument to the position aesthetics x and y indicate that something should be placed at the most extreme position of an axis. -Inf positions are placed at the bottom/left and Inf positions are placed at the top/right, depending on which position aesthetic is used.

    Contrary to a common misconception, hjust is not the horizontal justification of the text but it is the justification parallel to the direction of the text. Likewise, vjust is not the vertical justification of the text but the justification orthogonal to the direction of the text. When angle = 0 the misconception is true, but not for other angles. By 'justification' I mean where a particular string is placed in relation to the maximum width/height of strings in the same series. Setting hjust = 0 aligns text at their beginnings, whereas hjust = 1 aligns text at their ends. 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. Hence, setting a justification of 0.5 centers a series of text, because it will be moved half a string-width (string-heights for vjust). See also this answer of mine for a visual explanation.