I am trying to "split" the text of the label line in two so one label appears below the point and the other above, but I am not able to do it. Is it possible to do it with geom_text?
Hi @jynusmac. Below is one approach, which creates a variable in the data (adjust) that is passed to the vjust
argument in geom_text()
.
library(tidyverse)
# sample data
df = mtcars |>
arrange(desc(mpg)) |>
mutate(row = row_number()) |>
mutate(adjust = ifelse(row_number() %% 2 == 1, 2, -2))
ggplot(df, aes(x = row, y = mpg)) +
geom_point() +
geom_text(aes(x = row, y = mpg, label = mpg, vjust = adjust))
Created on 2023-12-06 with reprex v2.0.2
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.