C W
2013-Apr-18 22:30 UTC
[R] How to keep plot title same as column name using apply/sapply?
Dear list, I am trying to plot histogram of a 10 by 5 matrix by columns. What is a good way to paste the column names? dput(a) structure(c(0.891856482875103, 0.249593821948295, 0.0385066520473322, 0.109098868876336, 0.238035894186719, 0.971470380855763, 0.168018536530906, 0.941457062296419, -0.285381460315397, -0.0229335863796271, -20138.175683257, 28190.7238887329, 8521.44473371867, 37565.8599592035, -61036.6139527803, -57278.42539968, 24284.2860193156, 57470.8275786857, -5035.05919672665, -48223.3060594833, 0.105003428645432, 0.0385466025676578, 0.130895042931661, 0.0682065344415605, 0.155879093753174, 0.0598988083656877, 0.121624303748831, 0.120134906983003, 0.159434637567028, 0.0144033632241189, 6133.33965969139, 5363.20408479208, 22432.8473630982, 10627.5697498379, 273.481263455154, 8326.07386291277, 133.117356408579, 4108.23352922685, 643.558745779806, 5532.10081652847, -4512.80115728638, -5839.1178791698, 7341.05048720629, -1299.37511688102, -13151.5558568303, -6442.78100296569, -975.590624649323, 298.440121450997, 10808.9562034117, 3210.22497745543), .Dim = c(10L, 5L), .Dimnames = list(NULL, c("Tmrw", "hRm2", "vm", "Fluid", "SM"))) par(mfrow=c(3, 2)) apply(a, 2, function(x){hist(x, main="column name here")}) This plots, but doesn't plot title. But,> apply(a,2,function(x){colnames(x)})NULL Maybe the column names is lost? But, I see column names here.> apply(a,2,function(x){is.vector(x)})Tmrw hRm2 vm Fluid SM TRUE TRUE TRUE TRUE TRUE My understanding is that, apply() takes data matrix column by column, and pastes names back on at the end? But I need it for plot title, how should I change my code? Thanks, Mike
Bert Gunter
2013-Apr-18 22:36 UTC
[R] How to keep plot title same as column name using apply/sapply?
Note that in: apply(a,2,function(x){colnames(x)}) x is a vector, and therefore has no column names. -- Bert On Thu, Apr 18, 2013 at 3:30 PM, C W <tmrsg11 at gmail.com> wrote:> Dear list, > I am trying to plot histogram of a 10 by 5 matrix by columns. > > What is a good way to paste the column names? > > dput(a) > structure(c(0.891856482875103, 0.249593821948295, 0.0385066520473322, > 0.109098868876336, 0.238035894186719, 0.971470380855763, 0.168018536530906, > 0.941457062296419, -0.285381460315397, -0.0229335863796271, -20138.175683257, > 28190.7238887329, 8521.44473371867, 37565.8599592035, -61036.6139527803, > -57278.42539968, 24284.2860193156, 57470.8275786857, -5035.05919672665, > -48223.3060594833, 0.105003428645432, 0.0385466025676578, 0.130895042931661, > 0.0682065344415605, 0.155879093753174, 0.0598988083656877, 0.121624303748831, > 0.120134906983003, 0.159434637567028, 0.0144033632241189, 6133.33965969139, > 5363.20408479208, 22432.8473630982, 10627.5697498379, 273.481263455154, > 8326.07386291277, 133.117356408579, 4108.23352922685, 643.558745779806, > 5532.10081652847, -4512.80115728638, -5839.1178791698, 7341.05048720629, > -1299.37511688102, -13151.5558568303, -6442.78100296569, -975.590624649323, > 298.440121450997, 10808.9562034117, 3210.22497745543), .Dim = c(10L, > 5L), .Dimnames = list(NULL, c("Tmrw", "hRm2", "vm", "Fluid", > "SM"))) > par(mfrow=c(3, 2)) > apply(a, 2, function(x){hist(x, main="column name here")}) > > This plots, but doesn't plot title. > > But, >> apply(a,2,function(x){colnames(x)}) > NULL > > Maybe the column names is lost? But, I see column names here. > >> apply(a,2,function(x){is.vector(x)}) > Tmrw hRm2 vm Fluid SM > TRUE TRUE TRUE TRUE TRUE > > My understanding is that, apply() takes data matrix column by column, > and pastes names back on at the end? But I need it for plot title, > how should I change my code? > > Thanks, > Mike > > ______________________________________________ > R-help at 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.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
arun
2013-Apr-18 22:49 UTC
[R] How to keep plot title same as column name using apply/sapply?
Hi, Try: ?par(mfrow=c(3, 2)) mapply(hist,as.data.frame(a),main=colnames(a),xlab="x") A.K. ----- Original Message ----- From: C W <tmrsg11 at gmail.com> To: r-help <r-help at r-project.org> Cc: Sent: Thursday, April 18, 2013 6:30 PM Subject: [R] How to keep plot title same as column name using apply/sapply? Dear list, I am trying to plot histogram of a 10 by 5 matrix by columns. What is a good way to paste the column names? dput(a) structure(c(0.891856482875103, 0.249593821948295, 0.0385066520473322, 0.109098868876336, 0.238035894186719, 0.971470380855763, 0.168018536530906, 0.941457062296419, -0.285381460315397, -0.0229335863796271, -20138.175683257, 28190.7238887329, 8521.44473371867, 37565.8599592035, -61036.6139527803, -57278.42539968, 24284.2860193156, 57470.8275786857, -5035.05919672665, -48223.3060594833, 0.105003428645432, 0.0385466025676578, 0.130895042931661, 0.0682065344415605, 0.155879093753174, 0.0598988083656877, 0.121624303748831, 0.120134906983003, 0.159434637567028, 0.0144033632241189, 6133.33965969139, 5363.20408479208, 22432.8473630982, 10627.5697498379, 273.481263455154, 8326.07386291277, 133.117356408579, 4108.23352922685, 643.558745779806, 5532.10081652847, -4512.80115728638, -5839.1178791698, 7341.05048720629, -1299.37511688102, -13151.5558568303, -6442.78100296569, -975.590624649323, 298.440121450997, 10808.9562034117, 3210.22497745543), .Dim = c(10L, 5L), .Dimnames = list(NULL, c("Tmrw", "hRm2", "vm", "Fluid", "SM"))) par(mfrow=c(3, 2)) apply(a, 2, function(x){hist(x, main="column name here")}) This plots, but doesn't plot title. But,> apply(a,2,function(x){colnames(x)})NULL Maybe the column names is lost?? But, I see column names here.> apply(a,2,function(x){is.vector(x)})Tmrw? hRm2? ? vm Fluid? ? SM TRUE? TRUE? TRUE? TRUE? TRUE My understanding is that, apply() takes data matrix column by column, and pastes names back on at the end?? But I need it for plot title, how should I change my code? Thanks, Mike ______________________________________________ R-help at 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.