0

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?

  • It's not clear to me what you mean with "each line at my plot to be combination of three different colors". Do you want just to have three different colors for three different lines? Check https://stackoverflow.com/questions/5171263/changing-line-colors-with-ggplot and of course https://stackoverflow.com/questions/3777174/plotting-two-variables-as-lines-using-ggplot2-on-the-same-graph for how to shape the data in order to plot more than one line more efficiently – tjebo May 31 '23 at 14:17
  • Sorry for the confusion. I need that there should be one line, not three. But depending on a parameters' values the color of this line will be different. For example, if I have 1 1 1 for all three parameters, it means that these parameters have maximal values, and in rgb() its white (#FFFFFF). But instead of rgb() I want to use my own colors whose mix will be used for each line (one line - one color) – user19818282 May 31 '23 at 14:21
  • OK, I think I got it now. – tjebo May 31 '23 at 14:21
  • of another note, not sure this is a great idea what you're doing, as I understand that you're basically representing three dimensions with only one dimension. I don't think that most people will see based on a single color which of those dimensions (R/G/B) has been modified. – tjebo May 31 '23 at 14:44

1 Answers1

0

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

tjebo
  • 21,977
  • 7
  • 58
  • 94
  • Thank you for your reply! I have one point to clarify. Where do you specify which colors will be used to make unique colors for each line? In your example it is still rgb, right? How to modify this part to customize the range of colors which will be used to create mixes for each line? – user19818282 May 31 '23 at 14:47
  • 1
    @user19818282 I'm not sure I understand. I was using your way to convert your values to Hex values with `rgb`. If you need more information/clarity, please provide a proper reproducible example which also includes data in the actual shape of yours. Otherwise, not possible to help you better than that I'm afraid. – tjebo May 31 '23 at 15:50
  • Sorry that I wasn't specific. What I meant is that in your code you are using rgb colors: df_cols$color <- with(df_cols, rgb(r, g, b)). But how to replace rgb colors by three other colors. For example, the code can choose not between red, green and blue, but instead between yellow, pink and white. I hope it is clear what I wanted to say. Basically, can I create my own set of colors that will be mixed between each other to color lines (one line - one color). – user19818282 May 31 '23 at 15:59
  • 1
    @user19818282 I think this goes deeply into color theory and depends on the color space in which you want to express and mix your colors. You might want to have a look at the [colorspace package](https://cran.r-project.org/package=colorspace) – tjebo May 31 '23 at 16:05