【问题标题】:Cannot set colours as values in scale_colour_discrete [duplicate]无法将颜色设置为 scale_colour_discrete [重复] 中的值
【发布时间】:2021-05-12 21:32:36
【问题描述】:

我正在创建一个图,描绘每个个体(“患者”)的多个数据点(“区域”)。我正在尝试将表示区域的颜色设置为手动色盲友好调色板:

#data sample
patient <- c("pat1", "pat1", "pat1", "pat1", "pat1", "pat1", "pat2", "pat2", "pat2", "pat2", "pat2", "pat2") %>% as.factor()
region <- c("bg", "cb", "fr", "hc", "sn", "th", "bg", "cb", "fr", "hc", "sn", "th")
abundance <- c(0.00257, 0.00172, 0.0214, 0.00594, 0.00151, 0.00955, 0.00257, 0.00172, 0.0214, 0.00594, 0.00151, 0.00955)
errormax <- c(0.00569, 0.00435, 0.0378, 0.0131, 0.00507, 0.0194, 0.00569, 0.00435, 0.0378, 0.0131, 0.00507, 0.0194)
errormin <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
mydata <- tibble(patient, region, abundance, errormax, errormin)

#palette
cbbPalette <- c("#E69F00", "#56B4E9", "#009E73", "#0072B2", "#D55E00", "#CC79A7")

#plot
myplot <- ggplot(mydata, mapping = aes(x = patient, y = abundance, colour = region)) + 
  geom_point(position = position_dodge(width = 0.6), na.rm = TRUE) +
  geom_errorbar(aes(ymin = errormax, ymax = errormin), width = 0.2, position = position_dodge(width = 0.6)) +
  ylim(0,0.12)+
  xlab("Patient") + ylab("Abundance") +
  theme_bw() +
  scale_colour_discrete(name="Brain region",
                        labels = c("basal ganglia", "cerebellum", "frontal cortex", "hippocampus", "midbrain", "thalamus"), 
                        breaks = c("bg", "cb", "fr", "hc", "sn", "th"), 
                        values = cbbPalette)

但是上面的代码给了我这个错误:

Error in discrete_scale(aesthetics, "hue", hue_pal(h, c, l, h.start, direction),  : 
  unused argument (values = c("#E69F00", "#56B4E9", "#009E73", "#0072B2", "#D55E00", "#CC79A7"))

也许values 不是正确的论点?我应该改用其他东西吗?

【问题讨论】:

标签: r ggplot2 colors


【解决方案1】:

你可能需要scale_colour_manual

library(ggplot2)

ggplot(mydata, mapping = aes(x = patient, y = abundance, colour = region)) + 
  geom_point(position = position_dodge(width = 0.6), na.rm = TRUE) +
  geom_errorbar(aes(ymin = errormax, ymax = errormin), width = 0.2, 
               position = position_dodge(width = 0.6)) +
  ylim(0,0.12)+
  xlab("Patient") + ylab("Abundance") +
  theme_bw() +
  scale_colour_manual(name="Brain region",
                        labels = c("basal ganglia","cerebellum","frontal cortex",
                                   "hippocampus", "midbrain", "thalamus"), 
                        breaks = c("bg", "cb", "fr", "hc", "sn", "th"), 
                        values = cbbPalette)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-22
    • 2021-05-24
    • 2020-10-12
    • 2013-08-12
    • 2018-10-27
    • 2018-05-15
    • 2020-09-05
    • 2023-03-12
    相关资源
    最近更新 更多