在 ggplot 中使用预定义的调色板 [英] Using a pre-defined color palette in ggplot

查看:39
本文介绍了在 ggplot 中使用预定义的调色板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何在 ggplot 中使用预定义的调色板?

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()

推荐答案

你必须把 colour = rhg_cols 放在 aes() 里面.据我所知,您想对横坐标上具有因子变量的条形图(在条形图中)应用渐变?然后使用 fill - 试试这个:

You must put colour = rhg_cols inside aes(). As far as I can tell, you want to apply gradient to bars (in barplot) with factor variable on the abscissa? Then use fill - try this instead:

ggplot(mydata, aes(factor(phone_partner_products), fill = factor(phone_partner_products))) +
  geom_bar() + 
  scale_fill_manual(values = rhg_cols)

或尝试使用以下方法实现近似复制:

or try to achieve approximate replica with:

ggplot(mydata, aes(factor(phone_partner_products), fill = phone_partner_products))) +
  geom_bar() + 
  scale_fill_gradient(low = "#771C19", high = "#000000")

请注意,在第二种情况下,一个连续变量被传递给 fill 美学,因此 scale_fill_gradient 是在之后传递的.如果您将 factor 传递给 fill aes,则必须坚持使用 scale_fill_manual(values = rhg_cols).

Notice that in second case a continuous variable is passed to fill aesthetics, therefore scale_fill_gradient is passed afterwards. If you pass a factor to the fill aes, you must stick with scale_fill_manual(values = rhg_cols).

这篇关于在 ggplot 中使用预定义的调色板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆