R ggplot2 geom_line指定矢量的颜色会返回未知颜色

时间:2020-04-10 18:27:59

标签: r ggplot2 colors

我有这段代码

colors = c("score" = "#26648E")
plot = ggplot() + geom_line(data = DT, aes(x = position, y = score), stat = "summary_bin", binwidth = 1000, color = "score", group = 1) + scale_color_manual(name ="", values = colors) + facet_wrap(~chromosome, scales = "free_x")
ttle = paste0("referee score") 
plot = plot + labs(
  title = ttle) #+ theme(plot.title = element_markdown(lineheight = 1.5, size = 12), legend.text = element_markdown(size = 14))
p = plot +xlab( "position") + ylab("")
p

返回

错误:颜色名称未知:得分

这不是我第一次在向量中预先定义颜色,并且它一直在起作用。我不明白为什么在这种特定情况下它不起作用。我知道我可以直接在geom_line中定义颜色,我只是想了解为什么代码不起作用。例如,我有这段代码

colors = c("heterozygosity" = "#8b0000", "coverage" = "#00919c")
ggplot() + 
  geom_line(data = CT, aes(x = end, y = 3*(normalize(heterozygosity)), color = "heterozygosity")) + 
  geom_line(data = COV, aes(x = end, y = 2*(normalize(coverage)), color = "coverage")) + 
  scale_color_manual(name ="", values = colors) + 
  facet_wrap(~CHROM, scales = "free_x") 

不会产生任何错误,并提供所需的输出。

感谢您的见识

2 个答案:

答案 0 :(得分:1)

颜色属于美学范畴,请在分配y轴后尝试将其放置。如下所示。应该可以

颜色= c(“得分” =“#26648E”) plot = ggplot()+ geom_line(data = DT,aes(x = position,y = score,color =“ score”)stat =“ summary_bin”,binwidth = 1000,group = 1)+ scale_color_manual(name =“”,值=颜色)+ facet_wrap(〜染色体,比例=“ free_x”) ttle = paste0(“裁判得分”) 情节=情节+实验室( title = ttle)#+ theme(plot.title = element_markdown(lineheight = 1.5,size = 12),legend.text = element_markdown(size = 14)) p =图+ xlab(“ position”)+ ylab(“”) p

答案 1 :(得分:0)

谢谢大家

确实很尴尬,但是您当然是对的,这只是范围问题。