2022-01-19

How to draw a multi-colored dashed line (alternating colors for visual effect)

I was wondering if it is possible to create a multicolored dashed line in ggplot. Basically I have a plot displaying savings based on two packages.

A orange line with savings based on package A A green line with savings based on package B

I also have a third line and I would like that one to be dashed alterenating between orange and green. Is that something that somebody has been able to do?

Here is an example:

library(tidyverse)

S <- seq(0, 5, by = 0.05)

a <- S ^ 2
b <- S
a_b = a + b #This data should have the dashed multicolor line, since it is the sum of the other two lines.

S <- data.frame(S)
temp <- cbind(S, a, b, a_b)
temp <- gather(temp, variable, value, -S)
desiredOrder <- c("a", "b", "a_b")
temp$variable <- factor(temp$variable, levels = desiredOrder)
temp <- temp[order(temp$variable),]

p <- ggplot(temp, aes(x = S, y = value, colour = variable)) +
  theme_minimal() +
  geom_line(size = 1) +
  scale_color_manual(name = "Legend", values = c("orange", "green", "#0085bd"),
                     breaks = c("a", "b", "a_b"))
p

I basically want to have a multicolored (dashed or dotted) line for "c"

enter image description here



from Recent Questions - Stack Overflow https://ift.tt/33Ixz4e
https://ift.tt/33Md5aX

No comments:

Post a Comment