Splitting the columns of a matrix, sorted dotchart of each one, then laying out in figure
I have a matrix of data (with row names and column names). I would like to do a dotchart of each column o开发者_如何学JAVAf data, with the data sorted from largest to smallest in each chart, and then lay out these plots one atop the other in the figure.
For example, if I had the following matrix:
> X
W X Y Z
A 11.7 8.7 15.4 100.0
B 18.1 11.7 9.0 13.6
C 10.0 20.3 37.0 19.3
D 41.0 30.9 20.0 35.1
E 66.0 3.0 71.1 50.0
I want to do four dotcharts (columns "W","X","Y","Z"), with the data sorted from largest to smallest for each chart, and the charts laid out on top of one other.
(Note that dotchart(X)
won't work because the sort will be different for each column, see question 4654181)
What's the sequence of R commands that would allow me to do that?
apply(X, 2, sort)
W X Y Z
[1,] 10.0 3.0 9.0 13.6
[2,] 11.7 8.7 15.4 19.3
[3,] 18.1 11.7 20.0 35.1
[4,] 41.0 20.3 37.0 50.0
[5,] 66.0 30.9 71.1 100.0
dotchart(apply(X, 2, sort))
EDIT: To add the labels correctly sorted, you need to use names() on the columns then order()ed by the column values:
dotchart(apply(X, 2, sort),
labels=apply(X, 2, function(x) names(x)[order(x)] )
)
精彩评论