ggplot同步销售多个年的X轴 [英] Synchronous X-Axis For Multiple Years of Sales with ggplot

查看:51
本文介绍了ggplot同步销售多个年的X轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从2012-01-01到现在(2015-11-20),我有1417天的销售数据.即使使用ggplot的color = as.factor(Year)选项,我也无法弄清楚如何在同一年(一年)的窗口中拥有一个一年(1月1日至12月31日)轴,以及每年的销售额.

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.

然后我准备一些其他列

library(lubridate)
data$year <- year(data$date)
data$day_of_year <- as.Date(paste("2012",
                    month(data$date),mday(data$date), sep = "-"))

最后一行几乎可以肯定是Roland在他的评论中的意思.他选择the年是正确的,因为它包含所有可能的日期.正常年份会错过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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆