Add gradient color within groups in ggplot2

Viewed 382

I need help in order to add colors to ggplot objects (specificaly geom_bar).

Here is my data

Names       Family          Groups    Values
H.sapiens   A               G1        2
H.erectus   A               G1        6 
H.erectus   B               G2        12
M.griseus   C               G2        3
A.mellifera D               G3        3
L.niger     D               G3        8
H.erectus   D               G3        2
L.niger     A               G1        3
L.niger     B               G2        3
A.mellifera A               G1        8

And so far I suceeded to create this plot :

enter image description here

with this code :

library(ggplot2)
library(ggstance)
library(ggthemes)
ggplot(table, aes(fill=Family, y=Names, x=Values)) + 
  geom_barh(stat="identity",colour="white")+ theme_minimal() +
  scale_x_continuous(limits = c(0,60), expand = c(0, 0))

and now I would like to change the color depending of Groups. More precisely I would like to choose a major color for each group, for instance: G1= blue ; G2 = Green ; G3= Red.

and for each Family to get a gradient within these colors. For instance, B will be darkblue and C ligthblue.

Does someone have an idea, please ?

Here are the data :

dput(table)
structure(list(Names = structure(c(3L, 2L, 2L, 5L, 1L, 4L, 2L, 
4L, 4L, 1L), .Label = c("A.mellifera", "H.erectus", "H.sapiens", 
"L.niger", "M.griseus"), class = "factor"), Family = structure(c(1L, 
1L, 2L, 3L, 4L, 4L, 4L, 1L, 2L, 1L), .Label = c("A", "B", "C", 
"D"), class = "factor"), Groups = structure(c(1L, 1L, 2L, 2L, 
3L, 3L, 3L, 1L, 2L, 1L), .Label = c("G1", "G2", "G3"), class = "factor"), 
    Values = c(2L, 6L, 12L, 3L, 3L, 8L, 2L, 3L, 3L, 8L)), class = "data.frame", row.names = c(NA, 
-10L))
2 Answers

You may perhaps tweak this one to suit your requirements (I have changed your sample data a bit to show you different gradient among same Group)

df <- read.table(header = T, text = "Names       Family          Groups    Values
H.sapiens   A               G1        2
H.erectus   B               G1        6 
H.erectus   B               G2        12
M.griseus   C               G2        3
A.mellifera D               G3        3
L.niger     D               G3        8
H.erectus   A               G3        2
L.niger     A               G1        3
L.niger     B               G2        3
A.mellifera C               G1        8")

library(tidyverse)
df %>% ggplot() +
  geom_col(aes(x = Names, y = Values, fill = Groups, alpha = as.integer(as.factor(Family)))) +
  coord_flip() +
  scale_fill_manual(name = "Groups", values = c("blue", "green", 'red')) +
  scale_alpha_continuous(name = "Family", range = c(0.2, 0.7)) +
  theme_classic()

Created on 2021-06-12 by the reprex package (v2.0.0)

We can create range of colours for each Group then match on order of Family. You might need to play around with colours to make the difference more prominent:

cols <- lapply(list(G1 = c("darkblue", "lightblue"),
                    G2 = c("darkgreen", "lightgreen"),
                    G3 = c("red4", "red")),
               function(i) colorRampPalette(i)(length(unique(table$Family))))

table$col <- mapply(function(g, i) cols[[ g ]][ i ], 
                    g = table$Groups, i = as.numeric(table$Family))

ggplot(table, aes(x = Values, y = Names, fill = col )) + 
  geom_barh(stat = "identity", colour = "white") +
  scale_x_continuous(limits = c(0, 60), expand = c(0, 0)) +
  scale_fill_identity() +
  theme_minimal()

enter image description here

Related