Re: [R] add a color band
Hi Jim Lemon, thanks for the help, I appreciate this. right now my code looks like. par(mar=c(5,4,4,5)) color2D.matplot(data,1,c(0,1),0,xlab=,ylab=Spans, main=color.scale,xrange=c(-110,-50),border=NA,axes=F) color.legend(357,30,370,100,seq(-110,-50,length.out=13), align=rb,rect.col=color.scale(1:13,1,c(0,1),0), gradient=y) my major problem now is that the a. text in the color bar is squeezed so -50 overlaps with -60 and so on b. for some reason the color bar sometimes (the same code is called for all the data matrices I have) is misaligned in different positions each time Could you please also help me with those two? Regards Alex On Monday, October 28, 2013 9:00 AM, Alaios ala...@yahoo.com wrote: Hi Jim and thanks for your answer... I might be too tired with my new born or just exhausted. I am sharing for everyone a small data snipset that you can load https://www.dropbox.com/s/fh8jhwujgunmtrb/DataToPlotAsImage.Rdata load(DataToPlotAsImage.Rdata) require(plotrix) browser() test-data # this transforms the values of test into red-yellow color2D.matplot(test,axes=F,xlab=,ylab=,main=color.scale, extremes=c(#FF,#00),show.legend=FALSE) axis(1,at=seq(1,ncol(test),length.out=10),labels=seq(201,300,length.out=10)) color.legend(104,30,112,70,seq(-110,-30,length=11), align=rb,rect.col=color.scale(1:30,1,c(0,1),0),gradient=y) as you can see I have problems where the legend appears. My par(usr returned me par(usr) # [1] 0 351 0 200 but I am not sure how to read that to place the legend at a useful place. second I am not sure why the image is so full with black rows.. What I want is to have the legend visible and later on customize the x axis to write custom string of different size... First I need though to fix the more severe problems as I have described RegardsAlex On Sunday, October 27, 2013 12:25 PM, Jim Lemon j...@bitwrit.com.au wrote: On 10/27/2013 08:39 AM, Alaios wrote: Hi Jim and thanks for your answer... I might be too tired with my new born or just exhausted. I am attaching for everyone a small data snipset that you can load load(DataToPlotAsImage.Rdata) require(plotrix) browser() test-data # this transforms the values of test into red-yellow color2D.matplot(test,axes=F,xlab=,ylab=,main=color.scale, extremes=c(#FF,#00),show.legend=FALSE) axis(1,at=seq(1,ncol(test),length.out=10),labels=seq(201,300,length.out=10)) color.legend(104,30,112,70,seq(-110,-30,length=11), align=rb,rect.col=color.scale(1:30,1,c(0,1),0),gradient=y) as you can see I have problems where the legend appears. My par(usr returned me par(usr) # [1] 0 351 0 200 but I am not sure how to read that to place the legend at a useful place. second I am not sure why the image is so full with black rows.. What I want is to have the legend visible and later on customize the x axis to write custom string of different size... First I need though to fix the more severe problems as I have described Hi Alex, I'm not sure why you have created a copy of data to plot it. I can get quite a sensible plot using this: par(mar=c(5,4,4,5)) color2D.matplot(data,1,c(0,1),0,xlab=,ylab=, main=color.scale,xrange=c(-110,-50),border=NA) color.legend(357,30,370,100,seq(-110,-50,length.out=6), align=rb,rect.col=color.scale(1:6,1,c(0,1),0), gradient=y) Notice several things. First, when you have a large number of cells in a plot like this, setting the border to NA means that you don't get mostly borders (default = black) in the plot. The second thing is that your data range is -107.18150 to -54.07662. In order to get rounded numbers in your legend, I have set the xrange argument to -110 to -50. This gives a neat looking legend that spans your data, a bit like the pretty function would do. It also means that the color mapping is to that range and is the same in the legend as in the plot. I have left enough space on the right of the plot to fit in the legend, as that was where you said you wanted it in your last email. What par(usr) tells you is the dimensions of the plot in user units. Here it is x=0 at the left, x=351 at the right, y=0 at the bottom and y=200 at the top. Jim [[alternative HTML version deleted]] __ 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. [[alternative HTML version deleted]] __ 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] add a color band
On 11/04/2013 08:09 PM, Alaios wrote: Hi Jim Lemon, thanks for the help, I appreciate this. right now my code looks like. par(mar=c(5,4,4,5)) color2D.matplot(data,1,c(0,1),0,xlab=,ylab=Spans, main=color.scale,xrange=c(-110,-50),border=NA,axes=F) color.legend(357,30,370,100,seq(-110,-50,length.out=13), align=rb,rect.col=color.scale(1:13,1,c(0,1),0), gradient=y) my major problem now is that the a. text in the color bar is squeezed so -50 overlaps with -60 and so on b. for some reason the color bar sometimes (the same code is called for all the data matrices I have) is misaligned in different positions each time Could you please also help me with those two? Hi Alex, For your first question, I would simply extend the color legend vertically: color.legend(357,30,370,150,seq(-110,-50,length.out=13), align=rb,rect.col=color.scale(1:13,1,c(0,1),0), gradient=y) For the second one, you obviously have different dimensions for the data matrices. So, let's step through a method for getting the legend position and size from the plot itself. As I have written a few times previously, par(usr) gives you the dimensions of the plot in user units. For the example above, the dimensions were: x - 0-351 y - 0-200 With a bit of arithmetic, you can work out that the legend positions in the above are: xylim-par(usr) # x position of lower left corner of legend xl-xylim[2]+diff(xylim[1:2])*0.017 # y position of lower left corner yb-xylim[3]+diff(xylim[3:4])*0.15 # x position of upper right corner of legend xr-xylim[2]+diff(xylim[1:2])*0.054 # y position of upper right corner yt-xylim[3]+diff(xylim[3:4])*0.75 Having these lines means that you can get the position and size of the legend about right from the information provided by par(usr) even if you change the number of cells in the matrix passed to color2D.matplot. Then you would call: color.legend(xl,yb,xr,yt,seq(-110,-50,length.out=13), align=rb,rect.col=color.scale(1:13,1,c(0,1),0), gradient=y) unless of course you wanted to change the values in the scale markings. I'll leave that for you to work out. 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.
Re: [R] add a color band
Hi Jim, thanks for the answer! I toyed around as you said and now they look cool and sexy! many thanks! Do you know if there is a way to compress a bit the output? The image type of functions have the useRaster parameter to set to have a compressed output. Right now the produced plots are around 400kBytes which is a lot and I am looking for ways to reduce image size. I would like to thank you for you reply Alex On Monday, November 4, 2013 11:59 AM, Jim Lemon j...@bitwrit.com.au wrote: On 11/04/2013 08:09 PM, Alaios wrote: Hi Jim Lemon, thanks for the help, I appreciate this. right now my code looks like. par(mar=c(5,4,4,5)) color2D.matplot(data,1,c(0,1),0,xlab=,ylab=Spans, main=color.scale,xrange=c(-110,-50),border=NA,axes=F) color.legend(357,30,370,100,seq(-110,-50,length.out=13), align=rb,rect.col=color.scale(1:13,1,c(0,1),0), gradient=y) my major problem now is that the a. text in the color bar is squeezed so -50 overlaps with -60 and so on b. for some reason the color bar sometimes (the same code is called for all the data matrices I have) is misaligned in different positions each time Could you please also help me with those two? Hi Alex, For your first question, I would simply extend the color legend vertically: color.legend(357,30,370,150,seq(-110,-50,length.out=13), align=rb,rect.col=color.scale(1:13,1,c(0,1),0), gradient=y) For the second one, you obviously have different dimensions for the data matrices. So, let's step through a method for getting the legend position and size from the plot itself. As I have written a few times previously, par(usr) gives you the dimensions of the plot in user units. For the example above, the dimensions were: x - 0-351 y - 0-200 With a bit of arithmetic, you can work out that the legend positions in the above are: xylim-par(usr) # x position of lower left corner of legend xl-xylim[2]+diff(xylim[1:2])*0.017 # y position of lower left corner yb-xylim[3]+diff(xylim[3:4])*0.15 # x position of upper right corner of legend xr-xylim[2]+diff(xylim[1:2])*0.054 # y position of upper right corner yt-xylim[3]+diff(xylim[3:4])*0.75 Having these lines means that you can get the position and size of the legend about right from the information provided by par(usr) even if you change the number of cells in the matrix passed to color2D.matplot. Then you would call: color.legend(xl,yb,xr,yt,seq(-110,-50,length.out=13), align=rb,rect.col=color.scale(1:13,1,c(0,1),0), gradient=y) unless of course you wanted to change the values in the scale markings. I'll leave that for you to work out. Jim [[alternative HTML version deleted]] __ 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] add a color band
Hi Jim and thanks for your answer... I might be too tired with my new born or just exhausted. I am sharing for everyone a small data snipset that you can load https://www.dropbox.com/s/fh8jhwujgunmtrb/DataToPlotAsImage.Rdata load(DataToPlotAsImage.Rdata) require(plotrix) browser() test-data # this transforms the values of test into red-yellow color2D.matplot(test,axes=F,xlab=,ylab=,main=color.scale, extremes=c(#FF,#00),show.legend=FALSE) axis(1,at=seq(1,ncol(test),length.out=10),labels=seq(201,300,length.out=10)) color.legend(104,30,112,70,seq(-110,-30,length=11), align=rb,rect.col=color.scale(1:30,1,c(0,1),0),gradient=y) as you can see I have problems where the legend appears. My par(usr returned me par(usr) # [1] 0 351 0 200 but I am not sure how to read that to place the legend at a useful place. second I am not sure why the image is so full with black rows.. What I want is to have the legend visible and later on customize the x axis to write custom string of different size... First I need though to fix the more severe problems as I have described RegardsAlex On Sunday, October 27, 2013 12:25 PM, Jim Lemon j...@bitwrit.com.au wrote: On 10/27/2013 08:39 AM, Alaios wrote: Hi Jim and thanks for your answer... I might be too tired with my new born or just exhausted. I am attaching for everyone a small data snipset that you can load load(DataToPlotAsImage.Rdata) require(plotrix) browser() test-data # this transforms the values of test into red-yellow color2D.matplot(test,axes=F,xlab=,ylab=,main=color.scale, extremes=c(#FF,#00),show.legend=FALSE) axis(1,at=seq(1,ncol(test),length.out=10),labels=seq(201,300,length.out=10)) color.legend(104,30,112,70,seq(-110,-30,length=11), align=rb,rect.col=color.scale(1:30,1,c(0,1),0),gradient=y) as you can see I have problems where the legend appears. My par(usr returned me par(usr) # [1] 0 351 0 200 but I am not sure how to read that to place the legend at a useful place. second I am not sure why the image is so full with black rows.. What I want is to have the legend visible and later on customize the x axis to write custom string of different size... First I need though to fix the more severe problems as I have described Hi Alex, I'm not sure why you have created a copy of data to plot it. I can get quite a sensible plot using this: par(mar=c(5,4,4,5)) color2D.matplot(data,1,c(0,1),0,xlab=,ylab=, main=color.scale,xrange=c(-110,-50),border=NA) color.legend(357,30,370,100,seq(-110,-50,length.out=6), align=rb,rect.col=color.scale(1:6,1,c(0,1),0), gradient=y) Notice several things. First, when you have a large number of cells in a plot like this, setting the border to NA means that you don't get mostly borders (default = black) in the plot. The second thing is that your data range is -107.18150 to -54.07662. In order to get rounded numbers in your legend, I have set the xrange argument to -110 to -50. This gives a neat looking legend that spans your data, a bit like the pretty function would do. It also means that the color mapping is to that range and is the same in the legend as in the plot. I have left enough space on the right of the plot to fit in the legend, as that was where you said you wanted it in your last email. What par(usr) tells you is the dimensions of the plot in user units. Here it is x=0 at the left, x=351 at the right, y=0 at the bottom and y=200 at the top. Jim [[alternative HTML version deleted]] __ 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] add a color band
On 10/27/2013 08:39 AM, Alaios wrote: Hi Jim and thanks for your answer... I might be too tired with my new born or just exhausted. I am attaching for everyone a small data snipset that you can load load(DataToPlotAsImage.Rdata) require(plotrix) browser() test-data # this transforms the values of test into red-yellow color2D.matplot(test,axes=F,xlab=,ylab=,main=color.scale, extremes=c(#FF,#00),show.legend=FALSE) axis(1,at=seq(1,ncol(test),length.out=10),labels=seq(201,300,length.out=10)) color.legend(104,30,112,70,seq(-110,-30,length=11), align=rb,rect.col=color.scale(1:30,1,c(0,1),0),gradient=y) as you can see I have problems where the legend appears. My par(usr returned me par(usr) # [1] 0 351 0 200 but I am not sure how to read that to place the legend at a useful place. second I am not sure why the image is so full with black rows.. What I want is to have the legend visible and later on customize the x axis to write custom string of different size... First I need though to fix the more severe problems as I have described Hi Alex, I'm not sure why you have created a copy of data to plot it. I can get quite a sensible plot using this: par(mar=c(5,4,4,5)) color2D.matplot(data,1,c(0,1),0,xlab=,ylab=, main=color.scale,xrange=c(-110,-50),border=NA) color.legend(357,30,370,100,seq(-110,-50,length.out=6), align=rb,rect.col=color.scale(1:6,1,c(0,1),0), gradient=y) Notice several things. First, when you have a large number of cells in a plot like this, setting the border to NA means that you don't get mostly borders (default = black) in the plot. The second thing is that your data range is -107.18150 to -54.07662. In order to get rounded numbers in your legend, I have set the xrange argument to -110 to -50. This gives a neat looking legend that spans your data, a bit like the pretty function would do. It also means that the color mapping is to that range and is the same in the legend as in the plot. I have left enough space on the right of the plot to fit in the legend, as that was where you said you wanted it in your last email. What par(usr) tells you is the dimensions of the plot in user units. Here it is x=0 at the left, x=351 at the right, y=0 at the bottom and y=200 at the top. 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.
Re: [R] add a color band
Hi Jim and thanks for your answer... I might be too tired with my new born or just exhausted. I am attaching for everyone a small data snipset that you can load load(DataToPlotAsImage.Rdata) require(plotrix) browser() test-data # this transforms the values of test into red-yellow color2D.matplot(test,axes=F,xlab=,ylab=,main=color.scale, extremes=c(#FF,#00),show.legend=FALSE) axis(1,at=seq(1,ncol(test),length.out=10),labels=seq(201,300,length.out=10)) color.legend(104,30,112,70,seq(-110,-30,length=11), align=rb,rect.col=color.scale(1:30,1,c(0,1),0),gradient=y) as you can see I have problems where the legend appears. My par(usr returned me par(usr) # [1] 0 351 0 200 but I am not sure how to read that to place the legend at a useful place. second I am not sure why the image is so full with black rows.. What I want is to have the legend visible and later on customize the x axis to write custom string of different size... First I need though to fix the more severe problems as I have described Regards Alex On Friday, October 25, 2013 11:45 PM, Jim Lemon j...@bitwrit.com.au wrote: On 10/25/2013 11:16 PM, Alaios wrote: Hi Jim and thanks for help I will need some help to make -the legend visible (probably at the right side of the window) -split the legend at 0.1 steps -1,-0.9,-0.8.1 and -assign a color heat.color(30). I have selected orange and reds -show me the way to change the size of legend if it would be needed par(mar=c(7,4,4,6)) test-matrix(data=runif(1),nrow=100) color2D.matplot(test,axes=F,xlab=,ylab=,show.legend=FALSE) #,show.legend=TRUE axis(1,at=seq(1:nrow(test)),labels=seq(201,300)) # color.legend(11,6,11.8,9,seq(-1,1,length=10),rect.col=heat.colors(30),gradient=y) Hi Alex, You can do this in at least two ways. The first example uses the color.scale function to assign colors close to those of heat.colors. The second shows how to use heat.colors if you want that. library(plotrix) par(mar=c(7,4,4,6)) test-matrix(data=runif(1),nrow=100) # this transforms the values of test into red-yellow color2D.matplot(test,axes=F,xlab=,ylab=,main=color.scale, extremes=c(#FF,#00),show.legend=FALSE) axis(1,at=seq(1:nrow(test)),labels=seq(201,300)) # use par(usr) to find out the plot dimensions, # then place the legend where you want it color.legend(104,30,112,70,seq(-1,1,length=11), align=rb,rect.col=color.scale(1:30,1,c(0,1),0),gradient=y) # now try it with heat.colors test.cut-matrix(as.numeric(cut(test,breaks=seq(0,1,length.out=30))),nrow=100) color2D.matplot(test.cut,axes=F,xlab=,ylab=,main=heat.colors, cellcolors=heat.colors(30)[test.cut],show.legend=FALSE) axis(1,at=seq(1:nrow(test)),labels=seq(201,300)) color.legend(104,30,112,70,seq(-1,1,length=11), align=rb,rect.col=heat.colors(30),gradient=y) 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] add a color band
Hi all, I would like to ask your help to add a color band (Πam not sure regarding the right term, this color band at the right of the plot describing values with their corresponding color. For now I have only this code test-matrix(data=runif(1),nrow=100) plot(test,axes=FALSE) axis(1,at=c(0,1),labels=c(a,b)) # but I would like to add at the right side the color legend. How can I do that in R? I would like to thank you in advance for your help R. Alex [[alternative HTML version deleted]] __ 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] add a color band
On 10/25/2013 08:38 PM, Alaios wrote: Hi all, I would like to ask your help to add a color band (Ι am not sure regarding the right term, this color band at the right of the plot describing values with their corresponding color. For now I have only this code test-matrix(data=runif(1),nrow=100) plot(test,axes=FALSE) axis(1,at=c(0,1),labels=c(a,b)) # but I would like to add at the right side the color legend. How can I do that in R? I would like to thank you in advance for your help Hi Alex, If all else fails, look at color.legend (plotrix). 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.
Re: [R] add a color band
Hi Jim and thanks for help I will need some help to make -the legend visible (probably at the right side of the window) -split the legend at 0.1 steps -1,-0.9,-0.8.1 and -assign a color heat.color(30). I have selected orange and reds -show me the way to change the size of legend if it would be needed par(mar=c(7,4,4,6)) test-matrix(data=runif(1),nrow=100) color2D.matplot(test,axes=F,xlab=,ylab=,show.legend=FALSE) #,show.legend=TRUE axis(1,at=seq(1:nrow(test)),labels=seq(201,300)) # color.legend(11,6,11.8,9,seq(-1,1,length=10),rect.col=heat.colors(30),gradient=y) I would like to thank you in advance for your help Regards Alex On Friday, October 25, 2013 11:50 AM, Jim Lemon j...@bitwrit.com.au wrote: On 10/25/2013 08:38 PM, Alaios wrote: Hi all, I would like to ask your help to add a color band (Ã⢠am not sure regarding the right term, this color band at the right of the plot describing values with their corresponding color. For now I have only this code test-matrix(data=runif(1),nrow=100) plot(test,axes=FALSE) axis(1,at=c(0,1),labels=c(a,b)) # butàI would like to add at the rightàside the color legend. How can I do that in R? I would like to thank you in advance for your help Hi Alex, If all else fails, look at color.legend (plotrix). Jim [[alternative HTML version deleted]] __ 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] add a color band
On 10/25/2013 11:16 PM, Alaios wrote: Hi Jim and thanks for help I will need some help to make -the legend visible (probably at the right side of the window) -split the legend at 0.1 steps -1,-0.9,-0.8.1 and -assign a color heat.color(30). I have selected orange and reds -show me the way to change the size of legend if it would be needed par(mar=c(7,4,4,6)) test-matrix(data=runif(1),nrow=100) color2D.matplot(test,axes=F,xlab=,ylab=,show.legend=FALSE) #,show.legend=TRUE axis(1,at=seq(1:nrow(test)),labels=seq(201,300)) # color.legend(11,6,11.8,9,seq(-1,1,length=10),rect.col=heat.colors(30),gradient=y) Hi Alex, You can do this in at least two ways. The first example uses the color.scale function to assign colors close to those of heat.colors. The second shows how to use heat.colors if you want that. library(plotrix) par(mar=c(7,4,4,6)) test-matrix(data=runif(1),nrow=100) # this transforms the values of test into red-yellow color2D.matplot(test,axes=F,xlab=,ylab=,main=color.scale, extremes=c(#FF,#00),show.legend=FALSE) axis(1,at=seq(1:nrow(test)),labels=seq(201,300)) # use par(usr) to find out the plot dimensions, # then place the legend where you want it color.legend(104,30,112,70,seq(-1,1,length=11), align=rb,rect.col=color.scale(1:30,1,c(0,1),0),gradient=y) # now try it with heat.colors test.cut-matrix(as.numeric(cut(test,breaks=seq(0,1,length.out=30))),nrow=100) color2D.matplot(test.cut,axes=F,xlab=,ylab=,main=heat.colors, cellcolors=heat.colors(30)[test.cut],show.legend=FALSE) axis(1,at=seq(1:nrow(test)),labels=seq(201,300)) color.legend(104,30,112,70,seq(-1,1,length=11), align=rb,rect.col=heat.colors(30),gradient=y) 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.