How to build a sparse (with spaces) line with its segments colored according to some factor in R?

I have data.framewith time series. It also has NA, and also has a factor that I would like to use to highlight different segments of the string.

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 do it unconditionally

library(lattice)
xyplot(Discharge..cfs. ~ Timestamp, dat, type='l', cex=0.5, auto.key=TRUE)

enter image description here

But I can’t get rid of the connecting lines when I try to introduce a factor

xyplot(Discharge..cfs. ~ Timestamp, dat, type='l',
    groups=dat$Discharge..cfs..Quality, cex=0.5, auto.key=TRUE)

enter image description here

The 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')

enter image description here

I tried geom_linewithout success. I read the mail archive in ggplot2 , which geom_pathis the way to go. But this does not work for me.

PS Why does ggplot2 not like dots in a name, so I had to use another one?

+5
source share
1 answer

. year, . :

dat$grp <- format(dat$Timestamp, "%Y")
ggplot(dat, aes(x=Timestamp, y=Discharge..cfs.)) +
    geom_path(aes(colour = quality, group = grp)) + 
    theme(legend.position='bottom')

:

enter image description here

:. : , , . , , . - :

dat$grp <- paste(format(dat$Timestamp, "%Y"), format(dat$Timestamp, "%m"))
ggplot(dat, aes(x=Timestamp, y=Discharge..cfs.)) +
    geom_path(aes(colour = quality, group = grp)) + 
    theme(legend.position='bottom')

:

enter image description here

+2

All Articles