Camarda, Carlo Giovanni
2005-Jun-14 18:00 UTC
[R] Plotting rows (or columns) from a matrix in different graphs, not using "par"
Dear R-users, I would like to ask whether it's possible (for sure it would be), to plot each rows (or columns) in different graphs and in the same figure region without using the function "par" and then struggling around with "axes" and labels etc. Luckily, I would always have "rows + columns = even number" and the same "ylim". The next one could be a sort of example on what I would like to avoid plotting the rows of the matrix "mat": ########### EXAMPLE ###################### dat <- sort(runif(16, 1, 1000)) mat <- matrix(dat, ncol=4, byrow = T) y <- seq(1950, 1953) par(mfrow=c(2,2)) plot(y, mat[1,], ylim=c(1,1000), axes=F, xlab="", ylab="simul.") box() axis(side=2, tick=T, labels=T) axis(side=3, tick=T, labels=T) plot(y, mat[2,], ylim=c(1,1000), axes=F, xlab="", ylab="") box() axis(side=3, tick=T, labels=T) plot(y, mat[3,], ylim=c(1,1000), axes=F, xlab="years", ylab="simul.") box() axis(side=1, tick=T, labels=T) axis(side=2, tick=T, labels=T) plot(y, mat[4,], ylim=c(1,1000), axes=F, xlab="years", ylab="") box() axis(side=1, tick=T, labels=T) ########### END EXAMPLE ######################## Naturally something more compact would be even nicer. Thanks in advance, Carlo Giovanni Camarda +++++ This mail has been sent through the MPI for Demographic Rese...{{dropped}}
Marc Schwartz
2005-Jun-14 19:53 UTC
[R] Plotting rows (or columns) from a matrix in different graphs, not using "par"
On Tue, 2005-06-14 at 20:00 +0200, Camarda, Carlo Giovanni wrote:> Dear R-users, > I would like to ask whether it's possible (for sure it would be), to > plot each rows (or columns) in different graphs and in the same figure > region without using the function "par" and then struggling around with > "axes" and labels etc. > Luckily, I would always have "rows + columns = even number" and the same > "ylim". > > The next one could be a sort of example on what I would like to avoid > plotting the rows of the matrix "mat": > > ########### EXAMPLE ###################### > dat <- sort(runif(16, 1, 1000)) > mat <- matrix(dat, ncol=4, byrow = T) > y <- seq(1950, 1953) > par(mfrow=c(2,2)) > plot(y, mat[1,], ylim=c(1,1000), axes=F, xlab="", ylab="simul.") > box() > axis(side=2, tick=T, labels=T) > axis(side=3, tick=T, labels=T) > plot(y, mat[2,], ylim=c(1,1000), axes=F, xlab="", ylab="") > box() > axis(side=3, tick=T, labels=T) > plot(y, mat[3,], ylim=c(1,1000), axes=F, xlab="years", ylab="simul.") > box() > axis(side=1, tick=T, labels=T) > axis(side=2, tick=T, labels=T) > plot(y, mat[4,], ylim=c(1,1000), axes=F, xlab="years", ylab="") > box() > axis(side=1, tick=T, labels=T) > ########### END EXAMPLE ######################## > > Naturally something more compact would be even nicer. > > Thanks in advance, > Carlo Giovanni CamardaIf I am understanding what you are trying to do, then using xyplot() from the lattice package (part of the standard R installation) may be best: Modify 'mat' so that all data is in 'df':> df <- data.frame("Simul" = as.vector(mat),"Years" = rep(1950:1953, 4), "Group" = rep(LETTERS[1:4], each = 4))> dfSimul Years Group 1 88.67956 1950 A 2 364.73579 1951 A 3 546.63525 1952 A 4 774.05869 1953 A 5 138.09586 1950 B 6 382.20986 1951 B 7 592.85512 1952 B 8 810.45569 1953 B 9 232.43912 1950 C 10 524.91085 1951 C 11 598.49688 1952 C 12 963.97328 1953 C 13 261.80598 1950 D 14 533.91143 1951 D 15 765.72522 1952 D 16 996.72192 1953 D # Load the lattice package> library(lattice)# Draw the plot, note the use of standard R formulae in the syntax # with the addition of the grouping vector after the '|'> xyplot(Simul ~ Years | Group, data = df)See: library(lattice) ?xyplot for more information. HTH, Marc Schwartz
Gabor Grothendieck
2005-Jun-14 22:04 UTC
[R] Plotting rows (or columns) from a matrix in different graphs, not using "par"
On 6/14/05, Camarda, Carlo Giovanni <Camarda at demogr.mpg.de> wrote:> Dear R-users, > I would like to ask whether it's possible (for sure it would be), to > plot each rows (or columns) in different graphs and in the same figure > region without using the function "par" and then struggling around with > "axes" and labels etc. > Luckily, I would always have "rows + columns = even number" and the same > "ylim". > > The next one could be a sort of example on what I would like to avoid > plotting the rows of the matrix "mat": > > ########### EXAMPLE ###################### > dat <- sort(runif(16, 1, 1000)) > mat <- matrix(dat, ncol=4, byrow = T) > y <- seq(1950, 1953) > par(mfrow=c(2,2)) > plot(y, mat[1,], ylim=c(1,1000), axes=F, xlab="", ylab="simul.") > box() > axis(side=2, tick=T, labels=T) > axis(side=3, tick=T, labels=T) > plot(y, mat[2,], ylim=c(1,1000), axes=F, xlab="", ylab="") > box() > axis(side=3, tick=T, labels=T) > plot(y, mat[3,], ylim=c(1,1000), axes=F, xlab="years", ylab="simul.") > box() > axis(side=1, tick=T, labels=T) > axis(side=2, tick=T, labels=T) > plot(y, mat[4,], ylim=c(1,1000), axes=F, xlab="years", ylab="") > box() > axis(side=1, tick=T, labels=T) > ########### END EXAMPLE ######################## > > Naturally something more compact would be even nicer. >plot(ts(mat, start = 1950), nc = 2)
Maybe Matching Threads
- impossible to invert a spam-object, but possible when it's a matrix-object
- subset handling
- How can I extract part of the data in a panel dataset?
- interpolation
- using "eval(parse(text)) " , gsub(pattern, replacement, x) , to process "code" within a loop/custom function