简体   繁体   English

R中的堆栈时间序列图

[英]stack time series graph in R

I have the following data set: 我有以下数据集:

Percentage  Date    Category
0.001546278 1   A
0.002334523 2   A
0.00089928  3   A
0.00205846  4   A
0.000994036 1   B
0.000961275 2   B
6.00E-04    3   B
0.001132153 4   B
0.000773139 1   C
0.000411975 2   C
0.00039968  3   C
0.000205846 4   C

which I use to make a graph by: 我通过以下方法制作图形:

ggplot(data.frame(data), aes(x = Date, y = Percentage, fill = Category)) + 
  geom_area(position = 'stack')

what I would like is to get in the x-axes a date instead of the sequence 1-4: ie: Jan 2001, Feb 2002, March 2002 and April 2002. Any date format will do, as long as month and year appear. 我想要的是在x轴中输入一个日期而不是序列1-4:即:2001年1月,2002年2月,2002年3月和2002年4月。只要出现月和年,任何日期格式都可以。 Any suggestions on how to make it? 关于如何制作的任何建议?

Use yearmon() function from zoo package. 使用zoo包中的yearmon()函数。

For example : Substitute : 例如:替代:

     x = as.yearmon(as.Date("2015-12-25", %Y-%m-%d),"%y-%m")

You could do 你可以做

ggplot(data.frame(data), aes(x = as.Date("2000-12-01")+months(Date), y = Percentage, fill = Category)) + 
  geom_area(position = 'stack') + 
  scale_x_date(date_labels = "%b %Y")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM