0

I'm trying to create an image similar to this one in R using ggplot2.

However, I'm new to using this package. I'm struggling to find out how to draw lines that each have a different gradient. I want each line to start with one colour and end in another colour (gradually changing throughout), and I want to be able to specify this for each individual line uniquely. Can I do this with geom_segment? Would it also be possible for curves with geom_curve? It seems that the package ggforce could be useful for this. Any help would be greatly appreciated! Thank you.

Rhondson
  • 3
  • 1
  • 2
    Hello and welcome to stack overflow. Please provide us some example code for (at least) the lines you want to draw and if you have it, some code for an attempt. See the [guide for asking good R questions](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). That said, yes, you'd probably want `geom_link()` from the ggforce package. – teunbrand Apr 08 '21 at 20:55

1 Answers1

1

This is the best I could pull together in 20 minutes to just illustrate that ggforce can be handy.

library(ggplot2)
library(ggforce)

n <- 1000
df <- data.frame(
  x = runif(2 * n),
  id = rep(seq_len(n), each = 2),
  y = rep(c(0:1), n)
)

g <- ggplot(df, aes(x = x, y = y)) +
  geom_link2(aes(group = id, colour = x),
             alpha = 0.3) +
  scale_colour_gradientn(colours = rainbow(100),
                         guide = "none") +
  theme_void() +
  theme(plot.background = element_rect(fill = "black"))

enter image description here

teunbrand
  • 33,645
  • 4
  • 37
  • 63