how to assign specific colors to some categories in 4 figures

I have 4 plots, 2 UMAP plots, and 2 Violin Plots, I would like to add color to them based on a variable named "celltype",
I would like to add a specific color to each of celltypes, for example, "CM1" would be "yellow", "CM2" would be "red" and so on.
I used this code:
</>
scale_color_manual(values = c
("CM1"= "dodgerblue2",
"CM2"="green4",
"CM3"="black", "CM4"="gold1",
"CM6"= "palegreen2",
"EC1"="gray70", "EC2"="khaki2",
"EC3"="maroon","ECFB"= "orchid1", "FB"="deeppink1",
"Leuko1"="blue1", "Leuko2"="steelblue4",
"NLC"= "darkturquoise", "PC"="green1", "SMC"="yellow3",
"Unk1"= "darkorange4", "Unk2"="brown")
)
</>
it worked, but how can I write it generaly, because here I know the number of celltypes and the name of them, also, I can assign specific colors, my question is: how can I write the above code generally, I have a vector of celltype named r_celltype and a vector of color named r_color.
Many thanks

lets say you have a goal of arriving at the following vector, that you intend to use by passing as the values argument of scale_color_manual

goal <- c("CM1"= "dodgerblue2",
          "CM2"="green4",
          "CM3"="black", "CM4"="gold1",
          "CM6"= "palegreen2",
          "EC1"="gray70", "EC2"="khaki2",
          "EC3"="maroon","ECFB"= "orchid1", "FB"="deeppink1",
          "Leuko1"="blue1", "Leuko2"="steelblue4",
          "NLC"= "darkturquoise", "PC"="green1", "SMC"="yellow3",
          "Unk1"= "darkorange4", "Unk2"="brown")
)

and you are starting with vectors containing the information (r_celltype,r_color)


(name_vec <- c("CM1", "CM2", "CM3", "CM4", "CM6", "EC1", "EC2", "EC3", "ECFB", 
              "FB", "Leuko1", "Leuko2", "NLC", "PC", "SMC", "Unk1", "Unk2"))

(color_vec <- c("dodgerblue2", "green4", "black", "gold1", "palegreen2", "gray70", 
               "khaki2", "maroon", "orchid1", "deeppink1", "blue1", "steelblue4", 
               "darkturquoise", "green1", "yellow3", "darkorange4", "brown"))

You can make your goal named vector from these two vectors via the following approach


solution <-color_vec
names(solution)<- name_vec

identical(solution,goal)

the last line verifies that we produces solution and it matches our goal.

1 Like

Thank you so much for your answer

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.