[R] Change line colors based on data values in Lattice
Chris Campbell
ccampbell at mango-solutions.com
Tue Jan 31 10:51:37 CET 2012
Hi Andrew
You could plot each section of your plot as a mini-line, something like this:
x = seq(1, 40)
y = seq(1, 40)^2
g = rep(c("A", "B", "A", "B"), each = 10)
newdf <- data.frame(x = x, y = y, g = g)
newpanel <- function(x, y, cols, ...) {
panel.xyplot(x, y, type = "n", ...)
for(i in seq_along(x)) {
panel.lines(c(x[i], x[i + 1]), c(y[i], y[i + 1]),
col = cols[i])
}
}
xyplot(y ~ x, data = newdf, groups = g, panel = newpanel,
cols = c("blue", "pink", "blue", "pink")[newdf$g])
Best wishes
Chris Campbell
MANGO SOLUTIONS
Data Analysis that Delivers
+44 1249 767700
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Liu, Andrew
Sent: 30 January 2012 21:09
To: 'r-help at r-project.org'
Subject: [R] Change line colors based on data values in Lattice
Say I have
x = seq(1, 40)
y = seq(1, 40)^2
g = rep(c("A", "B", "A", "B"), each = 10) xyplot(y~x, groups = g, type = 'l')
Is there a way to have a single line and have it be one color when g == "A", and another when g == "B"?
Thanks!
Regards,
Andrew Liu
Fixed Income | Investment Fellow
T. Rowe Price
100 E. Pratt St, BA-0735
Baltimore, MD 21202
* Office: (410) 345-2750
* E-mail: andrew_liu at troweprice.com<mailto:andrew_liu at troweprice.com>
* Please consider the environment before printing this e-mail.
T. Rowe Price (including T. Rowe Price Group, Inc. and its affiliates) and its associates do not provide legal or tax advice. Any tax-related discussion contained in this e-mail, including any attachments, is not intended or written to be used, and cannot be used, for the purpose of (i) avoiding any tax penalties or (ii) promoting, marketing, or recommending to any other party any transaction or matter addressed herein. Please consult your independent legal counsel and/or professional tax advisor regarding any legal or tax issues raised in this e-mail.
The contents of this e-mail and any attachments are intended solely for the use of the named addressee(s) and may contain confidential and/or privileged information. Any unauthorized use, copying, disclosure, or distribution of the contents of this e-mail is strictly prohibited by the sender and may be unlawful. If you are not the intended recipient, please notify the sender immediately and delete this e-mail.
______________________________________________
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.
LEGAL NOTICE
This message is intended for the use o...{{dropped:10}}
More information about the R-help
mailing list