Help with Plot colour by combination of factors

Not sure how I can explain myself better so I produced a reprex to illustrate what I am trying to solve. Do not bother trying to understand what the data means.

What I want is just to plot d vs c and group them by a. In other words I want to color all the results which belong to container "1234" in red and the results from "1256" in blue. I need to colour=paste(a,b))) because other wise if I do colour = a it would group different experiments conduced on different days together. So instead of having 6 different colour I need to plot 6 data set with two different colors the colour being determined by a

a <- c("1234","1234","1234","1234","1234","1234","1234","1234","1234","1256","1256","1256","1256","1256","1256","1256","1256","1256") 
b <- c("21-Jun-21","21-Jun-21","21-Jun-21","22-Jun-21","22-Jun-21","22-Jun-21","23-Jun-21","23-Jun-21","23-Jun-21","21-Jun-21","21-Jun-21","21-Jun-21","22-Jun-21","22-Jun-21","22-Jun-21","23-Jun-21","23-Jun-21","23-Jun-21")
c <- c(0.2,0.3,0.5,0.02,0.05,0.04,0.08,0.09,0.1,0.7,0.02,0.3,0.1,0.05,0.06,0.1,0.5,0.9)
d <- c(1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3)

df <- data.frame(a,b,c,d)

ggplot(data=df, aes(x=d, y=c, colour=paste(a,b))) + geom_line()

which produces this plot

Maybe so:

library(ggplot2)
a <- c("1234","1234","1234","1234","1234","1234","1234","1234","1234",
       "1256","1256","1256","1256","1256","1256","1256","1256","1256") 
b <- c("21-Jun-21","21-Jun-21","21-Jun-21","22-Jun-21","22-Jun-21","22-Jun-21","23-Jun-21","23-Jun-21","23-Jun-21",
       "21-Jun-21","21-Jun-21","21-Jun-21","22-Jun-21","22-Jun-21","22-Jun-21","23-Jun-21","23-Jun-21","23-Jun-21")
c <- c(0.2,0.3,0.5,0.02,0.05,0.04,0.08,0.09,0.1,0.7,0.02,0.3,0.1,0.05,0.06,0.1,0.5,0.9)
d <- c(1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3)
e <- paste(a,b)

df <- data.frame(a,b,c,d,e)

p <- ggplot(data=df, aes(x=d, y=c, colour=e)) + geom_line()

cols <- c('red','blue')[match(df$a,unique(df$a))]
names(cols) <- df$e

f <- match(unique(df$a),df$a)

p + scale_colour_manual(values = cols, 
                        breaks = df$e[f],
                        labels = df$a[f]) + 
  labs(colour="legend")


Created on 2021-07-26 by the reprex package (v2.0.0)

This produces:
image

Thank you for your reply.

I was able to get something similar to what you produced using this: ggplot(data=df, aes(x=d, y=c, colour=a, group=interaction(a, b))) + geom_line()

However the problem is that the legend section shows only two things. I would like to keep each individual line in the legend. The reason is because ultimately I use this in plotly and I would like to be able to hide/show individual plots right now it will hide 3 plots

In that case leave out the breaks and labels arguments

p + scale_colour_manual(values = cols) + 
  labs(colour="legend")
1 Like

You're the best. Thanks for your help much appreciated

One more thing if I have a dynamic number of unique factors in a. How can I change this part to change dynamically?

cols <- c('red','blue')[match(df$a,unique(df$a))]
names(cols) <- df$e```

If you know beforehand the maximum number of unique factors that you can have, you can type that many color names in stead of red and blue. That is the extent of my ideas.
But have a look at this page .

Yes that's what I did but in some instance I have an unknown number which truthfully it gets so messy that I still need to figure out how to filter the data again. I managed to use RcolorBrewer to have 8 different colors. But I will take a look at what you linked. Thanks very much your help has solved something which has been bothering me for a while now.


library(ggplot2)
library(RColorBrewer)

a <- c("1234","1234","1234","1234","1234","1234","1234","1234","1234",
       "1256","1256","1256","1256","1256","1256","1256","1256","1256") 
b <- c("21-Jun-21","21-Jun-21","21-Jun-21","22-Jun-21","22-Jun-21","22-Jun-21","23-Jun-21","23-Jun-21","23-Jun-21",
       "21-Jun-21","21-Jun-21","21-Jun-21","22-Jun-21","22-Jun-21","22-Jun-21","23-Jun-21","23-Jun-21","23-Jun-21")
c <- c(0.2,0.3,0.5,0.02,0.05,0.04,0.08,0.09,0.1,0.7,0.02,0.3,0.1,0.05,0.06,0.1,0.5,0.9)
d <- c(1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3)
e <- paste(a,b)

df <- data.frame(a,b,c,d,e)

cols <- brewer.pal(n = 8, name = "Dark2")[match(df$a,unique(df$a))]
names(cols) <- df$e

p <- ggplot(data=df, aes(x=d, y=c, colour=e)) + geom_line()

p + scale_colour_manual(values = cols) + 
  labs(colour="legend")

I am leaving this here just in case someone else stumbles across this. This will render a dynamic number of colors from the default ggplot palette. Once again most of the credit goes to @HanOostdijk for pointing me in the right direction towards a complete solution.

library(ggplot2)
library(scales)

a <- c("1234","1234","1234","1234","1234","1234","1234","1234","1234",
       "1256","1256","1256","1256","1256","1256","1256","1256","1256") 
b <- c("21-Jun-21","21-Jun-21","21-Jun-21","22-Jun-21","22-Jun-21","22-Jun-21","23-Jun-21","23-Jun-21","23-Jun-21",
       "21-Jun-21","21-Jun-21","21-Jun-21","22-Jun-21","22-Jun-21","22-Jun-21","23-Jun-21","23-Jun-21","23-Jun-21")
c <- c(0.2,0.3,0.5,0.02,0.05,0.04,0.08,0.09,0.1,0.7,0.02,0.3,0.1,0.05,0.06,0.1,0.5,0.9)
d <- c(1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3)
e <- paste(a,b)

df <- data.frame(a,b,c,d,e)
hex_codes1 <- hue_pal()(length(unique(df$Code)))
cols <- hex_codes1[match(df$a,unique(df$a))]
names(cols) <- df$e

p <- ggplot(data=df, aes(x=d, y=c, colour=e)) + geom_line() + scale_colour_manual(values = cols) + 
  labs(colour="legend")

ggplotly(p)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.