jiho.han wrote:> hello, useRs~
> suppose i have a matrix as follows:
> item category sub-category
> A 1 11
> B 1 12
> C 1 12
> D 2 21
> E 2 22
>
> i like to draw a plot that represent the proportion of each
> category at each level. that is, i want a plot like this:
> +-----+ +-----+
> | | | 11 |
> | | +-----+
> | 1 | | 12 |
> | | | |
> +-----+ +-----+
> +-----+ +-----+
> | | | 21 |
> | 2 | +-----+
> | | | 22 |
> +-----+ +-----+
> category sub-cateory
>
> hope my plot makes sense. anyone know how to make such plot?
> any hint/suggestion would be much appreciate. thank you.
>
Hi jiho,
An interesting request. Here is a very basic function that does what you
want (I think) with a slightly expanded dataset to show its generality.
item cat subcat subsubcat
A 1 11 111
B 1 12 121
C 1 12 122
D 2 21 211
E 2 22 221
F 3 31 311
G 3 31 312
H 3 32 322
I 3 32 322
J 3 33 331
K 3 34 341
L 3 34 341
M 3 34 342
bh<-read.table("bh.dat",header=TRUE)
barhier<-function(x) {
dimx<-dim(x)
plot(0,xlim=c(0,dimx[2]),ylim=c(0,dimx[1]),axes=FALSE,type="n",
xlab="",ylab="")
for(level in 1:dimx[2]) {
top<-dimx[1]
levelfreq<-freq(x[,level])
valuenames<-names(levelfreq[[1]])
for(value in 1:length(levelfreq[[1]])) {
bottom<-top-levelfreq[[1]][value]
rect(level-1,bottom,level,top)
text(level-0.5,(bottom+top)/2,valuenames[value])
top<-bottom
}
}
}
barhier(bh[,-1])
Jim