Site data with different dates

I have problems with the plots of my dataset. This is an excerpt from 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 graph with a value of 1 (y) versus mount (x) for each year. In other words, a graph with three lines that show a change in the value of 1 every month in 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 the march. In this regard, the values ​​plotted on the graph are mixed and no longer correspond to their month. Is there a way to plot all of these values ​​on the same graph while maintaining the original data fit?

+1
source share
3 answers
 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

+3
source

Create a numerical value for your months:

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

Then draw these numerical 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 shortcuts if you want to define 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

You can also get 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

+2
source

And an alternative to lattice using @Michele df. I show here 2 alternatives (with and without cut)

 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

+2
source

Source: https://habr.com/ru/post/1498373/


All Articles