使用 ggplot 进行多年销售的同步 X 轴 [英] Synchronous X-Axis For Multiple Years of Sales with ggplot
问题描述
我有从 2012-01-01 到现在 (2015-11-20) 的 1417 天销售数据.即使使用 ggplot 的 color = as.factor(Year)
选项.
I have 1417 days of sale data from 2012-01-01 to present (2015-11-20). I can't figure out how to have a single-year (Jan 1 - Dec 31) axis and each year's sales on the same, one year-long window, even when using ggplot's color = as.factor(Year)
option.
总销售额为 int
head(df$Total.Sales)
[1] 495 699 911 846 824 949
我已经使用 lubridate
包将 Year 从原始 Day 变量中提取出来.
and I have used the lubridate
package to pull Year out of the original Day variable.
df$Day <- as.Date(as.numeric(df$Day), origin="1899-12-30")
df$Year <- year(df$Day)
但是因为Day包含了年份信息
But because Day contains the year information
sample(df$Day, 1)
[1] "2012-05-05"
ggplot 仍在绘制三年而不是将它们同步到同一时间段(一年,全年):
ggplot is still graphing three years instead of synchronizing them to the same period of time (one, full year):
g <- ggplot(df, aes(x = Day, y = Total.Sales, color = as.factor(Year))) +
geom_line()
推荐答案
我创建一些示例数据如下
I create some sample data as follows
set.seed(1234)
dates <- seq(as.Date("2012-01-01"), as.Date("2015-11-20"), by = "1 day")
values <- sample(1:6000, size = length(dates))
data <- data.frame(date = dates, value = values)
顺便说一句,提供类似的东西就是一个可重现的例子.
Providing something of the sort is, by the way, what is meant by a reproducible example.
然后我准备一些额外的列
Then I prepare some additional columns
library(lubridate)
data$year <- year(data$date)
data$day_of_year <- as.Date(paste("2012",
month(data$date),mday(data$date), sep = "-"))
最后一行几乎肯定是罗兰在评论中的意思.他选择闰年是正确的,因为它包含所有可能的日期.正常年份会错过 2 月 29 日.
The last line is almost certainly what Roland meant in his comment. And he was right to choose the leap year, because it contains all possible dates. A normal year would miss February 29th.
现在剧情是由
library(ggplot2)
library(scales)
g <- ggplot(data, aes(x = day_of_year, y = value, color = as.factor(year))) +
geom_line() + scale_x_date(labels = date_format("%m/%d"))
我调用 scale_x_date
来定义没有年份的 x 轴标签.这依赖于包 scales
中的函数 date_format
.字符串 "%m/%d"
定义日期格式.如果您想了解有关这些格式字符串的更多信息,请使用 ?strptime
.
I call scale_x_date
to define x-axis labels without the year. This relies on the function date_format
from the package scales
. The string "%m/%d"
defines the date format. If you want to know more about these format strings, use ?strptime
.
如图:
您可以立即看到这种表示可能存在的问题.在这个情节上很难区分任何东西.但这当然也与我的样本数据差异很大这一事实有关.您的数据可能看起来不同.否则,请考虑使用分面(参见 ?facet_grid
或 ?facet_wrap
).
You can see immediately what might be the trouble with this representation. It is hard to distinguish anything on this plot. But of course this is also related to the fact that my sample data is wildly varying. Your data might look different. Otherwise, consider using faceting (see ?facet_grid
or ?facet_wrap
).
这篇关于使用 ggplot 进行多年销售的同步 X 轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!