ggplot w/ x 轴月-年(无日)和 "filter" 开始日期
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")
这是我的基本图,指定了我的 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")
我在文本块上得到的错误是这样的:
Error in as.Date.default(e) : do not know how to convert 'e' to class “Date”
有人知道为什么吗?提前致谢!
我想我发现了错误:
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,您将得到整数。如果使用日期变量会更好。
然后我尝试绘图但没有图形可用。为什么?因为您定义的最小日期是“2020-11-01”。你的限制是从 2020-11-01 到现在的 2021-02-23。该区间内不包含样本中的任何数据点。
所以假设我有数据集:
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")
这是我的基本图,指定了我的 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")
我在文本块上得到的错误是这样的:
Error in as.Date.default(e) : do not know how to convert 'e' to class “Date”
有人知道为什么吗?提前致谢!
我想我发现了错误:
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,您将得到整数。如果使用日期变量会更好。
然后我尝试绘图但没有图形可用。为什么?因为您定义的最小日期是“2020-11-01”。你的限制是从 2020-11-01 到现在的 2021-02-23。该区间内不包含样本中的任何数据点。