Christopher W Ryan
2018-May-01 20:17 UTC
[R] How would I color points conditional on their value in a plot of a time series
How would I color points conditional on their value in a plot of a time series. Something like this: ## demonstration data ttt <- ts(rpois(12, lambda = 8), start = c(2000, 1), freq = 4) ttt plot(ttt, type = "p") ## doesn't work--all points the same color plot(ttt, type = "p", col = ifelse(ttt < 8, "black", "red")) ## also doesn't work--all points the same color q <- as.numeric(ttt) q plot(ttt, type = "p", col = ifelse(q < 8, "black", "red")) ## works OK with a simple, non-time-series scatterplot, as in sss <- data.frame(x = rpois(12, lambda = 8), y = rnorm(12, mean = 100, sd 25)) with(sss, plot(y ~ x, col = ifelse(y > 100, "black", "red"))) ## but I am missing something about time series. Thanks. --Chris Ryan Broome County Health Department and Binghamton University Binghamton, NY [[alternative HTML version deleted]]
William Dunlap
2018-May-01 20:33 UTC
[R] How would I color points conditional on their value in a plot of a time series
The ts method for plot() is quirky. You can use the default method: plot(as.vector(time(ttt)), as.vector(ttt), type = "p", col=ifelse(ttt<8, "black", "red")) Bill Dunlap TIBCO Software wdunlap tibco.com On Tue, May 1, 2018 at 1:17 PM, Christopher W Ryan <cryan at binghamton.edu> wrote:> How would I color points conditional on their value in a plot of a time > series. Something like this: > > ## demonstration data > ttt <- ts(rpois(12, lambda = 8), start = c(2000, 1), freq = 4) > ttt > plot(ttt, type = "p") > > ## doesn't work--all points the same color > plot(ttt, type = "p", col = ifelse(ttt < 8, "black", "red")) > > ## also doesn't work--all points the same color > q <- as.numeric(ttt) > q > plot(ttt, type = "p", col = ifelse(q < 8, "black", "red")) > > > ## works OK with a simple, non-time-series scatterplot, as in > > sss <- data.frame(x = rpois(12, lambda = 8), y = rnorm(12, mean = 100, sd > 25)) > with(sss, plot(y ~ x, col = ifelse(y > 100, "black", "red"))) > > ## but I am missing something about time series. > > Thanks. > > --Chris Ryan > Broome County Health Department > and Binghamton University > Binghamton, NY > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]
Christopher W Ryan
2018-May-01 21:00 UTC
[R] How would I color points conditional on their value in a plot of a time series
Excellent! Worked like a charm. Thanks. --Chris Ryan On Tue, May 1, 2018 at 4:33 PM, William Dunlap <wdunlap at tibco.com> wrote:> The ts method for plot() is quirky. You can use the default method: > > plot(as.vector(time(ttt)), as.vector(ttt), type = "p", col=ifelse(ttt<8, > "black", "red")) > > > Bill Dunlap > TIBCO Software > wdunlap tibco.com > > On Tue, May 1, 2018 at 1:17 PM, Christopher W Ryan <cryan at binghamton.edu> > wrote: > >> How would I color points conditional on their value in a plot of a time >> series. Something like this: >> >> ## demonstration data >> ttt <- ts(rpois(12, lambda = 8), start = c(2000, 1), freq = 4) >> ttt >> plot(ttt, type = "p") >> >> ## doesn't work--all points the same color >> plot(ttt, type = "p", col = ifelse(ttt < 8, "black", "red")) >> >> ## also doesn't work--all points the same color >> q <- as.numeric(ttt) >> q >> plot(ttt, type = "p", col = ifelse(q < 8, "black", "red")) >> >> >> ## works OK with a simple, non-time-series scatterplot, as in >> >> sss <- data.frame(x = rpois(12, lambda = 8), y = rnorm(12, mean = 100, sd >> >> 25)) >> with(sss, plot(y ~ x, col = ifelse(y > 100, "black", "red"))) >> >> ## but I am missing something about time series. >> >> Thanks. >> >> --Chris Ryan >> Broome County Health Department >> and Binghamton University >> Binghamton, NY >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posti >> ng-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> > >[[alternative HTML version deleted]]
Possibly Parallel Threads
- How would I color points conditional on their value in a plot of a time series
- How would I color points conditional on their value in a plot of a time series
- How would I color points conditional on their value in a plot of a time series
- stuck on making a line graph across time, with 4 categories
- adding vertical segments to an xyplot in lattice