Hi list, I have a question about 'plot'. I am trying to plot values registered every month - or every other month. If i build a data.frame called mydata like this (as an example) jan 3 1 7 mar 2 4 2 may 1 3 2 jul 3 7 4 sep 5 2 3 nov 3 1 5 and use the command line: plot(mydata[c(1,3)]) I get a graph that has on the x axis my months in alphabetical order - which i don't want, and instead of points i have thick horizontal lines. I've tried everything i could and understood from the R help files to give me points and on x axis the month in my order instead of alpha order. No success. What is the trick? I fixed the month order by using numerals in front of them like 01, 03, ... etc, but this is not an elegant solution. Any help will be much appreciated. Monica
You need to read R docs. Month is a factor. ?factor As a result you are getting a bar plot. You can make month numeric and control axis labelling via ?axis See also ?plot.default and ?par There are also undoubtedly functions available either in base R or through packages that would do this directly. You might check the hmisc and zoo packages for starters. -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Monica Pisica > Sent: Thursday, August 31, 2006 9:30 AM > To: r-help at stat.math.ethz.ch > Subject: [R] problems with plot.data.frame > Importance: High > > Hi list, > > I have a question about 'plot'. I am trying to plot values > registered every > month - or every other month. If i build a data.frame called > mydata like > this (as an example) > > jan 3 1 7 > mar 2 4 2 > may 1 3 2 > jul 3 7 4 > sep 5 2 3 > nov 3 1 5 > > and use the command line: > > plot(mydata[c(1,3)]) > > I get a graph that has on the x axis my months in > alphabetical order - which > i don't want, and instead of points i have thick horizontal > lines. I've > tried everything i could and understood from the R help files > to give me > points and on x axis the month in my order instead of alpha order. No > success. What is the trick? > > I fixed the month order by using numerals in front of them > like 01, 03, ... > etc, but this is not an elegant solution. > > Any help will be much appreciated. > > Monica > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
I perhaps should have added: ?plot.data.frame, ?plot.factor and ?UseMethod for S3 generic dispatch (plot.factor is the plotting method actually being called and you are actually getting a boxplot for a single value.) -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Monica Pisica > Sent: Thursday, August 31, 2006 9:30 AM > To: r-help at stat.math.ethz.ch > Subject: [R] problems with plot.data.frame > Importance: High > > Hi list, > > I have a question about 'plot'. I am trying to plot values > registered every > month - or every other month. If i build a data.frame called > mydata like > this (as an example) > > jan 3 1 7 > mar 2 4 2 > may 1 3 2 > jul 3 7 4 > sep 5 2 3 > nov 3 1 5 > > and use the command line: > > plot(mydata[c(1,3)]) > > I get a graph that has on the x axis my months in > alphabetical order - which > i don't want, and instead of points i have thick horizontal > lines. I've > tried everything i could and understood from the R help files > to give me > points and on x axis the month in my order instead of alpha order. No > success. What is the trick? > > I fixed the month order by using numerals in front of them > like 01, 03, ... > etc, but this is not an elegant solution. > > Any help will be much appreciated. > > Monica > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
Convert the column with the months to an actual date. I am going to assume this is all the same year, so try this:> x <- read.table('clipboard', as.is=T) > xV1 V2 V3 V4 1 jan 3 1 7 2 mar 2 4 2 3 may 1 3 2 4 jul 3 7 4 5 sep 5 2 3 6 nov 3 1 5> x$Date <- as.POSIXct(strptime(paste(x$V1, '1 2006'), format='%b %d %Y')) > xV1 V2 V3 V4 Date 1 jan 3 1 7 2006-01-01 2 mar 2 4 2 2006-03-01 3 may 1 3 2 2006-05-01 4 jul 3 7 4 2006-07-01 5 sep 5 2 3 2006-09-01 6 nov 3 1 5 2006-11-01> plot(x$Date, x$V2) >On 8/31/06, Monica Pisica <pisicandru at hotmail.com> wrote:> Hi list, > > I have a question about 'plot'. I am trying to plot values registered every > month - or every other month. If i build a data.frame called mydata like > this (as an example) > > jan 3 1 7 > mar 2 4 2 > may 1 3 2 > jul 3 7 4 > sep 5 2 3 > nov 3 1 5 > > and use the command line: > > plot(mydata[c(1,3)]) > > I get a graph that has on the x axis my months in alphabetical order - which > i don't want, and instead of points i have thick horizontal lines. I've > tried everything i could and understood from the R help files to give me > points and on x axis the month in my order instead of alpha order. No > success. What is the trick? > > I fixed the month order by using numerals in front of them like 01, 03, ... > etc, but this is not an elegant solution. > > Any help will be much appreciated. > > Monica > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
On Thu, 31 Aug 2006, Monica Pisica wrote:> Hi list, > > I have a question about 'plot'. I am trying to plot values registered every > month - or every other month. If i build a data.frame called mydata like > this (as an example) > > jan 3 1 7 > mar 2 4 2 > may 1 3 2 > jul 3 7 4 > sep 5 2 3 > nov 3 1 5This has no column names, so it looks like this is 3 columns with row names. I think it is actually four columns, but it would never print like that. Quite likely then the first column is a factor with levels in alphabetical order. If I copy your layout and do> read.table("clipboard") -> A > AV1 V2 V3 V4 1 jan 3 1 7 2 mar 2 4 2 3 may 1 3 2 4 jul 3 7 4 5 sep 5 2 3 6 nov 3 1 5> levels(A[[1]])[1] "jan" "jul" "mar" "may" "nov" "sep" and you seem to want a <- as.character(A[[1]]) A[[1]] <- factor(a, levels=a) and then plot.default(A[c(1,3)]) (You are showing a boxplot, since you are plotting a numeric variable against a factor.)> > and use the command line: > > plot(mydata[c(1,3)]) > > I get a graph that has on the x axis my months in alphabetical order - which > i don't want, and instead of points i have thick horizontal lines. I've > tried everything i could and understood from the R help files to give me > points and on x axis the month in my order instead of alpha order. No > success. What is the trick? > > I fixed the month order by using numerals in front of them like 01, 03, ... > etc, but this is not an elegant solution. > > Any help will be much appreciated. > > Monica > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595