geom_mosaic: X axis tick labels not showing?

2.2k Views Asked by At

I'm using ggmosaic::geom_mosaic to generate a mosaic plot. I can't seem to get the value labels for my independent variable to show on the X axis. The variable is a labelled factor, with levels labelled "1", "2", "3". I'm sure it's something stupid but I can't figure out what's going on here. Any insight would be appreciated.

ggplot(data = mosaic)+
geom_mosaic(aes(x = product(X, Norm_Dx), fill=Norm_Dx), na.rm=TRUE)

enter image description here

2

There are 2 best solutions below

0
On

I recommend you use the mosaicplot.

mosaicplot(table(X, Norm_Dx))

For more information please refer to this tutorial.

0
On

The github issue seems to be not quite resolved yet ... so for anyone else desperate for a mosaic plot in ggplot in the meantime and unable to get the update from github to work, it's possible to use annotate:

data(Titanic)

titanic <- as.data.frame(Titanic)
titanic$Survived <- factor(titanic$Survived,
                           levels = c("Yes", "No"))

ggplot(data = titanic) +
  geom_mosaic(aes(weight = Freq, x = product(Class), fill = Survived)) +
  labs(x = "Passenger class",
       y = "Survived sinking",
       title = "Survival rate by passenger class")+      
  annotate(geom="text",x=0.43,y=-0.02,label="This                 is a                          very silly                                     solution",
           color="black",size=3)  +
  annotate(geom="text",x=-0.02,y=0.5,label="0                    0.25                  0.5                  0.75                    1",
           colour="black",size=3,angle=90)

enter image description here