Hi:
Evidently I misunderstood your intention, so let's try again. I tried to
find a lattice solution but came up empty. I think this works instead using
base graphics. The idea is to build up vectors of start points (x0, y0),
endpoints (x1, y1) and the color of the segment between each pair of points.
The last line is to call segments, which (fortunately) takes vector
arguments. The last element is removed from the vectors to get the start
points, while the first element is removed to get the end points; if it
doesn't make sense, cbind them and it should be clearer.
x0 <- pressure[-length(pressure)]
y0 <- time[-length(time)]
x0 <- time[-length(time)]
y0 <- pressure[-length(pressure)]
x1 <- time[-1]
y1 <- pressure[-1]
cols <- c(rep('green', 2), rep('orange', 3), 'green', rep('red', 2))
# Set up plot area, but don't plot (yet)
plot(time, pressure, type = 'n')
segments(x0, y0, x1, y1, col = cols)
Hope I got it right this time,
Dennis
PS: Something like this ought to work in lattice, too. A message to that
effect is cited here:
http://r.789695.n4.nabble.com/forum/PrintPost.jtp?post=2165353
On Thu, Sep 23, 2010 at 5:14 AM, Dennis Murphy <[email protected]> wrote:
> Hi:
>
> 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)
>
> # You want to combine the vectors into a data frame instead
> # of concatenating them into one long vector.
> df <- data.frame(time, pressure, status)
> df
> time pressure status
> 1 1 0 0
> 2 2 10 0
> 3 3 20 1
> 4 7 20 1
> 5 10 50 1
> 6 11 18 0
> 7 14 60 3
> 8 16 65 3
> 9 20 90 3
>
> library(lattice)
> mycols <- c('green', 'orange', 'red')
> xyplot(pressure ~ time, data = df, group = status, type = 'b', col =
> mycols)
>
> For this type of graphic, the lattice package works well. The package has
> its own book and extensive help pages.
>
>
>
>
> On Thu, Sep 23, 2010 at 4:38 AM, Richard DeVenezia
> <[email protected]>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)
>>
>> # A bad idea. Don't use attach(); use the function with(dataframe,
> expression) instead; see ?with
>
>
>> 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
>>
>>
>>
> HTH,
> Dennis
>
>
>> TIA,
>> Richard A. DeVenezia
>>
>> ______________________________________________
>> [email protected] 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.
>>
>
>
[[alternative HTML version deleted]]
______________________________________________
[email protected] 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.