ggplot and color definition of different levels in figures

For my Scientific paper I have created 3 different figures with multiple plots. My outcome has 6 levels but these are not all present in all plots due to gender and age differences. I therefore need to define each group by a specific color consistent in all graphs across figures. Can you please help me with a good solution to this in ggplot? I have tried everything!! Many thanks!

Welcome to the forum.
We need some idea of what you have tried and some sample data. See:

A handy way to supply some sample data is the dput() function. In the case of a large dataset something like dput(head(mydata, 100)) should supply the data we need. Just do dput(mydata) where mydata is your data. Copy the output and paste it here.

I am not allowed to reproduce the codes for the ggplot… but it is in a ggplot statement and I figure there probably is a universal statement that I could use in the ggplot statement defining specific colors for the different levels of any 6-level outcome variable called for example ‘breast density_outcome’ with 6 levels : 1=Breast density a (red color), 2=Breast density b (blue color), 3=breast density c (green color), 4=breast density d (yellow color), 5= breast density e (Grey color), 6=breast density g (purple color).

I would like the colors to be the same for the specific outcome levels in all my ggplots. Currently for some plots I only have for example 5 levels due to the distribution of the outcome in data and then the colors for the different outcome levels are currently becoming mixed so that breast density e being Grey in Figure 1 becomes purple in Figure 2. Which is not a good thing for Scientific papers.

How is this done in ggplot ?

Grateful for your help.

Okay, I think this tutorial on using RcolorBrewer is a good place to start.

Change ggplot2 Color & Fill Using scale_brewer Functions & RColorBrewer Package in R

But by which statement would you specify a color to each level of the outcome variable in ggplot by use of this package?
Is it in the theme statement or some other. I think Inuse the scale_fill_jama()

I think I use the scale_fill_jama() …. Is it in this statement or should I add something specific. Thought would be something like 1=Grey, 2=purple, 3=green…..

What something like scale_fill_jama() does is provide a palate of colours and ggplot2 starts at the first colour and works through the "list'. If various palates do not have exactly what you want you can create your own using the scale_colour_manual function. Note there is an equivalent scale_fill_manual() function. The scale_fill_jama() palette is in the ggsi package so you will need to install it.
Scientific Journal and Sci-Fi Themed Color Palettes for ggplot2

Some examples


dat1 <- data.frame(aa = sample(LETTERS[1:5], 20, replace = TRUE), xx = rnorm(20), yy = rnorm(20))

p <-  ggplot(dat1, aes(xx, yy, colour = aa)) + geom_point() 

p1 <- p +  ggtitle("Default Colour Scheme")

p2 <- p + scale_color_jama()  +
    ggtitle("JAMA colour scheme")

my_colours <- c("green", "orange", "blue", "yellow", "black")

p3 <- p + scale_colour_manual(values = my_colours) +
  ggtitle("Handcrafted colours")

Many thanks,

I will try this - seems in the right direction. But how do I then define each level of density to stay the same color in all my plots so that a reader will always know that for example a green color in my plots is equivalent to for example breast density level 1, red color for example always breast density level 2, etc up to 6 levels?

I am very grateful for your kind help.

Many thanks again!

I believe ggplot2 will handle this automatically as long as the data set remains unchanged but I have not checked.

you can be explicit and have complete control

my_colours <- c( "D"="orange",

p + scale_colour_manual(values = my_colours) +
  ggtitle("Handcrafted colours")

Ah thanks.
I thought one could do so but stopped before I tried to work out how to do it. I don't think I'd have found such a nice solution. I was thinking we might need multiple multiple my_colours declarations.

Extremely many thanks to both of you for your kind solutions - will try to make these nice inputs work and hopefully my many hours of EXTREME frustration will come to an end!:sweat_smile:

This topic was automatically closed 21 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.