Re: [R] Multi-line plots with matrices in R
Hi see matplot, matlines. or use forbidden for cycle. for (i in 1:n) lines(x,y[,i], col=i) or if you want to use more colours use built in rainbow, topo.colors or generate your own set. Regards Petr On 7 Mar 2007 at 12:30, Joseph Wakeling wrote: Date sent: Wed, 07 Mar 2007 12:30:46 + From: Joseph Wakeling [EMAIL PROTECTED] To: r-help@stat.math.ethz.ch Subject:[R] Multi-line plots with matrices in R Hello all, I'm a new user of R, experienced with Octave/MATLAB and therefore struggling a bit with the new syntax. One of the easy things in Octave or MATLAB is to plot multiple lines or sets of points by using a matrix where either the columns or the rows contain the y-values to be plotted. Both packages automatically give each line/points their own unique colour, character etc. I'm wondering how I get the same functionality in R. For example, if X is a vector of x-values and Y is a matrix whose rows contain the y-values, I can do, apply(Y,1,lines,x=X) ... but of course everything is all in black, with the same type of line or points. I'd like each line to have its own unique colour and/or style. Another thing I'd like clarification on is the ability to update an existing plot. For example if I do, plot.window(xlim=c(0,100),ylim=c(0,1)) and then after plotting data decide I want ylim=c(0,0.5), how do I update the graphic? A new plot.window() command does nothing. Many thanks, -- Joe __ R-help@stat.math.ethz.ch 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. Petr Pikal [EMAIL PROTECTED] __ R-help@stat.math.ethz.ch 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] Multi-line plots with matrices in R
On Wed, 2007-03-07 at 12:30 +, Joseph Wakeling wrote: Hello all, I'm a new user of R, experienced with Octave/MATLAB and therefore struggling a bit with the new syntax. One of the easy things in Octave or MATLAB is to plot multiple lines or sets of points by using a matrix where either the columns or the rows contain the y-values to be plotted. Both packages automatically give each line/points their own unique colour, character etc. I'm wondering how I get the same functionality in R. For example, if X is a vector of x-values and Y is a matrix whose rows contain the y-values, I can do, apply(Y,1,lines,x=X) You want maplot here. See ?matplot but here is an example: ## generate some data to use, a matrix of Y values ## and a vector of x indices. mat - matrix(runif(100), ncol = 5) vec - seq(1, 100, length = 20) ## plot it using matplot matplot(vec, mat, type = l) # type = l to get lines There is also matlines() and matpoints() for adding lines and points to existing plots. ... but of course everything is all in black, with the same type of line or points. I'd like each line to have its own unique colour and/or style. Another thing I'd like clarification on is the ability to update an existing plot. For example if I do, plot.window(xlim=c(0,100),ylim=c(0,1)) Standard graphics in R are not modifiable after being plotted. You need to re-plot. When plotting data, I rarely need plot.window. This is what I would do: x - 1:100 * runif(100) y - seq(0,1, length = 100) * runif(100) plot(x, y, xlim = c(0, 100), ylim = c(0, 1)) # now change the limits plot(x, y, xlim = c(0, 100), ylim = c(0, 0.5)) and then after plotting data decide I want ylim=c(0,0.5), how do I update the graphic? A new plot.window() command does nothing. But it does: opar - par(mfrow = c(1,2)) plot(x, y, xlim = c(0, 100), ylim = c(0, 0.5)) plot(x, y, xlim = c(0, 100), ylim = c(0, 1)) plot.window(xlim = c(0, 100), ylim = c(0, 0.5)) points(x, y, col = red) par(opar) The points on the left plot correspond exactly to the points in red on the right plot. The axis limits have changed, but because the axes have already been labelled, these are not updated. We can illustrate this by adding axes to the top and right of that plot opar - par(mfrow = c(1,2), mar = c(5,4,4,4) + 0.1) plot(x, y, xlim = c(0, 100), ylim = c(0, 0.5)) plot(x, y, xlim = c(0, 100), ylim = c(0, 1)) plot.window(xlim = c(0, 100), ylim = c(0, 0.5)) points(x, y, col = red) axis(3) axis(4) par(opar) Note the changed axis range in the right-hand margin. The problem is that you can't use plot.window to achieve what you want, not that plot.window doesn't do anything. Many thanks, -- Joe HTH G -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% __ R-help@stat.math.ethz.ch 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] Multi-line plots with matrices in R
Gavin Simpson wrote: You want maplot here. See ?matplot but here is an example: Great! Thanks to you and Petr for pointing this out, it's exactly what I wanted. Petr's other suggestions look interesting and I'll explore them at length later. Note the changed axis range in the right-hand margin. The problem is that you can't use plot.window to achieve what you want, not that plot.window doesn't do anything. Ahhh, I see. So, it does not affect what has already been plotted, but affects how new material is inserted into the plot area. Entering plot.window(xlim=c(0,100),ylim=c(0,0.5)) axis(1) axis(2) plot.window(xlim=c(0,100),ylim=c(0,1)) axis(2) ... is instructive. :-) So, _is_ there a command which will rearrange the existing plotted items, including axes? Or does R require that I have a good idea of the space in which I want to plot from the start? Oh, and a quick cosmetic query---I notice that the axes when created are spaced apart somewhat so the axis lines do not meet at the plot origin. Is there a way to alter this so that the outline of the box, and the extreme values of the axis, match up? Thanks again, -- Joe __ R-help@stat.math.ethz.ch 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] Multi-line plots with matrices in R
On Wed, 2007-03-07 at 15:11 +, Joseph Wakeling wrote: Gavin Simpson wrote: You want maplot here. See ?matplot but here is an example: Great! Thanks to you and Petr for pointing this out, it's exactly what I wanted. Petr's other suggestions look interesting and I'll explore them at length later. Note the changed axis range in the right-hand margin. The problem is that you can't use plot.window to achieve what you want, not that plot.window doesn't do anything. Ahhh, I see. So, it does not affect what has already been plotted, but affects how new material is inserted into the plot area. Entering plot.window(xlim=c(0,100),ylim=c(0,0.5)) axis(1) axis(2) plot.window(xlim=c(0,100),ylim=c(0,1)) axis(2) ... is instructive. :-) So, _is_ there a command which will rearrange the existing plotted items, including axes? Or does R require that I have a good idea of the space in which I want to plot from the start? Not with the standard R graphics - think of the graphics window as a piece of paper and if you draw anything on it you have done so in permanent ink. If something needs changing you need a new sheet of paper and have to redraw the lot. Most people I know write their code in some text editor and send (or copy paste) it into R. It is an easy matter to edit one or two bits of your code to tweak the display and re-plot... I think you can modify lattice graphics objects and just plot (print really) them again - but again you are really redrawing the whole plot from scratch. IIRC grid might be able to do some of what you are looking for. Oh, and a quick cosmetic query---I notice that the axes when created are spaced apart somewhat so the axis lines do not meet at the plot origin. Is there a way to alter this so that the outline of the box, and the extreme values of the axis, match up? Look at ?par and xaxs and yaxs. E.g. plot(1:10, xaxs = i, yaxs = i) G Thanks again, -- Joe -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% __ R-help@stat.math.ethz.ch 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.