1

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!

danisabbia
  • 25
  • 5

1 Answers1

1

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.

Manu
  • 1,070
  • 10
  • 27
  • 1
    thank you!!! that solved the error. as for the sample data – i actually do have data after the specified min, i just didn't realise i hadn't included the right sample data for the purpose of the question here. do you know how i might resolve the error i'm getting now? `Error: stat_count() can only have an x or y aesthetic.` thanks for helping a beginner! – danisabbia Feb 23 '21 at 22:14
  • 1
    Hello @danisabbia, stat_count could be an error of the type of graphic you want. I think you can create a new question (with more data points as in my answer) and show us what `geom` you are using so I (or other guys in SO) can help you. And don't worry about being a beginner... I'm still a beginner too. – Manu Feb 24 '21 at 01:52