如何在geom_bar中更改AE的颜色填充

发布于 2025-01-26 08:09:47 字数 489 浏览 2 评论 0原文

我有以下代码:

  ggplot(aes(x = Education)) + 
  geom_bar(aes(fill = P_3_1, P_3_2)) +
  facet_wrap(~Education) +
  labs(y = 'Qunatity', title = 'Answers for group "P_3" 
       based on education') +
  theme_bw() 

我想以下面的方式获得GEOM_BAR,并具有两种颜色。 是可能的还是代码的这一部分:fill = p_3_1,p_3_2 需要更改吗? 我只有一个彩色的灰色条。

I have below code:

  ggplot(aes(x = Education)) + 
  geom_bar(aes(fill = P_3_1, P_3_2)) +
  facet_wrap(~Education) +
  labs(y = 'Qunatity', title = 'Answers for group "P_3" 
       based on education') +
  theme_bw() 

I want to get geom_bar as below with two colours.
Is that possible or this part of code: fill = P_3_1, P_3_2
needs to be changed?
I'm getting just one coloured gray bars.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

想你只要分分秒秒 2025-02-02 08:09:48

如果您可以提供这样的小示例,那就更好了。您的填充可能是下面的单列?

library(tidyverse)

tribble(
  ~Education, ~quantity, ~fill,
  "a", 1000, "P_3_1",
  "a", 2000, "P_3_2",
  "b", 3000, "P_3_1", 
  "b", 4000, "P_3_2"
) |> 
  ggplot(aes(Education, quantity, fill = fill)) + 
  geom_col() +
  labs(y = 'Qunatity', title = 'Answers for group "P_3" based on education') +
  theme_bw() 

“”

在2022-05-05上由 reprex软件包(v2.0.1)

It's better if you can provide a small reproducible example like this. Your fill probably needs to be a single column per below?

library(tidyverse)

tribble(
  ~Education, ~quantity, ~fill,
  "a", 1000, "P_3_1",
  "a", 2000, "P_3_2",
  "b", 3000, "P_3_1", 
  "b", 4000, "P_3_2"
) |> 
  ggplot(aes(Education, quantity, fill = fill)) + 
  geom_col() +
  labs(y = 'Qunatity', title = 'Answers for group "P_3" based on education') +
  theme_bw() 

Created on 2022-05-05 by the reprex package (v2.0.1)

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