[R] How to keep plot title same as column name using apply/sapply?
arun
smartpink111 at yahoo.com
Fri Apr 19 04:18:38 CEST 2013
Hi,
You can try:
par(mfrow=c(3, 2))
mapply(hist,split(a,col(a)),main=colnames(a),xlab="x")
If you look at the output,
mapply(hist,a,main=colnames(a),xlab="x")
[,1] [,2] [,3]
breaks Numeric,2 Numeric,2 Numeric,2
counts 1 1 1
density 2 5 20
mids 0.75 0.3 0.025
xname "dots[[1L]][[1L]]" "dots[[1L]][[2L]]" "dots[[1L]][[3L]]"
equidist TRUE TRUE TRUE
[,4] [,5] [,6]
breaks Numeric,2 Numeric,2 Numeric,2
counts 1 1 1
density 10 5 1
mids 0.15 0.3 0.5
_____________________________________
[,46] [,47] [,48]
breaks Numeric,2 Numeric,2 Numeric,2
counts 1 1 1
density 0.002 0.01 0.02
mids -6250 -950 275
xname "dots[[1L]][[46L]]" "dots[[1L]][[47L]]" "dots[[1L]][[48L]]"
equidist TRUE TRUE TRUE
[,49] [,50]
breaks Numeric,2 Numeric,2
counts 1 1
density 0.001 0.005
mids 10500 3300
xname "dots[[1L]][[49L]]" "dots[[1L]][[50L]]"
equidist TRUE TRUE
#it is going through every single observation rather than by columns.
A.K.
----- Original Message -----
From: C W <tmrsg11 at gmail.com>
To: arun <smartpink111 at yahoo.com>
Cc: R help <r-help at r-project.org>
Sent: Thursday, April 18, 2013 7:20 PM
Subject: Re: [R] How to keep plot title same as column name using apply/sapply?
> mapply(hist,as.data.frame(a),main=colnames(a),xlab="x")
Why it does't work when I use "a" instead of "as.data.frame(a)"?
?mapply says "arguments to vectorize over (vectors or lists of
strictly positive length, or all of zero length)."
Thanks,
Mike
On Thu, Apr 18, 2013 at 6:49 PM, arun <smartpink111 at yahoo.com> wrote:
> 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.
>
More information about the R-help
mailing list