Recommand · October 14, 2021 0

Creating linetype from string in ggplot

I would like to experiment in ggplot with an idea. I have 10 points between 0-5 on the x axis and the corresponding y values for several data series. Now I am making a scatterplot connected with lines. To diffentiate between series types, I would try to use a linetype, where every dot/dash is replaced with an ‘A’, so instead of the lines, there will be a series of A-s. How could I do this? I tried to label every point, but since it is only 10/series, they are not really forming a continuous line. Any ideas?

Thank you!

ps. I realize it migt look horrible, but would like to give it a try first.

I’m not sure I’m imagining this correctly, but something like the following? Below, we interpolate the x-y series to place a later at the interpolated points.

library(ggplot2)
library(dplyr)

df <- expand.grid(
  series = LETTERS[1:3],
  x = seq(0, 5, length.out = 10)
) %>%
  mutate(y = rnorm(nrow(.))) %>%
  group_by(series) %>%
  summarise(
    xout = seq(min(x), max(x), length.out = 100),
    yout = approx(x, y, xout = xout)$y
  )
#> `summarise()` has grouped output by 'series'. You can override using the `.groups` argument.

ggplot(df, aes(xout, yout)) +
  geom_text(aes(label = series, colour = series))

Created on 2021-10-14 by the reprex package (v0.3.0)