Dear R users, I have a data generated as the following, dat <- data.frame(matrix(sample(24), nrow=4)) dimnames(dat) <-list(rownames=c('g1','g2','g3','g4'), colnames=c("A_CH1","A_CH2","B_CH1","B_CH2","C_CH3","C_CH3")) » dat A_CH1 A_CH2 B_CH1 B_CH2 C_CH3 C_CH3 g1 16 24 7 9 14 20 g2 4 10 19 22 5 17 g3 11 18 21 12 13 1 g4 2 3 15 6 23 8 I am trying to plot them pairwise by column(might extend to more than 3 pairs) Instead manually plotting as below, could you please point me out with easier ways? par(mfrow=c(3,1)) plot(dat$A_CH1+dat$A_CH2, dat$A_CH1-dat$A_CH2) plot(dat$B_CH1+dat$B_CH2, dat$B_CH1-dat$B_CH2) plot(dat[,5]+dat[,6], dat[,5]-dat[,6]) Thanks a lot for your help __________________________________________________ [[alternative HTML version deleted]]
Something like this should do it: invisible(lapply(seq(1, length(dat), by=2), function(i) plot(dat[[i]]+dat[[i+1]], dat[[i]] - dat[[i+1]]))) Andy> From: Hai Lin > > Dear R users, > > I have a data generated as the following, > > dat <- data.frame(matrix(sample(24), nrow=4)) > dimnames(dat) <-list(rownames=c('g1','g2','g3','g4'), > colnames=c("A_CH1","A_CH2","B_CH1","B_CH2","C_CH3","C_CH3")) > > > dat > A_CH1 A_CH2 B_CH1 B_CH2 C_CH3 C_CH3 > g1 16 24 7 9 14 20 > g2 4 10 19 22 5 17 > g3 11 18 21 12 13 1 > g4 2 3 15 6 23 8 > > I am trying to plot them pairwise by column(might extend to > more than 3 pairs) > Instead manually plotting as below, could you please point me > out with easier ways? > > par(mfrow=c(3,1)) > plot(dat$A_CH1+dat$A_CH2, dat$A_CH1-dat$A_CH2) > plot(dat$B_CH1+dat$B_CH2, dat$B_CH1-dat$B_CH2) > plot(dat[,5]+dat[,6], dat[,5]-dat[,6]) > > Thanks a lot for your help > > __________________________________________________ > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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 > > >
On Tue, 29 Mar 2005 06:58:51 -0800 (PST), Hai Lin <kevinvol2002 at yahoo.com> wrote :>Dear R users, > >I have a data generated as the following, > >dat <- data.frame(matrix(sample(24), nrow=4)) >dimnames(dat) <-list(rownames=c('g1','g2','g3','g4'), colnames=c("A_CH1","A_CH2","B_CH1","B_CH2","C_CH3","C_CH3")) > >? dat > A_CH1 A_CH2 B_CH1 B_CH2 C_CH3 C_CH3 >g1 16 24 7 9 14 20 >g2 4 10 19 22 5 17 >g3 11 18 21 12 13 1 >g4 2 3 15 6 23 8 > >I am trying to plot them pairwise by column(might extend to more than 3 pairs) >Instead manually plotting as below, could you please point me out with easier ways? > >par(mfrow=c(3,1)) >plot(dat$A_CH1+dat$A_CH2, dat$A_CH1-dat$A_CH2) >plot(dat$B_CH1+dat$B_CH2, dat$B_CH1-dat$B_CH2) >plot(dat[,5]+dat[,6], dat[,5]-dat[,6])Your last line gives the hint: for (i in 1:3) { c1 <- dat[,2*i - 1] c2 <- dat[,2*i] n1 <- colnames(dat)[2*i - 1] n2 <- colnames(dat)[2*i] plot(c1+c2, c1-c2, main=paste("Pair", i,":",n1, n2)) } Duncan Murdoch
Hallo On 29 Mar 2005 at 6:58, Hai Lin wrote:> Dear R users, > > I have a data generated as the following, > > dat <- data.frame(matrix(sample(24), nrow=4)) > dimnames(dat) <-list(rownames=c('g1','g2','g3','g4'), > colnames=c("A_CH1","A_CH2","B_CH1","B_CH2","C_CH3","C_CH3")) > > ? dat > A_CH1 A_CH2 B_CH1 B_CH2 C_CH3 C_CH3 > g1 16 24 7 9 14 20 > g2 4 10 19 22 5 17 > g3 11 18 21 12 13 1 > g4 2 3 15 6 23 8 > > I am trying to plot them pairwise by column(might extend to more than > 3 pairs) Instead manually plotting as below, could you please point me > out with easier ways? > > par(mfrow=c(3,1))You can use for construction for(i in c(1,3,5)) plot(dat[,i]+dat[,i+1], dat[,i]-dat[,i+1]) but if you want x or y labels formated differently, you probably need to ad some kind of expression in xlab (ylab) parameters. Cheers Petr> plot(dat$A_CH1+dat$A_CH2, dat$A_CH1-dat$A_CH2) > plot(dat$B_CH1+dat$B_CH2, dat$B_CH1-dat$B_CH2) > plot(dat[,5]+dat[,6], dat[,5]-dat[,6]) > > Thanks a lot for your help > > __________________________________________________ > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.htmlPetr Pikal petr.pikal at precheza.cz
you could try something like this: dat <- array(sample(24), dim=c(4,2,3)) par(mfrow=c(3,1)) apply(dat, 3, function(x) plot(rowSums(x), x[,2]-x[,1])) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/336899 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Hai Lin" <kevinvol2002 at yahoo.com> To: <r-help at stat.math.ethz.ch> Sent: Tuesday, March 29, 2005 4:58 PM Subject: [R] pairewise plots Dear R users, I have a data generated as the following, dat <- data.frame(matrix(sample(24), nrow=4)) dimnames(dat) <-list(rownames=c('g1','g2','g3','g4'), colnames=c("A_CH1","A_CH2","B_CH1","B_CH2","C_CH3","C_CH3")) ? dat A_CH1 A_CH2 B_CH1 B_CH2 C_CH3 C_CH3 g1 16 24 7 9 14 20 g2 4 10 19 22 5 17 g3 11 18 21 12 13 1 g4 2 3 15 6 23 8 I am trying to plot them pairwise by column(might extend to more than 3 pairs) Instead manually plotting as below, could you please point me out with easier ways? par(mfrow=c(3,1)) plot(dat$A_CH1+dat$A_CH2, dat$A_CH1-dat$A_CH2) plot(dat$B_CH1+dat$B_CH2, dat$B_CH1-dat$B_CH2) plot(dat[,5]+dat[,6], dat[,5]-dat[,6]) Thanks a lot for your help __________________________________________________ [[alternative HTML version deleted]] ______________________________________________ R-help at 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
On Tuesday 29 March 2005 08:58, Hai Lin wrote:> Dear R users, > > I have a data generated as the following, > > dat <- data.frame(matrix(sample(24), nrow=4)) > dimnames(dat) <-list(rownames=c('g1','g2','g3','g4'), > colnames=c("A_CH1","A_CH2","B_CH1","B_CH2","C_CH3","C_CH3")) > > » dat > A_CH1 A_CH2 B_CH1 B_CH2 C_CH3 C_CH3 > g1 16 24 7 9 14 20 > g2 4 10 19 22 5 17 > g3 11 18 21 12 13 1 > g4 2 3 15 6 23 8Why would you want two columns with the same name?> I am trying to plot them pairwise by column(might extend to more than > 3 pairs) Instead manually plotting as below, could you please point > me out with easier ways?If your data frame can be easily restructured (using reshape perhaps), one possible solution would be to use the lattice package: require(lattice) tmd(xyplot(c(A_CH1, B_CH1, C_CH1) ~ c(A_CH2, B_CH2, C_CH2) | gl(3, 4), dat, layout = c(3, 1))) Note that 1. This is with the data frame as it is, except that I assume the last 2 columns are named C_CH1 and C_CH2 2. This plots the mean instead of the sum (but on the other hand, it's a standard plot that way) Deepayan> par(mfrow=c(3,1)) > plot(dat$A_CH1+dat$A_CH2, dat$A_CH1-dat$A_CH2) > plot(dat$B_CH1+dat$B_CH2, dat$B_CH1-dat$B_CH2) > plot(dat[,5]+dat[,6], dat[,5]-dat[,6]) > > Thanks a lot for your help