For the data below I would like to add colours so that ID
within p__Firmicutes
have a red scale, ID
within p__Bacteroidetes
have a blue scale, and ID
within p__Proteobacteria
have a green scale. How can this be done?
ggplot(df,aes(variable,value, fill=ID)) geom_bar(position = "fill", stat="identity")
df <- dput(head(Metaphlan.species.m.top5,10))
structure(list(ID = c("s__Streptococcus_lutetiensis", "s__Streptococcus_equinus",
"s__Ligilactobacillus_animalis", "s__Prevotella_copri_clade_A",
"s__Catenibacterium_sp_AM22_15", "s__GGB6572_SGB9285", "s__Blautia_producta",
"s__GGB2738_SGB3684", "s__Amedibacterium_intestinale", "s__GGB48468_SGB66515"
), Phyla = c("p__Firmicutes", "p__Firmicutes", "p__Firmicutes",
"p__Bacteroidetes", "p__Firmicutes", "p__Proteobacteria", "p__Firmicutes",
"p__Proteobacteria", "p__Firmicutes", "p__Bacteroidetes"), variable = structure(c(1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("Reads.DNA_H1H_8_H3.WHRDDOGroxMAMVAA.706511_profile20",
"Reads.DNA_H1H_8_F1.WHRDDOGroxMAIKAA.703505_profile20", "Reads.DNA_H1H_8_D1.WHRDDOGroxMAIJAA.702505_profile20",
"Reads.DNA_H1H_8_B5.WHRDDOGroxMAJBAA.710506_profile20", "Reads.DNA_H1H_7_D5.WHRDDOGroxMAIAAA.705503_profile20",
"Reads.DNA_H1H_7_A5.WHRDDOGroxMAHXAA.702503_profile20", "Reads.DNA_H1H_6_C3.WHRDDOGroxMAGCAA.703508_profile20",
"Reads.DNA_H1H_6_A5.WHRDDOGroxMAGLAA.715508_profile20", "Reads.DNA_H1H_3_D6.WHRDDOGroxMAKUAA.701506_profile20",
"Reads.DNA_H1H_3_B3.WHRDDOGroxMACOAA.707508_profile20", "Reads.DNA_H1H_3_A3.WHRDDOGroxMACNAA.706508_profile20",
"DNA_H1H_6_H4_profile20", "DNA.H1H.54_A7_profile20", "DNA.H1H.47_E1_profile20",
"DNA.H1H.47_D1_profile20", "DNA_H1H_37_E4_profile20", "DNA_H1H_35_H4_profile20",
"DNA_H1H_35_B5_profile20", "DNA_H1H_35_A5_profile20", "DNA_H1H_34_D4_profile20",
"DNA_H1H_34_C1_profile20", "DNA_H1H_33_B5_profile20", "DNA_H1H_30_F1_profile20",
"DNA_H1H_30_E1_profile20", "DNA_H1H_24_E2_profile20", "DNA_H1H_24_E1_profile20",
"DNA_H1H_24_C4_profile20", "DNA_H1H_24_B2_profile20", "DNA_H1H_15_C6_profile20"
), class = "factor"), value = c(24.51658, 12.33968, 10.00188,
5.16247, 4.56347, 34.40184, 20.38267, 18.77016, 9.94069, 4.14584
)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA,
-10L), groups = structure(list(variable = structure(1:2, .Label = c("Reads.DNA_H1H_8_H3.WHRDDOGroxMAMVAA.706511_profile20",
"Reads.DNA_H1H_8_F1.WHRDDOGroxMAIKAA.703505_profile20", "Reads.DNA_H1H_8_D1.WHRDDOGroxMAIJAA.702505_profile20",
"Reads.DNA_H1H_8_B5.WHRDDOGroxMAJBAA.710506_profile20", "Reads.DNA_H1H_7_D5.WHRDDOGroxMAIAAA.705503_profile20",
"Reads.DNA_H1H_7_A5.WHRDDOGroxMAHXAA.702503_profile20", "Reads.DNA_H1H_6_C3.WHRDDOGroxMAGCAA.703508_profile20",
"Reads.DNA_H1H_6_A5.WHRDDOGroxMAGLAA.715508_profile20", "Reads.DNA_H1H_3_D6.WHRDDOGroxMAKUAA.701506_profile20",
"Reads.DNA_H1H_3_B3.WHRDDOGroxMACOAA.707508_profile20", "Reads.DNA_H1H_3_A3.WHRDDOGroxMACNAA.706508_profile20",
"DNA_H1H_6_H4_profile20", "DNA.H1H.54_A7_profile20", "DNA.H1H.47_E1_profile20",
"DNA.H1H.47_D1_profile20", "DNA_H1H_37_E4_profile20", "DNA_H1H_35_H4_profile20",
"DNA_H1H_35_B5_profile20", "DNA_H1H_35_A5_profile20", "DNA_H1H_34_D4_profile20",
"DNA_H1H_34_C1_profile20", "DNA_H1H_33_B5_profile20", "DNA_H1H_30_F1_profile20",
"DNA_H1H_30_E1_profile20", "DNA_H1H_24_E2_profile20", "DNA_H1H_24_E1_profile20",
"DNA_H1H_24_C4_profile20", "DNA_H1H_24_B2_profile20", "DNA_H1H_15_C6_profile20"
), class = "factor"), .rows = structure(list(1:5, 6:10), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -2L), .drop = TRUE))
CodePudding user response:
One approach to achieve your desired result would be to first assign colors by Phyla
. In a second step group by Phyla
and use e.g. colorspace::lighten
to lighten the chosen colors to get a color scale for each Phyla
which requires some data wrangling.
library(ggplot2)
library(dplyr)
library(tibble)
library(colorspace)
pal <- c("red", "blue", "green")
names(pal) <- c("p__Firmicutes", "p__Bacteroidetes", "p__Proteobacteria")
pal <- tibble::enframe(pal, name = "Phyla", value = "fill")
pal <- df %>%
left_join(pal, by = "Phyla") %>%
arrange(Phyla) %>%
group_by(Phyla) %>%
mutate(id = seq(n()),
amount = (id - 1) / n()) %>%
ungroup() %>%
mutate(fill = colorspace::lighten(fill, amount)) %>%
select(ID, fill) %>%
tibble::deframe()
ggplot(df, aes(variable, value, fill = ID))
geom_bar(position = "fill", stat = "identity")
scale_fill_manual(values = pal)