New to R. I am trying to create a simple xy plot wherein the line
segment color is determined by a categorical column
The following does not change colors for me, probably because I don't
quite have a handle on either functions or value mapping syntax.
----------
time <- c(1, 2, 3, 7,10,11,14,16,20)
pressure <- c(0,10,20,20,50,18,60,65,90)
status <- c(0, 0, 1, 1, 1, 0, 3, 3, 3)
measures <- c(time,pressure,status)
attach(measures)
statusColor <- function (x) {
if (x==0) return ("green")
if (x==1) return ("orange")
if (x==2) return ("pink")
if (x==3) return ("red")
}
par(mfrow=c(3,2))
plot(time,pressure,type="l")
plot(time,pressure,type="l")
plot(time,pressure,type="l")
plot(time,pressure,type="l")
plot(time,pressure,type="l",col=statusColor(status))
plot(time,pressure,type="l")
----------
Warning message:
In if (x == 0) return("green") :
the condition has length > 1 and only the first element will be used
TIA,
Richard A. DeVenezia
Simple categorical scatter plot
6 messages · Richard DeVenezia, PIKAL Petr, Michael Bedward +2 more
Hi
I am not sure but you probably need to use segments.
?segments
AFAIK for line there is only one colour for whole line possible.
and in your function statusColor you shall not use if but ifelse or better
statusColor <- function (x) {
c("green","orange","pink","red")[x+1]
}
based on fact that x is vector of integers from 0 to 3.
Regards
Petr
r-help-bounces at r-project.org napsal dne 23.09.2010 13:38:43:
New to R. I am trying to create a simple xy plot wherein the line
segment color is determined by a categorical column
The following does not change colors for me, probably because I don't
quite have a handle on either functions or value mapping syntax.
----------
time <- c(1, 2, 3, 7,10,11,14,16,20)
pressure <- c(0,10,20,20,50,18,60,65,90)
status <- c(0, 0, 1, 1, 1, 0, 3, 3, 3)
measures <- c(time,pressure,status)
attach(measures)
statusColor <- function (x) {
if (x==0) return ("green")
if (x==1) return ("orange")
if (x==2) return ("pink")
if (x==3) return ("red")
}
par(mfrow=c(3,2))
plot(time,pressure,type="l")
plot(time,pressure,type="l")
plot(time,pressure,type="l")
plot(time,pressure,type="l")
plot(time,pressure,type="l",col=statusColor(status))
plot(time,pressure,type="l")
----------
Warning message:
In if (x == 0) return("green") :
the condition has length > 1 and only the first element will be used
TIA,
Richard A. DeVenezia
______________________________________________ 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.
Something like this ?
time <- c(1, 2, 3, 7,10,11,14,16,20)
pressure <- c(0,10,20,20,50,18,60,65,90)
status <- c(0, 0, 1, 1, 1, 0, 3, 3, 3)
statusColor <- c("green", "orange", "pink", "red")
plot(time, pressure)
for (i in 2:length(time)) lines(time[(i-1):i], pressure[(i-1):i],
col=statusColor[status[i]+1])
Michael
On 23 September 2010 21:38, Richard DeVenezia <rdevenezia at gmail.com> wrote:
New to R. ?I am trying to create a simple xy plot wherein the line
segment color is determined by a categorical column
The following does not change colors for me, probably because I don't
quite have a handle on either functions or value mapping syntax.
----------
time ? ? <- c(1, 2, 3, 7,10,11,14,16,20)
pressure <- c(0,10,20,20,50,18,60,65,90)
status ? <- c(0, 0, 1, 1, 1, 0, 3, 3, 3)
measures <- c(time,pressure,status)
attach(measures)
statusColor <- function (x) {
?if (x==0) return ("green")
?if (x==1) return ("orange")
?if (x==2) return ("pink")
?if (x==3) return ("red")
}
par(mfrow=c(3,2))
plot(time,pressure,type="l")
plot(time,pressure,type="l")
plot(time,pressure,type="l")
plot(time,pressure,type="l")
plot(time,pressure,type="l",col=statusColor(status))
plot(time,pressure,type="l")
----------
Warning message:
In if (x == 0) return("green") :
?the condition has length > 1 and only the first element will be used
TIA,
Richard A. DeVenezia
______________________________________________ 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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20100923/af7f4eb7/attachment.pl>
On 09/23/2010 09:38 PM, Richard DeVenezia wrote:
New to R. I am trying to create a simple xy plot wherein the line
segment color is determined by a categorical column
The following does not change colors for me, probably because I don't
quite have a handle on either functions or value mapping syntax.
----------
time<- c(1, 2, 3, 7,10,11,14,16,20)
pressure<- c(0,10,20,20,50,18,60,65,90)
status<- c(0, 0, 1, 1, 1, 0, 3, 3, 3)
measures<- c(time,pressure,status)
attach(measures)
statusColor<- function (x) {
if (x==0) return ("green")
if (x==1) return ("orange")
if (x==2) return ("pink")
if (x==3) return ("red")
}
par(mfrow=c(3,2))
plot(time,pressure,type="l")
plot(time,pressure,type="l")
plot(time,pressure,type="l")
plot(time,pressure,type="l")
plot(time,pressure,type="l",col=statusColor(status))
plot(time,pressure,type="l")
----------
Warning message:
In if (x == 0) return("green") :
the condition has length> 1 and only the first element will be used
Hi Richard, Look at the color.scale.lines function in the plotrix package. Jim
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20100923/5581de7f/attachment.pl>