How to change colours of specific groups in ggerrorplot (ggpubr)?

Viewed 28

Could someone please help me in changing colours of specific groups in this error plot.

enter image description here

I want groups with similar "habitat types" to be the same colour.

I.e.,

  • All Seagrass (hard-bottom) sites to be white.

  • Seagrass soft-bottom = grey

  • Mussel = black

  • After hatch year male/female & Hatch year male/female 2021 are all from MUSSEL dominated sites so I want them to be BLACK as well.

enter image description here

Here is my code:

#### METABOLITE DATA ####

TRIG_rawdata = read.csv("C:\\Users\\o_tru\\OneDrive\\ARP\\Metabolites\\SUSC metabolites_TrueTRIG_2005-2006_2021.csv")

## Create error plot ##

# Combine age and sex to create single ("COHORT") factor (HYF = Hatch Year Female, HYM = Hatch Year Male, AHYF = AFTER Hatch Year Female, AHYM = AFTER Hatch Year Male)
library(dplyr)
SUSC_TRIG = mutate(TRIG_rawdata, Cohort = case_when(Age == "HY" & Sex == "F"  ~ "HYF", Age == "HY" & Sex == "M"  ~ "HYM", Age == "AHY" & Sex == "F"  ~ "AHYF", 
                                                    Age == "AHY" & Sex == "M"  ~ "AHYM"))

# Combine Age, Habitat type , and Cohort

SUSC_TRIG_subset = mutate(SUSC_TRIG, Habitat_Year_Cohort = case_when(Year == "2005" & Habitat.Type == "Seagrass (hard-bottom)" & Cohort == "AHYM" ~ "Seagrass (hard-bottom) 2005", 
                                                                              Year == "2005" & Habitat.Type == "Seagrass (soft-bottom)" & Cohort == "AHYM" ~ "Seagrass (soft-bottom) 2005",
                                                                              Year == "2005" & Habitat.Type == "Mussel (mixed substrate)" & Cohort == "AHYM" ~ "Mussel (mixed substrate) 2005", 
                                                                              Year == "2006" & Habitat.Type == "Seagrass (hard-bottom)" & Cohort == "AHYM" ~ "Seagrass (hard-bottom) 2006",
                                                                              Year == "2006" & Habitat.Type == "Seagrass (soft-bottom)" & Cohort == "AHYM" ~ "Seagrass (soft-bottom) 2006", 
                                                                              Year == "2006" & Habitat.Type == "Mussel (mixed substrate)" & Cohort == "AHYM" ~ "Mussel (mixed substrate) 2006", 
                                                                              Year == "2021" & Habitat.Type == "Mussel (mixed substrate)" & Cohort == "AHYF" ~ "After Hatch Year Female 2021", 
                                                                              Year == "2021" & Habitat.Type == "Mussel (mixed substrate)" & Cohort == "AHYM" ~ "After Hatch Year Male 2021", 
                                                                              Year == "2021" & Habitat.Type == "Mussel (mixed substrate)" & Cohort == "HYF" ~ "Hatch Year Female 2021", 
                                                                              Year == "2021" & Habitat.Type == "Mussel (mixed substrate)" & Cohort == "HYM" ~ "Hatch Year Male 2021"))

# Create error plot using R function ggerrorplot()  [in ggpubr]

library(ggpubr)

SUSC_TRIG_subset$Month <-factor(SUSC_TRIG_subset$Month, levels=c("Dec", "Mar")) #reorder x-axis

SUSC_TRIG_subset$Habitat_Year_Cohort <- factor(SUSC_TRIG_subset$Habitat_Year_Cohort, levels=c("Seagrass (hard-bottom) 2005","Seagrass (soft-bottom) 2005", "Mussel (mixed substrate) 2005",
                                                                                              "After Hatch Year Male 2021", "After Hatch Year Female 2021", "Hatch Year Male 2021", "Hatch Year Female 2021", 
                                                                                              "Seagrass (hard-bottom) 2006","Seagrass (soft-bottom) 2006", "Mussel (mixed substrate) 2006"))

TRIG.plot <- ggerrorplot(SUSC_TRIG_subset, x = "Month", y = "True.TRIG", color = "Habitat_Year_Cohort", desc_stat = "mean_sd", error.plot = "errorbar", 
 add = "mean")

This is what I tried:

# Change colours of groups of error plot

group.colours <- c("Seagrass (hard-bottom) 2005" = "#FFFFFF", "Seagrass (soft-bottom) 2005" = "#999999", "Mussel (mixed substrate) 2005" = "#000000", 
                    "After Hatch Year Male 2021" = "#000000", "After Hatch Year Female 2021" = "#000000", "Hatch Year Male 2021" = "#000000", "Hatch Year Male 2021 = #000000", 
                     "Seagrass (hard-bottom) 2006" = "#FFFFFF", "Seagrass (soft-bottom) 2006" = "#999999", "Mussel (mixed substrate) 2006" = "#000000")

TRIG.plot <- ggerrorplot(SUSC_TRIG_subset, x = "Month", y = "True.TRIG", color = "Habitat_Year_Cohort", 
            desc_stat = "mean_sd", 
            error.plot = "errorbar", 
            add = "mean") + scale_fill_manual(values=group.colours)

Thank you for your help!!

0 Answers
Related