AllanCameron/geomtextpath

Request something like "dodge" along path

dmoul opened this issue · 3 comments

dmoul commented

Thanks for this package!

Would it be possible to provide more control over where the text / label is placed along the curve? I think in general putting it in the middle is a good guess, but sometime the middle is where things are the most busy, as in the example below.

library(tidyverse)
library(geomtextpath)

set.seed(2022)
tibble(x = seq(0, 1, 0.01),
       a_fairly_long_label = rnorm(x, mean = 0, sd = 1),
       b_fairly_long_label = rnorm(x, mean = 0.5, sd = 1)
) %>%
  pivot_longer(cols = c(a_fairly_long_label, b_fairly_long_label), 
               names_to = "group", values_to = "y") %>%
  ggplot(aes(x, y, linetype = group)) +
  #geom_smooth(se = FALSE) +
  geom_labelsmooth(aes(label = group), method = "loess", formula = y ~ x,
                   se = FALSE) +
  theme_minimal() +
  theme(legend.position = "none")

Created on 2022-02-22 by the reprex package (v2.0.1)

Hi there,

Have you tried playing around with the hjust aesthetic? It control where along the line text is placed. See also this part of the vignette.

You can set the hjust to any value between 0 and 1, or you can map it to an aesthetic (we have included scale_hjust_* functions for this very purpose, which by default dodge labels), or you can even set it to "auto"

library(tidyverse)
library(geomtextpath)

set.seed(2022)
tibble(x = seq(0, 1, 0.01),
       a_fairly_long_label = rnorm(x, mean = 0, sd = 1),
       b_fairly_long_label = rnorm(x, mean = 0.5, sd = 1)
) %>%
  pivot_longer(cols = c(a_fairly_long_label, b_fairly_long_label), 
               names_to = "group", values_to = "y") %>%
  ggplot(aes(x, y, linetype = group)) +
  #geom_smooth(se = FALSE) +
  geom_labelsmooth(aes(label = group), method = "loess", formula = y ~ x,
                   se = FALSE, hjust = "auto") +
  theme_minimal() +
  theme(legend.position = "none")

Created on 2022-02-23 by the reprex package (v2.0.1)

dmoul commented

Wow, very good! Thanks for the explanation. For anyone who comes along after and is reading this:

  • using hjust = 0.2 will put both labels in this case on the left side of the cross-over.
  • using hjust = "auto" in this case puts them at opposite ends as shown above.