user19818282
user19818282

Reputation: 11

Combination of three colors for geom_line()

I have time series data for different experiment settings. These experiments were obtained when three parameters were varied. I need to plot these time series as line graph. Each line should have its own color which depends on input parameter values.
I normalized parameter values, and now they look like that I normalized parameter values, and now they look like that

1 means that parameter has its maximal value for this experiment, and 0 - lowest value. I want each line at my plot to be combination of three different colors. I tried this code:

`results$mixed_color <- rgb(results$rescaled_color1, results$rescaled_color2, results$rescaled_color3)

ggplot(data=results , aes(x= step , y= average, group = Scenario , color = mixed_color )) + geom_line(size=1.5)+ scale_color_manual(values = results$mixed_color, labels = unique(results$Scenario)) `

There is my result

I need to change the code, so I can select my own three colors which will be mixed to plot the lines. Currently, I use the combination of green, red and blue. But I want to replace it by #f0e442,"#0072b2" and "red". How can I do it?

Upvotes: 0

Views: 170

Answers (1)

tjebo
tjebo

Reputation: 23717

You could do this easily with a identity color scale, either using aes(color = I(...)), or, less obscure, + scale_color_identity.

I'm just assuming how your data looks like.

library(tidyverse)

## creating data that should look somewhat like yours
set.seed(42)
df_cols <- setNames(data.frame(replicate(3, runif(10))), c("r", "g", "b"))
df_cols$color <- with(df_cols, rgb(r, g, b))
df_cols$run <- letters[1:nrow(df_cols)]

df_lines <- data.frame(run = rep(letters[1:10], each = 10), x = rep(1:10, 10), y = rnorm(100))

df <- left_join(df_lines, df_cols[c("run", "color")], by = "run")

## now you can basically start here
ggplot(df, aes(x, y, color = color)) +
  geom_line() +
  scale_color_identity()

Created on 2023-05-31 with reprex v2.0.2

Upvotes: 1

Related Questions