1

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?

matteo
  • 4,683
  • 9
  • 41
  • 77

3 Answers3

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

enter image description here

Michele
  • 8,563
  • 6
  • 45
  • 72
  • A nice alternative for both data transformation and plotting. – Thomas Jun 28 '13 at 13:03
  • @agstudy I agree. I just followed the OP question `a plot with Value 1 (y) against the mount (x) for every year. In other words, a plot with 3 lines... ` he asked for 3 lines. @matteo Am I correct? – Michele Jun 28 '13 at 13:16
2

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]])

single plot, multiple lines

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)
})

multiple plots

Thomas
  • 43,637
  • 12
  • 109
  • 140
  • Thanks for the quickly reply. I did what you said but unfortunately the 2nd and 3rd parts (so 1997 and 1998) are not plotted. – matteo Jun 28 '13 at 12:47
  • @matteo have tried mine? unfortunately somehow I can't upload the image of plot, damned SO :-) – Michele Jun 28 '13 at 12:57
  • oh now I can see the images, temporary issue of the system maybe(?) – Michele Jun 28 '13 at 13:04
  • sorry, how do you distinguish the year? I think you need a legend like in @agstudy upper plot – Michele Jun 28 '13 at 13:25
  • @Thomas of course is a personal decision, what I mean is that whatever you wanna use use it :-)... this way is misleading... – Michele Jun 28 '13 at 13:33
  • 1
    Oh yea, agree, I just added them. Just had to work out the code to do it programmatically with these data. – Thomas Jun 28 '13 at 13:39
  • yep! less pretty than the other two answers but +1 for doing in base r! – Michele Jun 28 '13 at 14:32
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)

enter image description here

agstudy
  • 119,832
  • 17
  • 199
  • 261
  • may I ask a quick question? I use `ggplot2` for reports, would you suggest to give a try to `lattice` (never used)? I mean, in your work, have you ever faced a situation when `lattice` was able to provide something more? (in terms of appearance and ease of comprehension for the viewer of the plot) – Michele Jun 28 '13 at 13:40
  • 1
    @Michele You should consider the strongest points of each system. `ggplot2` is very good to talk a story about data : his powerful aes system and . For waht the equivalent of aes shape. size? in lattice see. Also `stat_summary` also (hard to find a lattice equivalent) in the other side `lattice` system is simpler than ggplot2 (less viewports behind the scene) so it is more flexible and easier to customize (everything can be customized by a function); Easy to call `grid` functions within `lattice panel` and i think that `lattice` also is faster...I can't write more here – agstudy Jun 28 '13 at 14:00
  • it's ok don't worry. Thanks a lot! – Michele Jun 28 '13 at 14:04
  • does `lattice` allow to change the `strip.text` (as they named in `ggplot`...) or you just preferred to leave colour (and so the legend)? – Michele Jun 28 '13 at 15:16
  • 1
    @Michele it is the default choice here(lazy one). But see [this answer](http://stackoverflow.com/questions/17233575/plotting-multiple-lines-r/17235330#17235330) on how to customize the strip. – agstudy Jun 28 '13 at 15:21