绘制不同日期的数据 [英] plot data with different dates

查看:73
本文介绍了绘制不同日期的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对数据集的图有一些麻烦. 这是我的数据集的一部分.

i have some trouble with the plots of my dataset. This is an extract of my dataset.

   Date      Month Year  Value 1
30/05/96       May 1996 1835
06/12/96  December 1996 1770
18/03/97     March 1997 1640
27/06/97      June 1997 1379
30/09/97 September 1997 1195
24/11/97  November 1997 1335
13/03/98     March 1998 1790
07/05/98       May 1998  349
14/07/98      July 1998 1179
27/10/98   October 1998  665

我想做的是每年绘制一个值1(y)相对于坐骑(x)的图.换句话说,具有3条线的图显示了不同年份每个月值1的变化.

What I would like to do is a plot with Value 1 (y) against the mount (x) for every year. In other words, a plot with 3 lines that show the variation of Value 1 every month in th different years.

我执行以下操作:

plot(x[Year==1996,4], xaxt="n")
par(new=T)
plot(x[Year==1997,4], xaxt="n")
axis(1, at=1:length(x$Month), labels=x$Month)

问题在于1996年的第一个值表示可能,而1997年的第一个值表示进行曲.因此,绘制的值是混合的,不再与它们的月份相对应. 有没有办法在同一张图中绘制所有这些值,从而保持数据的原始对应关系?

The problem is that the first value of 1996 refers to may, and the first value of 1997 refers to march. Due to that, the values plotted are mixed and don't correspond to their month anymore. Is there a way to plot all these values in the same graph keeping the original correspondence of the data?

推荐答案

df <- read.table(text="Date      Month Year  Value1
30/05/96       May 1996 1835
06/12/96  December 1996 1770
18/03/97     March 1997 1640
27/06/97      June 1997 1379
30/09/97 September 1997 1195
24/11/97  November 1997 1335
13/03/98     March 1998 1790
07/05/98       May 1998  349
14/07/98      July 1998 1179
27/10/98   October 1998  665", header=T, as.is=T)

df$Month <- factor(df$Month, levels=month.name, ordered=T)

library(ggplot2)
ggplot(df) + geom_line(aes(Month, Value1, group=Year)) +
  facet_grid(Year~.)

这篇关于绘制不同日期的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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