将自定义颜色添加到ggplot [英] Add custom colors to ggplot
本文介绍了将自定义颜色添加到ggplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
手动
为每个类别
添加了一个额外的列: mtcars $ color [mtcars $ carb = 4] ='#F98866'
mtcars $ color [mtcars $ carb = 3] ='# 68829E'
mtcars $ color [mtcars $ carb = 2] ='#FF420E'
mtcars $ color [mtcars $ carb = 1] ='#89DA59'
p < - ggplot(mtcars)+
geom_point(aes(wt,mpg,
size = disp,
color = mtcars $ color))#+ scale_color_manual(values = mtcars $ color)
但是当我运行上面的代码时,我将它作为输出:
而不是我的指定的
颜色,我得到预设的 ggplot
颜色。 然而,如果我取消最后一行的注释,则部分即可获得我正在寻找的内容 - 只有一种我想要的颜色。
这个问题怎么解决?
解决方案
ggplot(mtcars)+
geom_point(aes(wt,mpg, size = disp,color = color))+
scale_color_identity(guide ='legend')
My main goal is to color elements differently in my plot. To do so, I manually
added an extra column with the desired color for each category
:
mtcars$color[mtcars$carb = 4] = '#F98866'
mtcars$color[mtcars$carb = 3] = '#68829E'
mtcars$color[mtcars$carb = 2] = '#FF420E'
mtcars$color[mtcars$carb = 1] = '#89DA59'
p <- ggplot(mtcars) +
geom_point(aes(wt, mpg,
size = disp,
color = mtcars$color))# + scale_color_manual(values = mtcars$color)
But when I run the above code, I get this as an output:
Instead of my specified
colors, I get the preset ggplot
colors.
But if I uncomment the last line, I partially get what I'm looking for--only one of the colors I wanted.
How can this problem be resolved?
解决方案
ggplot(mtcars) +
geom_point(aes(wt, mpg, size = disp, color = color)) +
scale_color_identity(guide = 'legend')
这篇关于将自定义颜色添加到ggplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文