在 ggplot 中使用预定义的调色板
有谁知道如何在 ggplot 中使用预定义的调色板?
我有一个我想使用的颜色向量:
rhg_cols <- c("#771C19", "#AA3929", "#E25033", "#F27314", "#F8A31B",
"#E2C59F", "#B6C5CC", "#8E9CA3", "#556670", "#000000")
但是当我尝试将它传递给什么都没有发生时
ggplot(mydata, aes(factor(phone_partner_products)), color = rhg_cols) +
geom_bar()
Does anyone know how to use a pre-defined color palette in ggplot?
I have a vector of colors I would like to use:
rhg_cols <- c("#771C19", "#AA3929", "#E25033", "#F27314", "#F8A31B",
"#E2C59F", "#B6C5CC", "#8E9CA3", "#556670", "#000000")
But when I try to pass it to nothing happened
ggplot(mydata, aes(factor(phone_partner_products)), color = rhg_cols) +
geom_bar()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须将
colour = rhg_cols
放入aes()
中。据我所知,您想将梯度应用于横坐标上带有因子变量的条形图(在条形图中)?然后使用fill
- 尝试这样做:或者尝试通过以下方式实现近似复制:
请注意,在第二种情况下,连续变量被传递给
fill
美学,因此scale_fill_gradient< /code> 之后被传递。如果将
factor
传递给fill
aes,则必须坚持使用scale_fill_manual(values = rhg_cols)
。You must put
colour = rhg_cols
insideaes()
. As far as I can tell, you want to apply gradient to bars (in barplot) with factor variable on the abscissa? Then usefill
- try this instead:or try to achieve approximate replica with:
Notice that in second case a continuous variable is passed to
fill
aesthetics, thereforescale_fill_gradient
is passed afterwards. If you pass afactor
to thefill
aes, you must stick withscale_fill_manual(values = rhg_cols)
.如果颜色是调色板,请使用
scale_colour_manual
:If the colours are a palette, use
scale_colour_manual
:首先将颜色添加到数据集:
然后将颜色映射到该列并使用
scale_colour_identity
First add, the colours to your data set:
Then map colour to that column and use
scale_colour_identity
由于您想要的颜色是颜色美学中的值,因此您真正想要的是
identity
比例,在本例中为scale_fill_identity
。由于您没有提供数据,我将使用您的颜色数据使用一个稍微不同的示例:
注意:为了清楚起见,我在示例中省略了 + opts(axis.text.x=theme_text(angle=90)) 。
Since the colors you want ARE the values in the color aesthetic, what you really want is the
identity
scale, in this casescale_fill_identity
.Since you didn't supply data, I'm going to use a slightly different example using your color data:
note: I omitted
+ opts(axis.text.x=theme_text(angle=90))
for clarity in the example.