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

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

问题描述

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



我有一个我想要使用的颜色矢量:

$ (#771C19,#AA3929,#E25033,#F27314,#F8A31B,#F8A31B,
#E2C59F,#B6C5CC,#8E9CA3,#556670,#000000)

但是,当我试图将它传递给没有发生任何事情时

  ggplot(mydata,aes(factor phone_partner_products)),color = rhg_cols)+ 
geom_bar()


解决方案您必须在 aes()内放置 color = rhg_cols 。据我所知,你想在横坐标上应用因子变量的梯度(barplot)?然后使用 fill - 试试这个:

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

或尝试实现近似副本:

  ggplot(mydata,aes(因子(phone_partner_products),fill = phone_partner_products)))+ 
geom_bar()+
scale_fill_gradient(low =#771C19,hi =#000000)

请注意,在第二种情况下,连续变量传递给 fill 美学,因此<$ c之后传递$ c> scale_fill_gradient 。如果您将因子传递给 fill aes,您必须坚持使用 scale_fill_manual(values = rhg_cols)


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

解决方案

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", hi = "#000000")

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

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

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