簡體   English   中英

ggplot w/x-axis Month-Year(無天)和“過濾器”開始日期

[英]ggplot w/ x-axis Month-Year (no day) and “filter” start date

所以假設我有數據集:

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

這是我的月度轉換: sales$month <- month(sales$date)

這是我在 x 軸上使用的最小值: min <- ymd("2020-11-01")

這是我的基礎 plot 指定我的 df: baseplot <- ggplot(sales, aes(month, order_sum))

我正在嘗試制作該月的日期與order_sum總和的基本圖表,並將 x 軸上的日期顯示為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")

我在文本塊上得到的錯誤是這樣的:

as.Date.default(e) 中的錯誤:不知道如何將“e”轉換為 class“日期”

有誰知道為什么? 提前致謝!

我想我發現了錯誤:

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

首先,如果你用 month() 轉換 sales$month,你會得到整數。 如果您使用日期變量會更好。

然后我嘗試了 plot 但沒有可用的圖形。 為什么? 因為您定義的最小日期是“2020-11-01”。 您的限制是從 2020-11-01 到現在 2021-02-23。 該間隔中不包含樣本中的任何數據點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM