I have a data.frame
with time series. There are also NA
s in it as well as there is a factor that I'd like to use to highlight different segments of a line.
flow.mndnr <- function(id, start, end) {
uri <- sprintf("http://maps1.dnr.state.mn.us/cgi-bin/csg.pl?mode=dump_hydro_data_as_csv&site=%s&startdate=%s&enddate=%s", id, start, end)
dat <- read.csv(url(uri), colClasses=c(Timestamp="Date"))
rng <- range(dat$Timestamp)
d <- data.frame(Timestamp=seq(rng[1], rng[2], by='day'))
merge(d, dat, all.x=TRUE)
}
dat <- flow.mndnr("28062001", as.Date("2002-04-02"), as.Date("2011-10-05"))
I can plot it unconditionally
library(lattice)
xyplot(Discharge..cfs. ~ Timestamp, dat, type='l', cex=0.5, auto.key=TRUE)
But I can't get rid of connecting lines when I try to introduce factor
xyplot(Discharge..cfs. ~ Timestamp, dat, type='l',
groups=dat$Discharge..cfs..Quality, cex=0.5, auto.key=TRUE)
Same with ggplot2
dat$quality <- dat$Discharge..cfs..Quality
ggplot(dat, aes(x=Timestamp, y=Discharge..cfs.)) +
geom_path(aes(colour=quality)) + theme(legend.position='bottom')
I tried geom_line
with no success. I read in ggplot2 mailing archive that geom_path
is the way to go. But it does not quite work for me.
P.S. Why ggplot2 does not like dots in a name so I had to use another one?