【问题标题】:How to change colours of specific groups in ggerrorplot (ggpubr)?如何更改ggerrorplot(ggpubr)中特定组的颜色?
【发布时间】:2022-09-29 12:09:33
【问题描述】:

有人可以帮我更改此错误图中特定组的颜色。

我希望具有相似“栖息地类型”的群体具有相同的颜色。

IE。,

  • 全部海草(硬底)要成为的网站白色的.

  • 海草软底=灰色的

  • =黑色的

  • 孵化年雄性/雌性和孵化年雄性/雌性 2021 均来自占主导地位的网站,所以我希望它们成为黑色的也是。

这是我的代码:

#### 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\")

这是我尝试过的:

# 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)

谢谢您的帮助!!

  • 欢迎来到 SO!如果您提供 a minimal reproducible example 包括您的数据的 sn-p 或一些虚假数据,那么帮助您会更容易。在您的情况下,发布用于绘图的数据集 SUSC_TRIG_subset 的示例就足够了。
  • 但是,根据我使用ggpubr 的经验,我不确定使用ggerrorplot 是否可以实现您的结果。当涉及到定制时,使用ggplot2 从头开始​​构建绘图可能是值得的并且更容易的选择。

标签: r ggplot2 colors ggpubr


【解决方案1】:

我通过使用groupcolor 美学设置和geom_errorbar 来尝试使用ggplot2。可以使用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

手动覆盖一个独立的水平误差条geom_errorbarh 用于年份注释。

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(x = x, 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)
#> Warning: Ignoring unknown aesthetics: x

创建于 2022-09-29,reprex v2.0.2

【讨论】:

  • 嗨,瑞安,这非常有帮助,谢谢!我想知道如何在图表中添加标签?即,我想为每个组添加 2005、2021 和 2006 年的年份标签。以及 2021 年的站点名称/同类群组名称。
猜你喜欢
  • 2021-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-13
  • 2011-07-10
  • 2012-03-02
相关资源
最近更新 更多