在 ggplot 中使用预定义的调色板

发布于 2024-09-06 00:40:20 字数 363 浏览 3 评论 0原文

有谁知道如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

早茶月光 2024-09-13 00:40:20

您必须将 colour = rhg_cols 放入 aes() 中。据我所知,您想将梯度应用于横坐标上带有因子变量的条形图(在条形图中)?然后使用 fill - 尝试这样做:

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

或者尝试通过以下方式实现近似复制:

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

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

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

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

溺ぐ爱和你が 2024-09-13 00:40:20

如果颜色是调色板,请使用 scale_colour_manual

ggplot(mydata, aes(factor(phone_partner_products), colour = colour_variable)) +
  scale_colour_manual(values = rhg_cols)

If the colours are a palette, use scale_colour_manual:

ggplot(mydata, aes(factor(phone_partner_products), colour = colour_variable)) +
  scale_colour_manual(values = rhg_cols)
泪之魂 2024-09-13 00:40:20

首先将颜色添加到数据集:

mydata$col <- rhg_cols

然后将颜色映射到该列并使用 scale_colour_identity

ggplot(mydata, aes(factor(phone_partner_products, colour = col))) + 
  geom_bar() + 
  scale_colour_identity()

First add, the colours to your data set:

mydata$col <- rhg_cols

Then map colour to that column and use scale_colour_identity

ggplot(mydata, aes(factor(phone_partner_products, colour = col))) + 
  geom_bar() + 
  scale_colour_identity()
妄司 2024-09-13 00:40:20

由于您想要的颜色是颜色美学中的值,因此您真正想要的是 identity 比例,在本例中为 scale_fill_identity

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

由于您没有提供数据,我将使用您的颜色数据使用一个稍微不同的示例:

 rhg_cols <- c("#771C19","#AA3929","#E25033","#F27314","#F8A31B",
               "#E2C59F","#B6C5CC","#8E9CA3","#556670","#000000")
 mydata <- sample(rhg_cols, 100, replace = TRUE)
 qplot(mydata, fill = mydata) +
   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 case scale_fill_identity.

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

Since you didn't supply data, I'm going to use a slightly different example using your color data:

 rhg_cols <- c("#771C19","#AA3929","#E25033","#F27314","#F8A31B",
               "#E2C59F","#B6C5CC","#8E9CA3","#556670","#000000")
 mydata <- sample(rhg_cols, 100, replace = TRUE)
 qplot(mydata, fill = mydata) +
   scale_fill_identity()

enter image description here


note: I omitted + opts(axis.text.x=theme_text(angle=90)) for clarity in the example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文