Remove names of categories from a mosaic plot and add a legend

1.8k Views Asked by At

I have this contingency table:

> ftable(mytable) # print table
                Sex     female male
Pclass Survived                    
1      No             0      5  118
       Si             0    139   61
2      No             0     12  146
       Si             0     94   25
3      No             0    110  418
       Si             0    106   75

Then, I make a mosaic plot using the base package of R. The code and the graphic are the following:

mosaicplot(mytable, color=c("#fb8072", "#8dd3c7"), cex.axis=1.2)

enter image description here

Now I want to dissapear the names female and male from the x-axis and put that names as a legend so that it's specified that pink refert to female and light blue to male. How Can I do that?

EDIT 1:

The output of "mytable" is :

mytable <- structure(c(0, 5, 118, 0, 139, 61, 0, 12, 146, 0, 94, 25, 0, 
110, 418, 0, 106, 75), .Dim = c(3L, 2L, 3L), .Dimnames = structure(list(
    Sex = c("", "female", "male"), Survived = c("No", "Si"), 
    Pclass = c("1", "2", "3")), .Names = c("Sex", "Survived", 
"Pclass")), class = c("xtabs", "table"))

EDIT 2:

Before the code to plot the mosaic , I got to remove the names of categories in the plot removing the names from the contingency table with the following code:

dimnames(mytable)[["Sex"]] = rep(NA,3)

Now, the only thing that remains without resolution is to add a legend.

0

There are 0 best solutions below