plot data with different dates

Deadly 提交于 2019-12-11 07:11:46

问题


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

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.

I do the following:

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)

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?


回答1:


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~.)




回答2:


And a lattice alternative using @Michele df. I show here the 2 alternative (with and without faceting)

library(lattice)
library(gridExtra)
p1 <- xyplot(Value1~Month,groups=Year,data=df,
       type=c('p','l'),auto.key=list(columns=3,lines=TRUE))
p2 <- xyplot(Value1~Month|Year,groups=Year,data=df,layout= c(1,3),
       type=c('p','l'),auto.key=list(columns=3,lines=TRUE))
grid.arrange(p1,p2)




回答3:


Create a numeric value for your months:

x$MonthNum <- sapply(x$Month, function(x) which(x==month.name))

Then plot using those numeric values, but label the axes with words.

plot(NA, xaxt="n", xlab="Month", xlim=c(0,13),
    ylim=c(.96*min(x$Value),1.04*max(x$Value)), type="l")
z <- sapply(1996:1998, function(y) with(x[x$Year==y,], lines(MonthNum, Value1)))
axis(1, at=1:12, labels=month.name)

And some labels, if you want to identify years:

xlabpos <- tapply(x$MonthNum, x$Year, max)
ylabpos <- mapply(function(mon, year) x$Value1[x$MonthNum==mon & x$Year==year],     
    xlabpos, dimnames(xlabpos)[[1]])
text(x=xlabpos+.5, y=ylabpos, labels=dimnames(xlabpos)[[1]])

One could also obtain something similar to the ggplot example using layout:

par(mar=c(2,4,1,1))
layout(matrix(1:3))
z <- sapply(1996:1998, function(y) {
    with(x[x$Year==y,], plot(Value1 ~ MonthNum, xaxt="n", xlab="Month", ylab=y,
        xlim=c(0,13), ylim=c(.96*min(x$Value),1.04*max(x$Value)), type="l"))
    axis(1, at=1:12, labels=month.name)
})



来源:https://stackoverflow.com/questions/17364435/plot-data-with-different-dates

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!