[R] Simple Newbie Question

Gabor Grothendieck ggrothendieck at gmail.com
Fri Feb 22 01:03:51 CET 2008


On Thu, Feb 21, 2008 at 4:51 PM, MassimoH <MassimoHeitor at gmail.com> wrote:
>
> How do I plot data like this:
>
> Date,Series,Value
> 1/5/2007,Blue,300
> 1/20/2007,Blue,400
> 1/9/2007,Red,200
> 1/15/2007,Red,500
>
> Two things I'm having trouble figuring out from online help & tutorials:
>
> 1) How does R handle dates? It seems there is no built-in support for dates,
> but some standard add-ons provide date functionality? I can also convert my
> dates to number of days, however I'd like my plots to show date values on
> the x-axis.

See ?axis.Date for examles of plotting with dates.
See R News 4/1 for more on dates.  If you are
working with time series see the zoo package.

> 2) How do plots support multiple "series" of data? Using R terms, the Series
> is a "factor" type. I want each series to be plotted with a different color.

Lines <- "Date,Series,Value
1/5/2007,Blue,300
1/20/2007,Blue,400
1/9/2007,Red,200
1/15/2007,Red,500
"
library(lattice)
DF <- read.csv(textConnection(Lines))
DF$Date <- as.Date(DF$Date, "%m/%d/%Y")

# assuming Series contains valid R colors:
plot(Value ~ Date, DF, type = "n")
for (x in levels(DF$Series)) lines(Value ~ Date, DF, subset = Series
== x, col = x, type = "o")



More information about the R-help mailing list