简体   繁体   English

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

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

i've read every relevant aggregate() by month and lubridate question i could find but am still running into an error of aesthetic length.我已经按月阅读了所有相关的aggregate()和我能找到的lubridate问题,但我仍然遇到了审美长度的错误。 lots didn't work for me bc they grouped data by month but the dataframe only contained data from one year.很多对我不起作用,因为他们按月对数据进行分组,但 dataframe 仅包含一年的数据。 i don't need the cumulative total of every January across time – i need it to be month- AND year-specific.我不需要跨时间每年一月的累计总数——我需要它是特定于月份和年份的。

my sample data: (df is called "sales")我的示例数据:(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

aggregating by month-year:按月汇总:

# 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?

here's my ggplot:这是我的 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()

if i use x = order_date_create and y = order_sum it plots correctly, with month-year axis, but each bar is still daily sum .如果我使用x = order_date_createy = order_sum它会正确绘制,带有月年轴,但每个条仍然是每日总和 if i use x = order_moyr and y = bymonth , i get this error:如果我使用x = order_moyry = bymonth ,我会收到此错误:

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

tangentially, if anyone knows how to use both scale::dollar AND format the thousands separator in the same scale_y_continous fcn it would be a great help.切线地,如果有人知道如何在同一个scale_y_continous fcn 中同时使用 scale::dollar 和格式化千位分隔符,那将是一个很大的帮助。 i've not found how to do both.我还没有找到如何做到这两点。

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

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM