Re: [R] Help with plotting a line that is multicoloured based on levels of a factor
Thank you Jim and David for your help. The 'levels' call is not a misdirection, in my actual dataset it is necessary because the flows aren't symmetrical. So while your solution is quite elegant David, it doesn't apply to my actual data, just the example. Too bad, it's quite nice! I do think that color.scale.lines can work, now I just need to figure out how! Unfortunately when I tried your example Jim: date=c(1:300) flow=sin(2*pi/53*c(1:300)) levels=c(rep(c(high,med,low),100)) data=cbind.data.frame(date, flow, levels) plot(data$date,data$flow,type=n) library(plotrix) color.scale.lines(data$date,data$flow, col=color.scale(data$flow,extremes=c(blue,red))) I got this error: Error in length(redrange) : 'redrange' is missing But I do think the function is the way to go for my dataset. Thank you! -Pam -- View this message in context: http://r.789695.n4.nabble.com/Help-with-plotting-a-line-that-is-multicoloured-based-on-levels-of-a-factor-tp3385857p3412310.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Help with plotting a line that is multicoloured based on levels of a factor
On 2011-03-28 09:33, Pam Allen wrote: Thank you Jim and David for your help. The 'levels' call is not a misdirection, in my actual dataset it is necessary because the flows aren't symmetrical. So while your solution is quite elegant David, it doesn't apply to my actual data, just the example. Too bad, it's quite nice! I do think that color.scale.lines can work, now I just need to figure out how! Unfortunately when I tried your example Jim: date=c(1:300) flow=sin(2*pi/53*c(1:300)) levels=c(rep(c(high,med,low),100)) data=cbind.data.frame(date, flow, levels) plot(data$date,data$flow,type=n) library(plotrix) color.scale.lines(data$date,data$flow, col=color.scale(data$flow,extremes=c(blue,red))) I got this error: Error in length(redrange) : 'redrange' is missing The code works just fine for me with plotrix_3.0-7. It would help if you provided your sessionInfo(). Peter Ehlers But I do think the function is the way to go for my dataset. Thank you! -Pam -- View this message in context: http://r.789695.n4.nabble.com/Help-with-plotting-a-line-that-is-multicoloured-based-on-levels-of-a-factor-tp3385857p3412310.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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. __ R-help@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.
Re: [R] Help with plotting a line that is multicoloured based on levels of a factor
Hello Baptiste and others, I tried your example with my dataset, and for a few days I thought it worked for me. But I realized yesterday that the result wasn't quite what I hoped for. In my actual data the flows aren't perfectly sinusoidal, and I used a series of ifelse queries to code the flows into their different categories (i.e., extremely high, high, low, extremely low). Your solution almost worked, except that some flows are coloured incorrectly. I think the issue lies in the use of the transform or approx functions. I tried to understand what they do, but I wasn't able to figure it out. Is there a way to use the exact data set, i.e.: date=c(1:300) flow=sin(2*pi/53*c(1:300)) levels=c(rep(c(high,med,low),100)) data=cbind.data.frame(date, flow, levels) With the following colours: colour=ifelse(data$levels==high,red, ifelse(data$levels==med,green, ifelse(data$levels==low,blue,))) And plot a line without having to create new data, i.e. d? Thank you. -Pam allen_...@hotmail.com -- View this message in context: http://r.789695.n4.nabble.com/Help-with-plotting-a-line-that-is-multicoloured-based-on-levels-of-a-factor-tp3385857p3406199.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Help with plotting a line that is multicoloured based on levels of a factor
On Mar 25, 2011, at 3:23 PM, Pam Allen wrote: Hello Baptiste and others, I tried your example with my dataset, and for a few days I thought it worked for me. But I realized yesterday that the result wasn't quite what I hoped for. In my actual data the flows aren't perfectly sinusoidal, and I used a series of ifelse queries to code the flows into their different categories (i.e., extremely high, high, low, extremely low). Your solution almost worked, except that some flows are coloured incorrectly. I think the issue lies in the use of the transform or approx functions. I tried to understand what they do, but I wasn't able to figure it out. Is there a way to use the exact data set, i.e.: date=c(1:300) flow=sin(2*pi/53*c(1:300)) levels=c(rep(c(high,med,low),100)) data=cbind.data.frame(date, flow, levels) Don't use `data` as a name. It's a function. With the following colours: colour=ifelse(data$levels==high,red, ifelse(data$levels==med,green, ifelse(data$levels==low,blue,))) And plot a line without having to create new data, i.e. d? with(dat,plot(date,flow,type=n)) with(dat, segments(date[1:299],flow[1:299], # starting points for segments date[2:300],flow[2:300], # ending points offset by 1 col=c(red,green,blue)[levels[1:299]])) I'mn ot sure the colors line up because you didn't define your factor in a manner that was properly ordered; str(dat) 'data.frame': 300 obs. of 3 variables: $ date : int 1 2 3 4 5 6 7 8 9 10 ... $ flow : num 0.118 0.235 0.348 0.457 0.559 ... $ levels: Factor w/ 3 levels high,low,med: 1 3 2 1 3 2 1 3 2 1 ... Better would have been to relevel after creating `levels` AND NOT USE `levels` as a name. It's an argument name in factor: levels=factor(levels, levels=c(high,med,low) ) Now you know what order they will be handled when used as an index. -- David Winsemius, MD West Hartford, CT __ R-help@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.
Re: [R] Help with plotting a line that is multicoloured based on levels of a factor
Hello again, I wrote an example that better represents my data, since the coloured points are actually consecutive, but with variable lengths: date=as.Date(c(1:300)) flow=sin(2*pi/53*c(1:300)) levels=c(rep(c(high,med,low),100)) data=cbind.data.frame(date, flow, levels) library(zoo) z - zoo(data$flow, data$date) zz=cbind.data.frame(date=as.Date(rownames(cbind.data.frame(rollapply(z, 2, align = right, FUN=+,flow.change=(rollapply(z, 2, align = right,FUN=+ ))) names(zz)=c(date,todays.flow,next.day.flow) zzz=cbind.data.frame(zz[,1], (zz[,3]-zz[,2])) names(zzz)=c(date,change.flow) data2=merge(data, zzz) rate=zoo(data2$change.flow,data2$date) x=cbind.data.frame(date=as.Date(rownames(cbind.data.frame(rollapply(rate, 2, align=left, FUN=+, sign=rollapply(rate, 2, align=left,FUN=+)) names(x)=c(date,todays.change,next.day.change) xx=cbind.data.frame(x[,1],(x[,3]*x[,2])) names(xx)=c(date,sign) data2=merge(data2, xx) data3=cbind(data2,pass1= ifelse(data2$flow0, extreme.low, ifelse(data2$flow=0.9, extreme.high,NA))) data4=cbind(data3, pass2= ifelse(data3$flow0.8data3$flow0data3$change.flow=0data3$change=0data3$pass1==NA,medium , ifelse(data3$flow0.7data3$flow0data3$change.flow0data3$change0data3$pass1==NA,medium,NA))) data4$pass1=paste(data4$pass1, data4$pass2) data4$pass1=replace(data4$pass1, data4$pass1==NA NA, low) dat=cbind(data4[,1:5], class= ifelse(data4$pass1==extreme.high NA,1.Extreme.High, ifelse(data4$pass1==NA medium,2.Medium, ifelse(data4$pass1==low,3.Low, ifelse(data4$pass1==extreme.low NA,4.Extreme.Low,NA) colour=ifelse(dat$class==1.Extreme.High,red, ifelse(dat$class==2.Medium,green, ifelse(dat$class==3.Low,blue, ifelse(dat$class==4.Extreme.Low,purple, plot(dat$date, dat$flow, col=colour) What I would like to do is to plot this using a line with the correct colours instead of points, i.e.: plot(dat$date, dat$flow, col=colour, type=l) ##Doesn't work, because the line is continuous Any help would be much appreciated. Thank you! -Pam -- View this message in context: http://r.789695.n4.nabble.com/Help-with-plotting-a-line-that-is-multicoloured-based-on-levels-of-a-factor-tp3385857p3406309.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Help with plotting a line that is multicoloured based on levels of a factor
On 03/26/2011 07:19 AM, Pam Allen wrote: Hello again, I wrote an example that better represents my data, since the coloured points are actually consecutive, but with variable lengths: date=as.Date(c(1:300)) flow=sin(2*pi/53*c(1:300)) levels=c(rep(c(high,med,low),100)) data=cbind.data.frame(date, flow, levels) library(zoo) z- zoo(data$flow, data$date) zz=cbind.data.frame(date=as.Date(rownames(cbind.data.frame(rollapply(z, 2, align = right, FUN=+,flow.change=(rollapply(z, 2, align = right,FUN=+ ))) names(zz)=c(date,todays.flow,next.day.flow) zzz=cbind.data.frame(zz[,1], (zz[,3]-zz[,2])) names(zzz)=c(date,change.flow) data2=merge(data, zzz) rate=zoo(data2$change.flow,data2$date) x=cbind.data.frame(date=as.Date(rownames(cbind.data.frame(rollapply(rate, 2, align=left, FUN=+, sign=rollapply(rate, 2, align=left,FUN=+)) names(x)=c(date,todays.change,next.day.change) xx=cbind.data.frame(x[,1],(x[,3]*x[,2])) names(xx)=c(date,sign) data2=merge(data2, xx) data3=cbind(data2,pass1= ifelse(data2$flow0, extreme.low, ifelse(data2$flow=0.9, extreme.high,NA))) data4=cbind(data3, pass2= ifelse(data3$flow0.8data3$flow0data3$change.flow=0data3$change=0data3$pass1==NA,medium , ifelse(data3$flow0.7data3$flow0data3$change.flow0data3$change0data3$pass1==NA,medium,NA))) data4$pass1=paste(data4$pass1, data4$pass2) data4$pass1=replace(data4$pass1, data4$pass1==NA NA, low) dat=cbind(data4[,1:5], class= ifelse(data4$pass1==extreme.high NA,1.Extreme.High, ifelse(data4$pass1==NA medium,2.Medium, ifelse(data4$pass1==low,3.Low, ifelse(data4$pass1==extreme.low NA,4.Extreme.Low,NA) colour=ifelse(dat$class==1.Extreme.High,red, ifelse(dat$class==2.Medium,green, ifelse(dat$class==3.Low,blue, ifelse(dat$class==4.Extreme.Low,purple, plot(dat$date, dat$flow, col=colour) What I would like to do is to plot this using a line with the correct colours instead of points, i.e.: plot(dat$date, dat$flow, col=colour, type=l) ##Doesn't work, because the line is continuous Hi Pam, I couldn't get the above example to run, and I've arrived at this problem rather late. However, if I have guessed your original question correctly, this might help: date=c(1:300) flow=sin(2*pi/53*c(1:300)) levels=c(rep(c(high,med,low),100)) data=cbind.data.frame(date, flow, levels) plot(data$date,data$flow,type=n) library(plotrix) color.scale.lines(data$date,data$flow, col=color.scale(data$flow,extremes=c(blue,red))) The clplot function in plotrix is similar and might also be of interest. Jim __ R-help@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.
[R] Help with plotting a line that is multicoloured based on levels of a factor
Hi All, I'm trying to plot data that is a time series of flows that are associated with a specific level, and I would like each level to represent a colour in a line plot. Here is some data that approximates what I'm using: date=c(1:300) flow=sin(2*pi/53*c(1:300)) levels=c(rep(c(high,med,low),100)) data=cbind.data.frame(date, flow, levels) the levels column represents the levels of flow. What I've done so far is to plot this data using coloured points corresponding with each flow level: colour=ifelse(data$levels==high,red, ifelse(data$levels==med,green, ifelse(data$levels==low,blue,))) plot(date, flow, col=colour) What I would like to do instead is to plot the line of this data, not the points. i.e., plot(date, flow, type=l) But I would like the colour of the line to change with each level, i.e., plot(date, flow, type=l, col=colour) But this doesn't work because the line is continuous and the colours are discrete. I looked into using clipplot, but I'm not sure how I would specify limits that would give different sections of the line correct colours. Does anyone know of a way to draw a line with different colours? One way I thought of was to plot each level of flow separately and then build the plot up, i.e., plot(data$date[data$levels==high], data$flow[data$levels==high], col=red, type=l) lines(data$date[data$levels==med], data$flow[data$levels==med], col=green, type=l) lines(data$date[data$levels==low], data$flow[data$levels==low], col=blue, type=l) But the line fills in data gaps, so this doesn't work. Any help would be much appreciated! Thank you. -Pam Allen pal...@hatfieldgroup.com __ R-help@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.
Re: [R] Help with plotting a line that is multicoloured based on levels of a factor
Hi! Not an elegant solution, but seems to work: date - c(1:300) flow - sin(2*pi/53*c(1:300)) levels - factor(rep(c(high,med,low),100)) data - cbind.data.frame(date, flow, levels) colours - as.numeric(levels)+1 # interpolate resolution - 0.001 appres - approx(date,flow,seq(min(date),max(date),resolution)) appres.colour - rep(colours,each=1/resolution)[1:length(appres[[1]])] # plot plot(appres,col=appres.colour,pch=16,cex=0.5) Of course you should play with the resolution, pch and cex parameters to get a higher quality plot, or might use other function for interpolation. Regards, Denes Hi All, I'm trying to plot data that is a time series of flows that are associated with a specific level, and I would like each level to represent a colour in a line plot. Here is some data that approximates what I'm using: date=c(1:300) flow=sin(2*pi/53*c(1:300)) levels=c(rep(c(high,med,low),100)) data=cbind.data.frame(date, flow, levels) the levels column represents the levels of flow. What I've done so far is to plot this data using coloured points corresponding with each flow level: colour=ifelse(data$levels==high,red, ifelse(data$levels==med,green, ifelse(data$levels==low,blue,))) plot(date, flow, col=colour) What I would like to do instead is to plot the line of this data, not the points. i.e., plot(date, flow, type=l) But I would like the colour of the line to change with each level, i.e., plot(date, flow, type=l, col=colour) But this doesn't work because the line is continuous and the colours are discrete. I looked into using clipplot, but I'm not sure how I would specify limits that would give different sections of the line correct colours. Does anyone know of a way to draw a line with different colours? One way I thought of was to plot each level of flow separately and then build the plot up, i.e., plot(data$date[data$levels==high], data$flow[data$levels==high], col=red, type=l) lines(data$date[data$levels==med], data$flow[data$levels==med], col=green, type=l) lines(data$date[data$levels==low], data$flow[data$levels==low], col=blue, type=l) But the line fills in data gaps, so this doesn't work. Any help would be much appreciated! Thank you. -Pam Allen pal...@hatfieldgroup.com __ R-help@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. __ R-help@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.
Re: [R] Help with plotting a line that is multicoloured based on levels of a factor
Hi, because each colour is defined on non-consecutive points, you'll probably need to cut the intervals to define segments around each point. One approach might be the following, d = transform(data, start = date - c(0, diff(date)/2), end = date + c(0, diff(date)/2) ) d$start.y = approx(d$date, d$flow, d$start)$y d$end.y = approx(d$date, d$flow, d$end)$y library(ggplot2) ggplot(d) + geom_segment(aes(x=start,y=start.y, xend=end, yend=end.y, colour=levels)) HTH, baptiste On 18 March 2011 11:33, Pamela Allen pal...@hatfieldgroup.com wrote: Hi All, I'm trying to plot data that is a time series of flows that are associated with a specific level, and I would like each level to represent a colour in a line plot. Here is some data that approximates what I'm using: date=c(1:300) flow=sin(2*pi/53*c(1:300)) levels=c(rep(c(high,med,low),100)) data=cbind.data.frame(date, flow, levels) the levels column represents the levels of flow. What I've done so far is to plot this data using coloured points corresponding with each flow level: colour=ifelse(data$levels==high,red, ifelse(data$levels==med,green, ifelse(data$levels==low,blue,))) plot(date, flow, col=colour) What I would like to do instead is to plot the line of this data, not the points. i.e., plot(date, flow, type=l) But I would like the colour of the line to change with each level, i.e., plot(date, flow, type=l, col=colour) But this doesn't work because the line is continuous and the colours are discrete. I looked into using clipplot, but I'm not sure how I would specify limits that would give different sections of the line correct colours. Does anyone know of a way to draw a line with different colours? One way I thought of was to plot each level of flow separately and then build the plot up, i.e., plot(data$date[data$levels==high], data$flow[data$levels==high], col=red, type=l) lines(data$date[data$levels==med], data$flow[data$levels==med], col=green, type=l) lines(data$date[data$levels==low], data$flow[data$levels==low], col=blue, type=l) But the line fills in data gaps, so this doesn't work. Any help would be much appreciated! Thank you. -Pam Allen pal...@hatfieldgroup.com __ R-help@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. __ R-help@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.
Re: [R] Help with plotting a line that is multicoloured based on levels of a factor
Indeed, I forgot about the segments function. with(d,plot(date,flow,type=n)) with(d,segments(start,start.y,end,end.y,col=colour)) Hi, because each colour is defined on non-consecutive points, you'll probably need to cut the intervals to define segments around each point. One approach might be the following, d = transform(data, start = date - c(0, diff(date)/2), end = date + c(0, diff(date)/2) ) d$start.y = approx(d$date, d$flow, d$start)$y d$end.y = approx(d$date, d$flow, d$end)$y library(ggplot2) ggplot(d) + geom_segment(aes(x=start,y=start.y, xend=end, yend=end.y, colour=levels)) HTH, baptiste On 18 March 2011 11:33, Pamela Allen pal...@hatfieldgroup.com wrote: Hi All, I'm trying to plot data that is a time series of flows that are associated with a specific level, and I would like each level to represent a colour in a line plot. Here is some data that approximates what I'm using: date=c(1:300) flow=sin(2*pi/53*c(1:300)) levels=c(rep(c(high,med,low),100)) data=cbind.data.frame(date, flow, levels) the levels column represents the levels of flow. What I've done so far is to plot this data using coloured points corresponding with each flow level: colour=ifelse(data$levels==high,red, ifelse(data$levels==med,green, ifelse(data$levels==low,blue,))) plot(date, flow, col=colour) What I would like to do instead is to plot the line of this data, not the points. i.e., plot(date, flow, type=l) But I would like the colour of the line to change with each level, i.e., plot(date, flow, type=l, col=colour) But this doesn't work because the line is continuous and the colours are discrete. I looked into using clipplot, but I'm not sure how I would specify limits that would give different sections of the line correct colours. Does anyone know of a way to draw a line with different colours? One way I thought of was to plot each level of flow separately and then build the plot up, i.e., plot(data$date[data$levels==high], data$flow[data$levels==high], col=red, type=l) lines(data$date[data$levels==med], data$flow[data$levels==med], col=green, type=l) lines(data$date[data$levels==low], data$flow[data$levels==low], col=blue, type=l) But the line fills in data gaps, so this doesn't work. Any help would be much appreciated! Thank you. -Pam Allen pal...@hatfieldgroup.com __ R-help@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. __ R-help@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. __ R-help@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.
Re: [R] Help with plotting a line that is multicoloured based on levels of a factor
On Mar 17, 2011, at 6:33 PM, Pamela Allen wrote: Hi All, I'm trying to plot data that is a time series of flows that are associated with a specific level, and I would like each level to represent a colour in a line plot. Here is some data that approximates what I'm using: date=c(1:300) flow=sin(2*pi/53*c(1:300)) levels=c(rep(c(high,med,low),100)) data=cbind.data.frame(date, flow, levels) the levels column represents the levels of flow. What I've done so far is to plot this data using coloured points corresponding with each flow level: colour=ifelse(data$levels==high,red, ifelse(data$levels==med,green, ifelse(data$levels==low,blue,))) plot(date, flow, col=colour) What I would like to do instead is to plot the line of this data, not the points. i.e., plot(date, flow, type=l) But I would like the colour of the line to change with each level, i.e., plot(date, flow, type=l, col=colour) But this doesn't work because the line is continuous and the colours are discrete. I looked into using clipplot, but I'm not sure how I would specify limits that would give different sections of the line correct colours. Does anyone know of a way to draw a line with different colours? One way I thought of was to plot each level of flow separately and then build the plot up, i.e., plot(data$date[data$levels==high], data$flow[data$levels==high], col=red, type=l) lines(data$date[data$levels==med], data$flow[data$levels==med], col=green, type=l) lines(data$date[data$levels==low], data$flow[data$levels==low], col=blue, type=l) But the line fills in data gaps, so this doesn't work. I haven't worked through what you have done but it sounds as though you want this function from the plotrix package; color.scale.lines # Display line segments with scaled colors Any help would be much appreciated! Thank you. -Pam Allen pal...@hatfieldgroup.com . David Winsemius, MD West Hartford, CT __ R-help@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.