并排栏图的解决方案错误:只能具有X或Y美学

发布于 2025-01-27 01:28:50 字数 1228 浏览 3 评论 0原文

我在用包含字符(ride_month)的列的并排条形图时遇到了困难,该列包含骑手两种类型的总数数据(ride_duration)(ride_duration)(构件_casual)。

ggplot(data=bike_data_v4)+
+   geom_bar(mapping = aes(x=ride_month,fill=member_casual))+
+   geom_col(position = "dodge")

Error in `check_required_aesthetics()`:
! geom_col requires the following missing aesthetics: x and y
Run `rlang::last_error()` to see where the error occurred.

我的数据集最初看起来像这样:

为了解决我的问题,我做了一个新的数据集分组成员_casual和ride_month。接下来,我给了一笔骑行的总和。

bike_data_removedcols_V2 <- bike_data_removedcols %>% 
  group_by(member_casual, ride_month) %>%
  summarise(ride_duration_sum=sum(ride_duration))

我采用了新创建的数据集并将其应用于此GGPLOT函数:

ggplot(data = bike_data_removedcols_V2, aes(ride_month, ride_duration_sum, fill=member_casual, group = member_casual))+
      geom_col(position = position_dodge())

成功!

I was having trouble making a side-by-side bar graph with a column containing characters (ride_month), a column containing the total numeric data (ride_duration) for rider two types(member_casual).

ggplot(data=bike_data_v4)+
+   geom_bar(mapping = aes(x=ride_month,fill=member_casual))+
+   geom_col(position = "dodge")

Error in `check_required_aesthetics()`:
! geom_col requires the following missing aesthetics: x and y
Run `rlang::last_error()` to see where the error occurred.

my dataset originally looked like this:
enter image description here

To fix my problem, I made a new dataset grouping member_casual and ride_month. Next, I piped a sum of ride_duration.

bike_data_removedcols_V2 <- bike_data_removedcols %>% 
  group_by(member_casual, ride_month) %>%
  summarise(ride_duration_sum=sum(ride_duration))

I took the newly created dataset and applied it to this ggplot function:

ggplot(data = bike_data_removedcols_V2, aes(ride_month, ride_duration_sum, fill=member_casual, group = member_casual))+
      geom_col(position = position_dodge())

Success!
enter image description here

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

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

发布评论

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

评论(2

少年亿悲伤 2025-02-03 01:28:50

如果要具有分组的条形图,请添加group这样的美学:

  ggplot(data = mtcars, aes(ride_month, ride_duration, fill=member_casual, group = member_casual))+
    geom_col(position = position_dodge()) 

以下是mtcars dataset的示例:

  ggplot(data = mtcars, aes(cyl, mpg, fill=am, group = am))+
    geom_col(position = position_dodge())

If you want to have a grouped bar chart then add group aesthetics like this:

  ggplot(data = mtcars, aes(ride_month, ride_duration, fill=member_casual, group = member_casual))+
    geom_col(position = position_dodge()) 

Here is an example with mtcars dataset:

  ggplot(data = mtcars, aes(cyl, mpg, fill=am, group = am))+
    geom_col(position = position_dodge())

enter image description here

夜唯美灬不弃 2025-02-03 01:28:50

这会做你想要的吗?

library(ggplot2)
data = mtcars

ggplot(data, aes(x = factor(carb), y = mpg, fill = factor(vs))) +
  geom_col(position = "dodge")

Does this do what you want?

library(ggplot2)
data = mtcars

ggplot(data, aes(x = factor(carb), y = mpg, fill = factor(vs))) +
  geom_col(position = "dodge")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文