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:BTW using the name
table
for a data-object is quite confusing to regular R users since thetable
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 withprop.table
.