ggplot:按月-年聚合多年数据,审美长度误差

ggplot: aggregate multi-year data by Month-Year, aesthetic length error

我已经按月阅读了所有相关的 aggregate() 和我能找到的 lubridate 问题,但我仍然 运行 陷入审美长度的错误。很多对我不起作用,因为他们按月对数据进行分组,但数据框只包含一年的数据。我不需要跨时间的每个一月的累计总数——我需要它是特定于月份和年份的。

我的示例数据:(df 称为“销售”)

order_date_create     order_sum
2020-05-19            900
2020-08-29            500
2020-08-30            900
2021-02-01            200
2021-02-06            500

按月-年汇总:

# aggregate by month (i used _moyr short for month year)
sales$bymonth <- aggregate(cbind(order_sum)~month(order_date_create),
                     data=sales,FUN=sum)
sales$order_moyr <- format(sales$order_date_create, '%m-%Y') # why does this get saved under values instead of data?

这是我的 ggplot:

# plot
ggplot(sales, aes(order_moyr, order_sum)) + 
  scale_x_date(limits = c(min, as.Date(now())), 
               breaks = "1 month", 
               labels = date_format("%m-%Y")) + 
  scale_y_continuous(labels = function(x) format(x, big.mark = "'", decimal.mark = ".", scientific = FALSE)) + 
  labs(x = "Date", y = "Sales Volume", title = "Sales by Month") +
  geom_bar(stat="identity")+ theme_economist(base_size = 10, base_family = "sans", horizontal = TRUE, dkpanel = FALSE) +  scale_colour_economist()

如果我使用 x = order_date_createy = order_sum 它会正确绘制,带有月-年轴,但 每个柱仍然是每日总和 。 如果我使用 x = order_moyry = bymonth,我会得到这个错误:

Error: Aesthetics must be either length 1 or the same as the data (48839): y

切线地,如果有人知道如何在同一个 scale_y_continous fcn 中同时使用 scale::dollar 和格式化千位分隔符,那将是一个很大的帮助。我还没有找到如何做到这两点。

library(scales); library(lubridate); library(dplyr); 
library(ggthemes)
sales %>%
  count(order_moyr = floor_date(order_date_create, "month"), 
        wt = order_sum, name = "order_sum") %>%
                     
ggplot(aes(order_moyr, order_sum)) + 
  scale_x_date(breaks = "1 month",
               labels = date_format("%m-%Y")) +
  scale_y_continuous(labels = scales::dollar_format(big.mark = "'", 
                                             decimal.mark = ".")) + 
  labs(x = "Date", y = "Sales Volume", title = "Sales by Month") +
  geom_bar(stat="identity", width = 25)+ 
  theme_economist(base_size = 10, base_family = "sans", 
                  horizontal = TRUE, dkpanel = FALSE) +  
  scale_colour_economist()