1
votes

so suppose i have data set:

date         order_sum
2015-01-01   800
2016-08-19   900
2019-04-16   1200
2020-10-28   850

here's my to-month conversion: sales$month <- month(sales$date)

here's my minimum to be used in x-axis: min <- ymd("2020-11-01")

here's my base plot specifying my df: baseplot <- ggplot(sales, aes(month, order_sum))

i'm trying to make a basic graph of date vs sum of order_sum for that month and display date in x-axis as 11-2020.

scale_x_date(limits = c(min, now), breaks = "1 month", labels = date_format("%m-%Y")) +
scale_y_continuous(labels = function(x) scales::dollar(x, suffix = 'M'), n.breaks = 3, expand = expansion(mult = c(0, 0.05))) +
labs(x = "Date", y = "Order Volume")

the error i get back on the text block is this:

Error in as.Date.default(e) : do not know how to convert 'e' to class “Date”

anyone know why? thanks in advance!

1

1 Answers

1
votes

I think I have found the error:

library(scales)
library(lubridate)
sales <- data.frame(date = c("2015-01-01","2016-08-19",
                               "2019-04-16","2020-10-28"),
                      order_sum = c(800,900,1200,850))

sales$month <- month(as.Date(sales$date))
sales$date <- as.Date(sales$date)

min <- ymd("2020-11-01")
ggplot(sales, aes(date, 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) scales::dollar(x, suffix = 'M'), 
                     n.breaks = 3, 
                     expand = expansion(mult = c(0, 0.05))) + 
  labs(x = "Date", y = "Order Volume") +
  geom_point()

First, if you transform sales$month with month(), you'll get integers. Better if you use the date variable.

Then I tried to plot but no graphic was available. Why? Because the min date you defined is "2020-11-01". And your limits are from 2020-11-01 to now 2021-02-23. No data point in your sample is contained in that interval.