Hi, I would like to plot time against rainfall data (data is at the end) using xyplot. The basic code looks like this: xyplot(rainfall~time, type="a") When I do this, the graph looks ok except that the x-axis has too many values. I would just like to display the years and not the months on the x-axis. I've been fiddling around with 'scales', and read previous posts about axis problems but I still can't find the solution. Thanks in advance. Kang Min time rainfall Jan1993 176.4 Feb1993 69.2 Mar1993 250.5 Apr1993 283.9 May1993 129.9 Jun1993 115.5 Jul1993 240 Aug1993 106.8 Sep1993 61.7 Oct1993 175.5 Nov1993 250.8 Dec1993 308.5 Jan1994 56.9 Feb1994 133.5 Mar1994 288.2 Apr1994 154 May1994 169.6 Jun1994 184.7 Jul1994 53.8 Aug1994 45.1 Sep1994 23.7 Oct1994 84.7 Nov1994 322.2 Dec1994 425.4 Jan1995 349.4 Feb1995 334 Mar1995 67.7 Apr1995 242.3 May1995 84.4 Jun1995 63.7 Jul1995 173.6 Aug1995 211.6 Sep1995 29.5 Oct1995 101.1 Nov1995 372.8 Dec1995 302.5 Jan1996 173.2 Feb1996 180.2 Mar1996 129.7 Apr1996 178.2 May1996 107.5 Jun1996 265.8 Jul1996 162.3 Aug1996 258.4 Sep1996 297 Oct1996 300 Nov1996 180.2 Dec1996 185.5 Jan1997 15.4 Feb1997 105.4 Mar1997 34.3 Apr1997 118.4 May1997 41.6 Jun1997 78.9 Jul1997 18.6 Aug1997 86.6 Sep1997 31.1 Oct1997 78.4 Nov1997 158.3 Dec1997 351.9 Jan1998 268.8 Feb1998 32.5 Mar1998 58.8 Apr1998 187.7 May1998 370.8 Jun1998 198.8 Jul1998 259.2 Aug1998 195 Sep1998 258.2 Oct1998 222.7 Nov1998 107.2 Dec1998 463.4 Jan1999 193.9 Feb1999 67.4 Mar1999 181.4 Apr1999 88.5 May1999 157.1 Jun1999 103.4 Jul1999 225.4 Aug1999 204 Sep1999 125.9 Oct1999 205 Nov1999 241.5 Dec1999 340.5 Jan2000 275.2 Feb2000 237.8 Mar2000 238.3 Apr2000 311.6 May2000 96.8 Jun2000 157.5 Jul2000 116.1 Aug2000 113.5 Sep2000 81.1 Oct2000 120.9 Nov2000 385.7 Dec2000 236 Jan2001 425.8 Feb2001 86.6 Mar2001 297.3 Apr2001 203.3 May2001 164.9 Jun2001 137.1 Jul2001 111.3 Aug2001 158.3 Sep2001 162 Oct2001 252.2 Nov2001 175.3 Dec2001 609 Jan2002 221.2 Feb2002 50.8 Mar2002 55.6 Apr2002 116.5 May2002 236.6 Jun2002 83.1 Jul2002 233.7 Aug2002 54.2 Sep2002 124.2 Oct2002 10.8 Nov2002 307.2 Dec2002 255 Jan2003 444.2 Feb2003 172.9 Mar2003 154.6 Apr2003 159.9 May2003 81.8 Jun2003 50.3 Jul2003 170.4 Aug2003 193.6 Sep2003 205.3 Oct2003 351.4 Nov2003 133.8 Dec2003 273 Jan2004 600.9 Feb2004 31.9 Mar2004 269.4 Apr2004 57.1 May2004 137.6 Jun2004 127.2 Jul2004 166.6 Aug2004 185.2 Sep2004 128.9 Oct2004 125.6 Nov2004 166.2 Dec2004 139.8 Jan2005 163.2 Feb2005 8.4 Mar2005 82.4 Apr2005 81.7 May2005 331.1 Jun2005 82.3 Jul2005 104 Aug2005 58.5 Sep2005 175.7 Oct2005 314.5 Nov2005 362.9 Dec2005 166 Jan2006 454.4 Feb2006 115.5 Mar2006 83.1 Apr2006 239.8 May2006 205.7 Jun2006 236.8 Jul2006 153.8 Aug2006 127.3 Sep2006 83.3 Oct2006 102 Nov2006 185.6 Dec2006 765.9 Jan2007 450.1 Feb2007 105.5 Mar2007 269.1 Apr2007 240.2 May2007 127.2 Jun2007 139 Jul2007 141.7 Aug2007 190.7 Sep2007 149 Oct2007 237.2 Nov2007 367.9 Dec2007 468.6
Hi: Try this, since your data have no missing months: rainfall$Time <- seq(from = as.Date('1993-01-01'), to as.Date('2007-12-01'), by = 'month') g <- ggplot(rainfall, aes(x = Time, y = rainfall)) g + geom_path() HTH, Dennis On Sun, Jan 16, 2011 at 5:01 AM, Kang Min <ngokangmin@gmail.com> wrote:> Hi, > > I would like to plot time against rainfall data (data is at the end) > using xyplot. > > The basic code looks like this: xyplot(rainfall~time, type="a") > When I do this, the graph looks ok except that the x-axis has too many > values. I would just like to display the years and not the months on > the x-axis. I've been fiddling around with 'scales', and read previous > posts about axis problems but I still can't find the solution. > > Thanks in advance. > > Kang Min > > time rainfall > Jan1993 176.4 > Feb1993 69.2 > Mar1993 250.5 > Apr1993 283.9 > May1993 129.9 > Jun1993 115.5 > Jul1993 240 > Aug1993 106.8 > Sep1993 61.7 > Oct1993 175.5 > Nov1993 250.8 > Dec1993 308.5 > Jan1994 56.9 > Feb1994 133.5 > Mar1994 288.2 > Apr1994 154 > May1994 169.6 > Jun1994 184.7 > Jul1994 53.8 > Aug1994 45.1 > Sep1994 23.7 > Oct1994 84.7 > Nov1994 322.2 > Dec1994 425.4 > Jan1995 349.4 > Feb1995 334 > Mar1995 67.7 > Apr1995 242.3 > May1995 84.4 > Jun1995 63.7 > Jul1995 173.6 > Aug1995 211.6 > Sep1995 29.5 > Oct1995 101.1 > Nov1995 372.8 > Dec1995 302.5 > Jan1996 173.2 > Feb1996 180.2 > Mar1996 129.7 > Apr1996 178.2 > May1996 107.5 > Jun1996 265.8 > Jul1996 162.3 > Aug1996 258.4 > Sep1996 297 > Oct1996 300 > Nov1996 180.2 > Dec1996 185.5 > Jan1997 15.4 > Feb1997 105.4 > Mar1997 34.3 > Apr1997 118.4 > May1997 41.6 > Jun1997 78.9 > Jul1997 18.6 > Aug1997 86.6 > Sep1997 31.1 > Oct1997 78.4 > Nov1997 158.3 > Dec1997 351.9 > Jan1998 268.8 > Feb1998 32.5 > Mar1998 58.8 > Apr1998 187.7 > May1998 370.8 > Jun1998 198.8 > Jul1998 259.2 > Aug1998 195 > Sep1998 258.2 > Oct1998 222.7 > Nov1998 107.2 > Dec1998 463.4 > Jan1999 193.9 > Feb1999 67.4 > Mar1999 181.4 > Apr1999 88.5 > May1999 157.1 > Jun1999 103.4 > Jul1999 225.4 > Aug1999 204 > Sep1999 125.9 > Oct1999 205 > Nov1999 241.5 > Dec1999 340.5 > Jan2000 275.2 > Feb2000 237.8 > Mar2000 238.3 > Apr2000 311.6 > May2000 96.8 > Jun2000 157.5 > Jul2000 116.1 > Aug2000 113.5 > Sep2000 81.1 > Oct2000 120.9 > Nov2000 385.7 > Dec2000 236 > Jan2001 425.8 > Feb2001 86.6 > Mar2001 297.3 > Apr2001 203.3 > May2001 164.9 > Jun2001 137.1 > Jul2001 111.3 > Aug2001 158.3 > Sep2001 162 > Oct2001 252.2 > Nov2001 175.3 > Dec2001 609 > Jan2002 221.2 > Feb2002 50.8 > Mar2002 55.6 > Apr2002 116.5 > May2002 236.6 > Jun2002 83.1 > Jul2002 233.7 > Aug2002 54.2 > Sep2002 124.2 > Oct2002 10.8 > Nov2002 307.2 > Dec2002 255 > Jan2003 444.2 > Feb2003 172.9 > Mar2003 154.6 > Apr2003 159.9 > May2003 81.8 > Jun2003 50.3 > Jul2003 170.4 > Aug2003 193.6 > Sep2003 205.3 > Oct2003 351.4 > Nov2003 133.8 > Dec2003 273 > Jan2004 600.9 > Feb2004 31.9 > Mar2004 269.4 > Apr2004 57.1 > May2004 137.6 > Jun2004 127.2 > Jul2004 166.6 > Aug2004 185.2 > Sep2004 128.9 > Oct2004 125.6 > Nov2004 166.2 > Dec2004 139.8 > Jan2005 163.2 > Feb2005 8.4 > Mar2005 82.4 > Apr2005 81.7 > May2005 331.1 > Jun2005 82.3 > Jul2005 104 > Aug2005 58.5 > Sep2005 175.7 > Oct2005 314.5 > Nov2005 362.9 > Dec2005 166 > Jan2006 454.4 > Feb2006 115.5 > Mar2006 83.1 > Apr2006 239.8 > May2006 205.7 > Jun2006 236.8 > Jul2006 153.8 > Aug2006 127.3 > Sep2006 83.3 > Oct2006 102 > Nov2006 185.6 > Dec2006 765.9 > Jan2007 450.1 > Feb2007 105.5 > Mar2007 269.1 > Apr2007 240.2 > May2007 127.2 > Jun2007 139 > Jul2007 141.7 > Aug2007 190.7 > Sep2007 149 > Oct2007 237.2 > Nov2007 367.9 > Dec2007 468.6 > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
On Sun, Jan 16, 2011 at 8:01 AM, Kang Min <ngokangmin at gmail.com> wrote:> Hi, > > I would like to plot time against rainfall data (data is at the end) > using xyplot. > > The basic code looks like this: xyplot(rainfall~time, type="a") > When I do this, the graph looks ok except that the x-axis has too many > values. I would just like to display the years and not the months on > the x-axis. I've been fiddling around with 'scales', and read previous > posts about axis problems but I still can't find the solution. >First we read in the data using zoo's "yearmon" class for the index: yearmon variables are stored as year plus a fraction to indicate the month such that January's fraction is zero so picking out the January indexes using cycle(z) == 1 gives the years. With lattice we need to convert that to numeric years but its unnecessary using classic graphics. library(zoo) z <- read.zoo("myfile.dat", header = TRUE, FUN = as.yearmon, format = "%b%Y") # using xyplot.zoo and lattice library(lattice) xyplot(z, scales = list(x = list(at = as.numeric(time(z))[cycle(z) == 1]))) # using plot.zoo and classic graphics plot(z, xaxt = "n") axis(1, time(z)[cycle(z) == 1]) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com