首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法在scale_colour_discrete中将颜色设置为值

无法在scale_colour_discrete中将颜色设置为值
EN

Stack Overflow用户
提问于 2021-02-09 09:02:22
回答 1查看 447关注 0票数 2

我正在创建一个图,描述每个人(“病人”)的几个数据点(“区域”)。我正在尝试将表示这些区域的颜色设置为手动的色盲友好调色板:

代码语言:javascript
运行
复制
#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)

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

代码语言:javascript
运行
复制
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不是正确的论点?我应该用不同的东西代替吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-09 09:06:58

你可能需要scale_colour_manual

代码语言:javascript
运行
复制
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)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66115833

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档