1

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!!

Phil
  • 7,287
  • 3
  • 36
  • 66
Olivia
  • 23
  • 3
  • Welcome to SO! It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. In your case it would be sufficient to post an example of your dataset `SUSC_TRIG_subset` used for the plot. – stefan Sep 24 '22 at 06:28
  • However, from my experience with `ggpubr` I am not sure whether your result could be achieved using `ggerrorplot`. When it comes to customizing it may be worthwhile and the easier option to built the plot from scratch using `ggplot2`. – stefan Sep 24 '22 at 06:31

1 Answers1

0

I gave it a try using ggplot2 by using both group and color aesthetic setting and geom_errorbar. Text annotation could be added using geom_text.

library(dplyr)
library(ggplot2)

# dummy data
SUSC_TRIG_subset = data.frame(
  Habitat_Year_Cohort = 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"),
  label = c("Birch Bay", "Padilla Bay", "Penn Cove", "Adult Male", "Adult Female", "Juvenile Male", "Juvenile Female", "Birch Bay", "Padilla Bay", "Penn Cove"),
  Month = c("Dec", "Dec", "Dec", "Dec", "Dec", "Dec", "Dec", "Mar", "Mar", "Mar"),
  True.TRIG = rnorm(30, sd = 0.5) + 1
) %>%
  mutate(
    Habitat_Year_Cohort = factor(Habitat_Year_Cohort, levels = unique(Habitat_Year_Cohort))
  )

# ggplot2
g <- SUSC_TRIG_subset %>%
  group_by(
    Habitat_Year_Cohort
  ) %>%
  summarise( # for Mean-SD error bar
    ymin = mean(True.TRIG) - sd(True.TRIG),
    ymax = mean(True.TRIG) + sd(True.TRIG),
    TRIG = mean(True.TRIG),
    Month = first(Month),
    label = first(label)
  ) %>%
  mutate( # create group for color
    group = case_when(
      grepl("Seagrass \\(hard-bottom\\)", Habitat_Year_Cohort) ~ "Seagrass (hard-bottom)",
      grepl("Seagrass \\(soft-bottom\\)", Habitat_Year_Cohort) ~ "Seagrass (soft-bottom)",
      TRUE ~ "Mussel"
    )) %>%
  ggplot(aes(Month, TRIG, 
             group = Habitat_Year_Cohort, # group for dodge position
             color = group)) +
  geom_errorbar(aes(ymin = ymin, ymax = ymax), 
                position = position_dodge(0.8), 
                width = 0.2) +
  geom_point(position = position_dodge(0.8), 
             size = 4) + # size of point
  geom_text(aes(label = label), color = "black", angle = 90, 
            position = position_dodge(0.8), vjust = 1.2, size = 3, hjust = -0.2) +
  scale_color_manual(values = c("Seagrass (hard-bottom)" = "#FFFFFF", 
                                "Seagrass (soft-bottom)" = "#999999", 
                                "Mussel" = "#000000")) +
  theme(legend.title=element_blank(), # remove legend title
        legend.position="top") +
  labs(x = NULL) # remove x label

g

Mannually overlaying an independent horizontal error bar geom_errorbarh for year annotation.

df_year = data.frame(
  y = c(0.1, 0.1, 0.1),
  x = c(1 - 0.22, 1 + 0.16, 2),
  Year = c("2005", "2021", "2006")
) %>%
  cbind(
    xmin = .$x - c(0.45, 0.6, 1) * 0.3,
    xmax = .$x + c(0.45, 0.65, 1) * 0.3
  )

g +
  geom_errorbarh(data = df_year, aes(y = y, xmin = xmin, xmax = xmax), 
                 inherit.aes = F, height = 0.1) +
  geom_text(data = df_year, aes(x = x, y = y, label = Year), 
            vjust = 1.3, size = 3, inherit.aes = F)

Created on 2022-09-29 with reprex v2.0.2

Ryan SY Kwan
  • 476
  • 3
  • 9
  • Hi Ryan, this is SUPER helpful, thank you! I was wondering, how might I add labels to the graph? i.e.., I want to add year labels for 2005, 2021, and 2006 for each group. As well as site names/ cohort names for 2021. – Olivia Sep 28 '22 at 16:31
  • Hi Olivia We may use `geom_text` for the labels and `geom_errorbarh` for the horizontal error bar. I have updated the answer accordingly. – Ryan SY Kwan Sep 29 '22 at 04:12
  • Hi Ryan, When I try to fit the plot to my data I am having some trouble. This is what I tried: g <- SUSC_TRIG_subset %>% group_by( Habitat_Year_Cohort ) %>% summarise( # for Mean-SD error bar ymin = mean(True.TRIG) - sd(True.TRIG), ymax = mean(True.TRIG) + sd(True.TRIG), TRIG = mean(True.TRIG), Month = first(Month), label = first(c("Birch Bay", "Padilla Bay", "Penn Cove", "Adult Male", "Adult Female", "Juvenile Male", "Juvenile Female", "Birch Bay", "Padilla Bay", "Penn Cove") )) %>% but all the labels come out as "Birch Bay" any advice? – Olivia Oct 05 '22 at 02:01
  • `first(c("Birch Bay", "Padilla Bay", "Penn Cove", "Adult Male", "Adult Female", "Juvenile Male", "Juvenile Female", "Birch Bay", "Padilla Bay", "Penn Cove") )` this would output `# Birch Bay`. You may want to try without `first`. – Ryan SY Kwan Oct 05 '22 at 10:31
  • Hi Olivia, if you think the answer is helpful to other people, it would be nice to mark it as "accepted" by toggling the checkmark next to the answer. – Ryan SY Kwan Oct 05 '22 at 10:36