Iara Faria
2011-Oct-17 21:23 UTC
[R] Beginner's question about plotting variables in a time series object with the date on the x axis
Dear R helpers, I am a beginner at R so please be gentle :) I have already read manuals and FAQs, with no help. I have a monthly time series data on public debt with 40 variables, it starts on January 1994 and ends on June 2011. I am loading the data into R using read.csv and the data looks ok when I do edit(dados):> dados<-read.csv("dadosR3.csv", header=T)then I tried making it into a time series object, using:> dados2<-ts(dados, start = c(1994,1), frequency=12)Now when I try plotting any of the variables, for example divliq.pib (net debt), the date doesn't appear on the x axis. How can I do this? I use> plot.ts(divliq.pib)also the function names() doesn't work, which would be important since I have so many variables.> names(dados2)NULL I am using R.2.13.1 Thank you very much. [[alternative HTML version deleted]]
R. Michael Weylandt
2011-Oct-17 22:38 UTC
[R] Beginner's question about plotting variables in a time series object with the date on the x axis
I'm not a huge fan of using R's ts class directly, much preferring to use zoo or xts (both of which are packages) to enhance. That said, I think your problem comes from identifying the data/variable name incorrectly. You've only "ts"ed dados, but you try to plot divliq.pib; try plot(dados[,"divliq.pib"]) instead. To get the names functionality, add the optional argument names= to the ts() call. If you go to xts, the following should work just fine for you: library(xts) dados<-read.csv("dadosR3.csv", header=T) dados<-xts(dados[,-1], as.Date(dados[,1])) # Assuming your date was in the left hand column Now dados will have its names inherited from the header of the CSV and you can plot them directly as plot(dados[,"divliq.pib"]) Hope this helps, Michael Weylandt On Mon, Oct 17, 2011 at 5:23 PM, Iara Faria <iaragohn at yahoo.com.br> wrote:> Dear R helpers, > > I am a beginner at R so please be gentle :) > I have already read manuals and FAQs, with no help. > I have a monthly?time series data on public debt with 40 variables, it starts on January 1994 and ends on June 2011. > I am loading the data into R using read.csv and the data looks ok when I do edit(dados): >> dados<-read.csv("dadosR3.csv", header=T) > then I tried making it into a time series object, using: >> dados2<-ts(dados, start = c(1994,1), frequency=12) > > Now when I try plotting any of the variables, for example divliq.pib (net debt), the date doesn't appear on the x axis. How can I do this? > I use >> plot.ts(divliq.pib) > > also the function names() doesn't work, which would be important since I have so many variables. >> names(dados2) > NULL > > I am using R.2.13.1 > > Thank you very much. > ? ? ? ?[[alternative HTML version deleted]] > > > ______________________________________________ > R-help at 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. > >
David Winsemius
2011-Oct-18 00:09 UTC
[R] Beginner's question about plotting variables in a time series object with the date on the x axis
On Oct 17, 2011, at 5:23 PM, Iara Faria wrote:> Dear R helpers, > > I am a beginner at R so please be gentle :) > I have already read manuals and FAQs, with no help. > I have a monthly time series data on public debt with 40 variables, > it starts on January 1994 and ends on June 2011.I agree with Weyland. Figuring out how to use class `ts` objects in R is difficult. If it were easy I don't think Zeileis would have invented `zoo`.> I am loading the data into R using read.csv and the data looks ok > when I do edit(dados): >> dados<-read.csv("dadosR3.csv", header=T) > then I tried making it into a time series object, using: >> dados2<-ts(dados, start = c(1994,1), frequency=12)(See below for what that did.)> > Now when I try plotting any of the variables, for example divliq.pib > (net debt), the date doesn't appear on the x axis. How can I do this? > I use >> plot.ts(divliq.pib)Try: plot(time(dados2), dados[, "divliq.pib"] , xy.labels=FALSE))> > also the function names() doesn't work, which would be important > since I have so many variables. >> names(dados2)Read the help page again(?). "A data frame will be coerced to a numeric matrix via data.matrix." The names function only works for lists and name vectors. Try colnames()> NULL > > I am using R.2.13.1 > > Thank you very much. > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.David Winsemius, MD West Hartford, CT