【发布时间】: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从头开始构建绘图可能是值得的并且更容易的选择。