R ggplot:按月份和年份聚合多年数据,美学长度误差

R ggplot:按月份和年份聚合多年数据,美学长度误差,r,ggplot2,lubridate,R,Ggplot2,Lubridate,我已经逐月阅读了所有相关的aggregate()和lubridate问题,但仍然遇到了美学长度上的错误。很多对我来说不起作用,因为他们按月对数据进行分组,但数据框只包含一年的数据。我不需要每个一月的累计总数——我需要它是月份和年份特定的 我的样本数据:(df称为“销售”) 按月份和年份汇总: # aggregate by month (i used _moyr short for month year) sales$bymonth <- aggregate(cbind(order_sum)

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

我的样本数据:(df称为“销售”)

按月份和年份汇总:

# 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?
如果我使用
x=order\u date\u create
y=order\u sum
它正确地绘制了月份-年份轴,但每个条形图仍然是每日总和。 如果我使用
x=order\u moyr
y=bymonth
,我会得到以下错误:

错误:长度必须为1或与数据相同(48839):y

顺便说一句,如果有人知道如何在同一个
scale\u y\u continuous
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()

您的代码与
aggregate
一起将汇总数据集作为一个整体分配给原始数据“sales”中的一个新列
# 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()
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()