简体   繁体   中英

R - Modified mosaic plot from descr package

I have a dataframe db with 2 categorical variables: varA has 4 levels ( 0 , 1 , 2 , 3 ), varB has 2 levels ( yes , no ). varB has no values for the level 0 of varA :

id  varA    varB
1   2       yes
2   3       no
3   3       no
4   1       yes
5   0       NA
6   1       no
7   2       no
8   3       yes
9   3       yes
10  2       no

I created a contingency table using CrossTable from the descr package and then a mosaic plot with the plot function:

table <- CrossTable(db$varA,db$varB, missing.include=FALSE)
plot(table,xlab="varA",ylab="varB")

I obtained this plot:

马赛克图

I would like to eliminate the level 0 from the plot. I also would like to add 2 y-axis, one on the left of the plot with a scale from 0 to 1 and one on the right with a scale from 1 to 0.

Could you help me?

Well, that was annoying. There is no support for subsetting such a "CrossTable" object. If it were a well-behaved table-like object you would been able to just pass table[ , -1] to the plot function. instead you need to do the subetting before the data that is passed to CrossTable:

table <- with( na.omit(db), CrossTable( varA, varB, missing.include=TRUE))
plot(table, xlab="varA", ylab="varB")

BTW using the name table for a data-object is quite confusing to regular R users since the table function is one of our basic tools.

Personally I would avoid avoid using that CrossTable function since its output is so weird and not available for management with typical R functions. Yeah, I know it produces a SAS-like output, but R users grow to love the compact output of the table function and the many matrix operations that are available for working with table-objects. You may need to get your margin percentages by hand with prop.table .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM