Re: [R] shared axes in multipanel plot
splendid! This worked well, but there are two oddities that I can't resolve. 1. In the real data, the baseline is a cumulative probability plot (from simulations) rather than the straight line. The panel.lines plots this curve, but seems to join the first and last points together. panel.points(x, baseline, type=l) did the same. I checked that the vector is indeed sorted properly, so I'm not sure why it should connect the first point to the last. 2. The screens are correctly labeled, but in the wrong order (left to right, top to bottom: 3,4,1,2). Is this easily corrected? I've been cowardly avoiding learning xyplot() so thanks for the jumpstart! Try this using xyplot.zoo in the zoo package. We define the baseline and a panel function. The panel function just performs the default action to display the graphs and adds the baseline. The screens variable is 1,1,2,2,3,3,4,4. We create a zoo object from dat and use screens to name the columns according to their group. Finally we call xyplot.zoo passing it screens so that the successive columns go in the indicated panels and also passing the other items. See ?xyplot.zoo in zoo and ?xyplot in lattice. library(zoo) library(lattice) baseline - 1:nrow(dat)/nrow(dat) pnl - function(x, ...) { panel.plot.default(x, ...) panel.lines(x, baseline, lwd = 2, col = grey(0.5)) } nc - ncol(dat) screens - rep(1:(nc/2), each = 2) z - zoo(dat) colnames(z) - paste(Group, screens) xyplot(z, screens = screens , layout = c(2, 2), col = black, lty = 2, scales = list(y = list(relation = same)), panel = pnl) On Fri, Dec 11, 2009 at 10:02 AM, Jennifer Young jennifer.yo...@math.mcmaster.ca wrote: Hello I've created a function to make a plot with multiple pannels from columns of data that are created in a previous function. In the example below the number of columns is 8, giving 4 pannels, but in general it takes data with any number of columns and figures out a nice layout. The panels all have the same axes, and so I wonder what functions are avialable to create axes only on the left and bottom of the whole plot rather than each pannel. I'd really like a generic way to do this for any number of plots, but was even having trouble figuring out how to do it manually for this example; How are pannels referred to, in a layout context? That is, how do I say, if(current.pannel==4) {do stuff} Here's a simple version of the code. baseline - (1:20)/20 #example data dat1 - matrix(baseline,20,8) dat - dat1+matrix(rnorm(20*8)/30, 20,8) nstrat - ncol(dat) rows - ceiling(nstrat/4) layout(matrix(1:(rows*2), rows, 2, T)) par(oma=c(4,4,3,1)) par(mar=c(1,1,0,1)) for(i in which(1:nstrat%%2!=0)){ plot(baseline, type=l, col=grey, lwd=2, xlab=, ylab=, ylim=c(0,1), xaxt='n', yaxt='n') axis(1, labels=F); axis(2, labels=F) points(dat[,i], type=l, lty=2) points(dat[,i+1], type=l, lty=2) } Thank you muchly Jennifer Young PS: I am a subscriber, but can't for the life of me figure out how to send an email while logged in so that the moderators don't have to take the time to read it over. I always get the please wait while we check it over email. Likely I'm being dumb. __ 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] shared axes in multipanel plot
On Mon, Dec 14, 2009 at 11:30 AM, Jennifer Young jennifer.yo...@math.mcmaster.ca wrote: splendid! This worked well, but there are two oddities that I can't resolve. 1. In the real data, the baseline is a cumulative probability plot (from simulations) rather than the straight line. The panel.lines plots this curve, but seems to join the first and last points together. panel.points(x, baseline, type=l) did the same. I checked that the vector is indeed sorted properly, so I'm not sure why it should connect the first point to the last. I can't reproduce the problem based on this description. 2. The screens are correctly labeled, but in the wrong order (left to right, top to bottom: 3,4,1,2). Is this easily corrected? xyplot(..., as.table = TRUE) will give one reordering. Another possibility is: plt - xplot(...) plt[ix] where ix is a permutation of 1:4 I've been cowardly avoiding learning xyplot() so thanks for the jumpstart! Try this using xyplot.zoo in the zoo package. We define the baseline and a panel function. The panel function just performs the default action to display the graphs and adds the baseline. The screens variable is 1,1,2,2,3,3,4,4. We create a zoo object from dat and use screens to name the columns according to their group. Finally we call xyplot.zoo passing it screens so that the successive columns go in the indicated panels and also passing the other items. See ?xyplot.zoo in zoo and ?xyplot in lattice. library(zoo) library(lattice) baseline - 1:nrow(dat)/nrow(dat) pnl - function(x, ...) { panel.plot.default(x, ...) panel.lines(x, baseline, lwd = 2, col = grey(0.5)) } nc - ncol(dat) screens - rep(1:(nc/2), each = 2) z - zoo(dat) colnames(z) - paste(Group, screens) xyplot(z, screens = screens , layout = c(2, 2), col = black, lty = 2, scales = list(y = list(relation = same)), panel = pnl) On Fri, Dec 11, 2009 at 10:02 AM, Jennifer Young jennifer.yo...@math.mcmaster.ca wrote: Hello I've created a function to make a plot with multiple pannels from columns of data that are created in a previous function. In the example below the number of columns is 8, giving 4 pannels, but in general it takes data with any number of columns and figures out a nice layout. The panels all have the same axes, and so I wonder what functions are avialable to create axes only on the left and bottom of the whole plot rather than each pannel. I'd really like a generic way to do this for any number of plots, but was even having trouble figuring out how to do it manually for this example; How are pannels referred to, in a layout context? That is, how do I say, if(current.pannel==4) {do stuff} Here's a simple version of the code. baseline - (1:20)/20 #example data dat1 - matrix(baseline,20,8) dat - dat1+matrix(rnorm(20*8)/30, 20,8) nstrat - ncol(dat) rows - ceiling(nstrat/4) layout(matrix(1:(rows*2), rows, 2, T)) par(oma=c(4,4,3,1)) par(mar=c(1,1,0,1)) for(i in which(1:nstrat%%2!=0)){ plot(baseline, type=l, col=grey, lwd=2, xlab=, ylab=, ylim=c(0,1), xaxt='n', yaxt='n') axis(1, labels=F); axis(2, labels=F) points(dat[,i], type=l, lty=2) points(dat[,i+1], type=l, lty=2) } Thank you muchly Jennifer Young PS: I am a subscriber, but can't for the life of me figure out how to send an email while logged in so that the moderators don't have to take the time to read it over. I always get the please wait while we check it over email. Likely I'm being dumb. __ 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] shared axes in multipanel plot
On Mon, Dec 14, 2009 at 11:30 AM, Jennifer Young jennifer.yo...@math.mcmaster.ca wrote: splendid! This worked well, but there are two oddities that I can't resolve. 1. In the real data, the baseline is a cumulative probability plot (from simulations) rather than the straight line. The panel.lines plots this curve, but seems to join the first and last points together. panel.points(x, baseline, type=l) did the same. I checked that the vector is indeed sorted properly, so I'm not sure why it should connect the first point to the last. I can't reproduce the problem based on this description. sorry that was lazy of me. If you modify the code you gave me as follows (with an extra line of the sqare root of baseline, as an example) the first and last points are joined. I didn't notice this before when baseline was just a line. baseline - (1:20)/20 dat1 - matrix(baseline,20,8) dat - dat1+matrix(rnorm(20*8)/30, 20,8) b2-sqrt(baseline) pnl - function(x, ...) { panel.plot.default(x, ...) panel.lines(x, baseline, lwd = 2, col = grey(0.5)) panel.lines(x, b2) } nc - ncol(dat) screens - rep(1:(nc/2), each = 2) z - zoo(dat) colnames(z) - paste(Group, screens) xyplot(z, screens = screens , layout = c(2, 2), col = black, lty = 2, scales = list(y = list(relation = same)), panel = pnl) 2. The screens are correctly labeled, but in the wrong order (left to right, top to bottom: 3,4,1,2). Is this easily corrected? xyplot(..., as.table = TRUE) will give one reordering. Another possibility is: plt - xplot(...) plt[ix] where ix is a permutation of 1:4 as.table=TRUE did the trick thanks. __ 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] shared axes in multipanel plot
Try this: You seem to have found a problem. At any rate try this instead: pnl - function(x, y, ...) { tt - time(z) y - matrix(y, length(tt)) for(j in 1:ncol(y)) panel.plot.default(tt, y[,j], ...) panel.lines(tt, baseline, lwd = 2, col = grey(0.5)) panel.lines(tt, b2) } On Mon, Dec 14, 2009 at 3:19 PM, Jennifer Young jennifer.yo...@math.mcmaster.ca wrote: On Mon, Dec 14, 2009 at 11:30 AM, Jennifer Young jennifer.yo...@math.mcmaster.ca wrote: splendid! This worked well, but there are two oddities that I can't resolve. 1. In the real data, the baseline is a cumulative probability plot (from simulations) rather than the straight line. The panel.lines plots this curve, but seems to join the first and last points together. panel.points(x, baseline, type=l) did the same. I checked that the vector is indeed sorted properly, so I'm not sure why it should connect the first point to the last. I can't reproduce the problem based on this description. sorry that was lazy of me. If you modify the code you gave me as follows (with an extra line of the sqare root of baseline, as an example) the first and last points are joined. I didn't notice this before when baseline was just a line. baseline - (1:20)/20 dat1 - matrix(baseline,20,8) dat - dat1+matrix(rnorm(20*8)/30, 20,8) b2-sqrt(baseline) pnl - function(x, ...) { panel.plot.default(x, ...) panel.lines(x, baseline, lwd = 2, col = grey(0.5)) panel.lines(x, b2) } nc - ncol(dat) screens - rep(1:(nc/2), each = 2) z - zoo(dat) colnames(z) - paste(Group, screens) xyplot(z, screens = screens , layout = c(2, 2), col = black, lty = 2, scales = list(y = list(relation = same)), panel = pnl) 2. The screens are correctly labeled, but in the wrong order (left to right, top to bottom: 3,4,1,2). Is this easily corrected? xyplot(..., as.table = TRUE) will give one reordering. Another possibility is: plt - xplot(...) plt[ix] where ix is a permutation of 1:4 as.table=TRUE did the trick thanks. __ 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] shared axes in multipanel plot
One resolution of the need to go outside of pnl would be to use this line: tt - unique(x) in place of tt - time(z). That would overcome the objection that the pnl function is not self contained. On Mon, Dec 14, 2009 at 3:44 PM, Gabor Grothendieck ggrothendi...@gmail.com wrote: Try this: You seem to have found a problem. At any rate try this instead: pnl - function(x, y, ...) { tt - time(z) y - matrix(y, length(tt)) for(j in 1:ncol(y)) panel.plot.default(tt, y[,j], ...) panel.lines(tt, baseline, lwd = 2, col = grey(0.5)) panel.lines(tt, b2) } On Mon, Dec 14, 2009 at 3:19 PM, Jennifer Young jennifer.yo...@math.mcmaster.ca wrote: On Mon, Dec 14, 2009 at 11:30 AM, Jennifer Young jennifer.yo...@math.mcmaster.ca wrote: splendid! This worked well, but there are two oddities that I can't resolve. 1. In the real data, the baseline is a cumulative probability plot (from simulations) rather than the straight line. The panel.lines plots this curve, but seems to join the first and last points together. panel.points(x, baseline, type=l) did the same. I checked that the vector is indeed sorted properly, so I'm not sure why it should connect the first point to the last. I can't reproduce the problem based on this description. sorry that was lazy of me. If you modify the code you gave me as follows (with an extra line of the sqare root of baseline, as an example) the first and last points are joined. I didn't notice this before when baseline was just a line. baseline - (1:20)/20 dat1 - matrix(baseline,20,8) dat - dat1+matrix(rnorm(20*8)/30, 20,8) b2-sqrt(baseline) pnl - function(x, ...) { panel.plot.default(x, ...) panel.lines(x, baseline, lwd = 2, col = grey(0.5)) panel.lines(x, b2) } nc - ncol(dat) screens - rep(1:(nc/2), each = 2) z - zoo(dat) colnames(z) - paste(Group, screens) xyplot(z, screens = screens , layout = c(2, 2), col = black, lty = 2, scales = list(y = list(relation = same)), panel = pnl) 2. The screens are correctly labeled, but in the wrong order (left to right, top to bottom: 3,4,1,2). Is this easily corrected? xyplot(..., as.table = TRUE) will give one reordering. Another possibility is: plt - xplot(...) plt[ix] where ix is a permutation of 1:4 as.table=TRUE did the trick thanks. __ 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] shared axes in multipanel plot
Ah, I think i see the problem. The default plot recognizes there is one set of x for each set of y, but since there were two vectors in the default.plot, the x vector is repeated and loops around for the lines part. Presumably the default plot uses your recursive plot automatically. At any rate, it seems that this simpler version (with your unique(x) solution) also works and avoids the recursion. pnl - function(x, y, ...) { tt - unique(x) panel.plot.default(x,y, ...) panel.lines(tt, baseline, lwd = 2, col = grey(0.5)) } Thanks for your time!! One resolution of the need to go outside of pnl would be to use this line: tt - unique(x) in place of tt - time(z). That would overcome the objection that the pnl function is not self contained. On Mon, Dec 14, 2009 at 3:44 PM, Gabor Grothendieck ggrothendi...@gmail.com wrote: Try this: You seem to have found a problem. At any rate try this instead: pnl - function(x, y, ...) { tt - time(z) y - matrix(y, length(tt)) for(j in 1:ncol(y)) panel.plot.default(tt, y[,j], ...) panel.lines(tt, baseline, lwd = 2, col = grey(0.5)) panel.lines(tt, b2) } On Mon, Dec 14, 2009 at 3:19 PM, Jennifer Young jennifer.yo...@math.mcmaster.ca wrote: On Mon, Dec 14, 2009 at 11:30 AM, Jennifer Young jennifer.yo...@math.mcmaster.ca wrote: splendid! This worked well, but there are two oddities that I can't resolve. 1. In the real data, the baseline is a cumulative probability plot (from simulations) rather than the straight line. The panel.lines plots this curve, but seems to join the first and last points together. panel.points(x, baseline, type=l) did the same. I checked that the vector is indeed sorted properly, so I'm not sure why it should connect the first point to the last. I can't reproduce the problem based on this description. sorry that was lazy of me. If you modify the code you gave me as follows (with an extra line of the sqare root of baseline, as an example) the first and last points are joined. I didn't notice this before when baseline was just a line. baseline - (1:20)/20 dat1 - matrix(baseline,20,8) dat - dat1+matrix(rnorm(20*8)/30, 20,8) b2-sqrt(baseline) pnl - function(x, ...) { panel.plot.default(x, ...) panel.lines(x, baseline, lwd = 2, col = grey(0.5)) panel.lines(x, b2) } nc - ncol(dat) screens - rep(1:(nc/2), each = 2) z - zoo(dat) colnames(z) - paste(Group, screens) xyplot(z, screens = screens , layout = c(2, 2), col = black, lty = 2, scales = list(y = list(relation = same)), panel = pnl) 2. The screens are correctly labeled, but in the wrong order (left to right, top to bottom: 3,4,1,2). Is this easily corrected? xyplot(..., as.table = TRUE) will give one reordering. Another possibility is: plt - xplot(...) plt[ix] where ix is a permutation of 1:4 as.table=TRUE did the trick thanks. __ 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] shared axes in multipanel plot
I think the basic problem is that you are bringing in new data from outside the xyplot() call -- i.e `b2` and `baseline` -- and are then trying to plot that against the panel argument `x`. This is asking for trouble, as you have found, because `x` as passed to the panel function is modified by grouping, conditioning or subsetting operations. If you are using outside data, it is best to define both the x and y values together. Alternatively, merge all the data into one object (a matrix in this case) and define the plot structure in the high-level plot call, so that all the data series are grouped or subsetted together. Hope that helps. -Felix 2009/12/15 Gabor Grothendieck ggrothendi...@gmail.com: One resolution of the need to go outside of pnl would be to use this line: tt - unique(x) in place of tt - time(z). That would overcome the objection that the pnl function is not self contained. On Mon, Dec 14, 2009 at 3:44 PM, Gabor Grothendieck ggrothendi...@gmail.com wrote: Try this: You seem to have found a problem. At any rate try this instead: pnl - function(x, y, ...) { tt - time(z) y - matrix(y, length(tt)) for(j in 1:ncol(y)) panel.plot.default(tt, y[,j], ...) panel.lines(tt, baseline, lwd = 2, col = grey(0.5)) panel.lines(tt, b2) } On Mon, Dec 14, 2009 at 3:19 PM, Jennifer Young jennifer.yo...@math.mcmaster.ca wrote: On Mon, Dec 14, 2009 at 11:30 AM, Jennifer Young jennifer.yo...@math.mcmaster.ca wrote: splendid! This worked well, but there are two oddities that I can't resolve. 1. In the real data, the baseline is a cumulative probability plot (from simulations) rather than the straight line. The panel.lines plots this curve, but seems to join the first and last points together. panel.points(x, baseline, type=l) did the same. I checked that the vector is indeed sorted properly, so I'm not sure why it should connect the first point to the last. I can't reproduce the problem based on this description. sorry that was lazy of me. If you modify the code you gave me as follows (with an extra line of the sqare root of baseline, as an example) the first and last points are joined. I didn't notice this before when baseline was just a line. baseline - (1:20)/20 dat1 - matrix(baseline,20,8) dat - dat1+matrix(rnorm(20*8)/30, 20,8) b2-sqrt(baseline) pnl - function(x, ...) { panel.plot.default(x, ...) panel.lines(x, baseline, lwd = 2, col = grey(0.5)) panel.lines(x, b2) } nc - ncol(dat) screens - rep(1:(nc/2), each = 2) z - zoo(dat) colnames(z) - paste(Group, screens) xyplot(z, screens = screens , layout = c(2, 2), col = black, lty = 2, scales = list(y = list(relation = same)), panel = pnl) 2. The screens are correctly labeled, but in the wrong order (left to right, top to bottom: 3,4,1,2). Is this easily corrected? xyplot(..., as.table = TRUE) will give one reordering. Another possibility is: plt - xplot(...) plt[ix] where ix is a permutation of 1:4 as.table=TRUE did the trick thanks. __ 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. -- Felix Andrews / 安福立 Postdoctoral Fellow Integrated Catchment Assessment and Management (iCAM) Centre Fenner School of Environment and Society [Bldg 48a] The Australian National University Canberra ACT 0200 Australia M: +61 410 400 963 T: + 61 2 6125 4670 E: felix.andr...@anu.edu.au CRICOS Provider No. 00120C -- http://www.neurofractal.org/felix/ __ 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] shared axes in multipanel plot
Good observation. In fact, panel.plot.default works because it uses the subscripts and groups variables which are passed down to the panel function and if not explicitly referenced in the formal argument list of pnl are passed via ... thus we could use any of these equivalent pnl functions though the prior solution using unique seems the shortest: # using groups pick out x components corresponding to group 1 pnl - function(x, ..., groups) { panel.plot.default(x, ..., groups) ix - which(groups == 1) panel.lines(x[ix], b2, lwd = 2, col = grey(0.5)) } # using groups and subscripts pnl - function(x, y, subscripts, ..., groups) { panel.plot.default(x, y, subscripts, ..., groups) tt - x[groups[subscripts] == 1] panel.lines(tt, b2, lwd = 2, col = grey(0.5)) } # trim x to length of b2 pnl - function(x, ...) { panel.plot.default(x, ...) tt - head(x, length(b2)) panel.lines(tt, b2, lwd = 2, col = grey(0.5)) } # unique pnl - function(x, ...) { panel.plot.default(x, ...) tt - unique(x) panel.lines(tt, b2, lwd = 2, col = grey(0.5)) } baseline - (1:20)/20 dat1 - matrix(baseline,20,8) dat - dat1+matrix(rnorm(20*8)/30, 20,8) b2-sqrt(baseline) nc - ncol(dat) screens - rep(1:(nc/2), each = 2) z - zoo(dat) colnames(z) - paste(Group, screens) xyplot(z, screens = screens , layout = c(2, 2), col = black, lty = 2, scales = list(y = list(relation = same)), panel = pnl) On Mon, Dec 14, 2009 at 5:21 PM, Jennifer Young jennifer.yo...@math.mcmaster.ca wrote: Ah, I think i see the problem. The default plot recognizes there is one set of x for each set of y, but since there were two vectors in the default.plot, the x vector is repeated and loops around for the lines part. Presumably the default plot uses your recursive plot automatically. At any rate, it seems that this simpler version (with your unique(x) solution) also works and avoids the recursion. pnl - function(x, y, ...) { tt - unique(x) panel.plot.default(x,y, ...) panel.lines(tt, baseline, lwd = 2, col = grey(0.5)) } Thanks for your time!! One resolution of the need to go outside of pnl would be to use this line: tt - unique(x) in place of tt - time(z). That would overcome the objection that the pnl function is not self contained. On Mon, Dec 14, 2009 at 3:44 PM, Gabor Grothendieck ggrothendi...@gmail.com wrote: Try this: You seem to have found a problem. At any rate try this instead: pnl - function(x, y, ...) { tt - time(z) y - matrix(y, length(tt)) for(j in 1:ncol(y)) panel.plot.default(tt, y[,j], ...) panel.lines(tt, baseline, lwd = 2, col = grey(0.5)) panel.lines(tt, b2) } On Mon, Dec 14, 2009 at 3:19 PM, Jennifer Young jennifer.yo...@math.mcmaster.ca wrote: On Mon, Dec 14, 2009 at 11:30 AM, Jennifer Young jennifer.yo...@math.mcmaster.ca wrote: splendid! This worked well, but there are two oddities that I can't resolve. 1. In the real data, the baseline is a cumulative probability plot (from simulations) rather than the straight line. The panel.lines plots this curve, but seems to join the first and last points together. panel.points(x, baseline, type=l) did the same. I checked that the vector is indeed sorted properly, so I'm not sure why it should connect the first point to the last. I can't reproduce the problem based on this description. sorry that was lazy of me. If you modify the code you gave me as follows (with an extra line of the sqare root of baseline, as an example) the first and last points are joined. I didn't notice this before when baseline was just a line. baseline - (1:20)/20 dat1 - matrix(baseline,20,8) dat - dat1+matrix(rnorm(20*8)/30, 20,8) b2-sqrt(baseline) pnl - function(x, ...) { panel.plot.default(x, ...) panel.lines(x, baseline, lwd = 2, col = grey(0.5)) panel.lines(x, b2) } nc - ncol(dat) screens - rep(1:(nc/2), each = 2) z - zoo(dat) colnames(z) - paste(Group, screens) xyplot(z, screens = screens , layout = c(2, 2), col = black, lty = 2, scales = list(y = list(relation = same)), panel = pnl) 2. The screens are correctly labeled, but in the wrong order (left to right, top to bottom: 3,4,1,2). Is this easily corrected? xyplot(..., as.table = TRUE) will give one reordering. Another possibility is: plt - xplot(...) plt[ix] where ix is a permutation of 1:4 as.table=TRUE did the trick thanks. __ 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] shared axes in multipanel plot
Hello I've created a function to make a plot with multiple pannels from columns of data that are created in a previous function. In the example below the number of columns is 8, giving 4 pannels, but in general it takes data with any number of columns and figures out a nice layout. The panels all have the same axes, and so I wonder what functions are avialable to create axes only on the left and bottom of the whole plot rather than each pannel. I'd really like a generic way to do this for any number of plots, but was even having trouble figuring out how to do it manually for this example; How are pannels referred to, in a layout context? That is, how do I say, if(current.pannel==4) {do stuff} Here's a simple version of the code. baseline - (1:20)/20#example data dat1 - matrix(baseline,20,8) dat - dat1+matrix(rnorm(20*8)/30, 20,8) nstrat - ncol(dat) rows - ceiling(nstrat/4) layout(matrix(1:(rows*2), rows, 2, T)) par(oma=c(4,4,3,1)) par(mar=c(1,1,0,1)) for(i in which(1:nstrat%%2!=0)){ plot(baseline, type=l, col=grey, lwd=2, xlab=, ylab=, ylim=c(0,1), xaxt='n', yaxt='n') axis(1, labels=F); axis(2, labels=F) points(dat[,i], type=l, lty=2) points(dat[,i+1], type=l, lty=2) } Thank you muchly Jennifer Young PS: I am a subscriber, but can't for the life of me figure out how to send an email while logged in so that the moderators don't have to take the time to read it over. I always get the please wait while we check it over email. Likely I'm being dumb. __ 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] shared axes in multipanel plot
Try this using xyplot.zoo in the zoo package. We define the baseline and a panel function. The panel function just performs the default action to display the graphs and adds the baseline. The screens variable is 1,1,2,2,3,3,4,4. We create a zoo object from dat and use screens to name the columns according to their group. Finally we call xyplot.zoo passing it screens so that the successive columns go in the indicated panels and also passing the other items. See ?xyplot.zoo in zoo and ?xyplot in lattice. library(zoo) library(lattice) baseline - 1:nrow(dat)/nrow(dat) pnl - function(x, ...) { panel.plot.default(x, ...) panel.lines(x, baseline, lwd = 2, col = grey(0.5)) } nc - ncol(dat) screens - rep(1:(nc/2), each = 2) z - zoo(dat) colnames(z) - paste(Group, screens) xyplot(z, screens = screens , layout = c(2, 2), col = black, lty = 2, scales = list(y = list(relation = same)), panel = pnl) On Fri, Dec 11, 2009 at 10:02 AM, Jennifer Young jennifer.yo...@math.mcmaster.ca wrote: Hello I've created a function to make a plot with multiple pannels from columns of data that are created in a previous function. In the example below the number of columns is 8, giving 4 pannels, but in general it takes data with any number of columns and figures out a nice layout. The panels all have the same axes, and so I wonder what functions are avialable to create axes only on the left and bottom of the whole plot rather than each pannel. I'd really like a generic way to do this for any number of plots, but was even having trouble figuring out how to do it manually for this example; How are pannels referred to, in a layout context? That is, how do I say, if(current.pannel==4) {do stuff} Here's a simple version of the code. baseline - (1:20)/20 #example data dat1 - matrix(baseline,20,8) dat - dat1+matrix(rnorm(20*8)/30, 20,8) nstrat - ncol(dat) rows - ceiling(nstrat/4) layout(matrix(1:(rows*2), rows, 2, T)) par(oma=c(4,4,3,1)) par(mar=c(1,1,0,1)) for(i in which(1:nstrat%%2!=0)){ plot(baseline, type=l, col=grey, lwd=2, xlab=, ylab=, ylim=c(0,1), xaxt='n', yaxt='n') axis(1, labels=F); axis(2, labels=F) points(dat[,i], type=l, lty=2) points(dat[,i+1], type=l, lty=2) } Thank you muchly Jennifer Young PS: I am a subscriber, but can't for the life of me figure out how to send an email while logged in so that the moderators don't have to take the time to read it over. I always get the please wait while we check it over email. Likely I'm being dumb. __ 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.