rmosaic-plot

Elegant way to save mosaic plots?


Most of my plots are made with ggplot2 and the ggsave command saves them where they should be with one line. However, mosaic plots using the vcd package are best for my data. Problem: I don't get an error message with the following code. R says that it has saved my plot, but the plot that gets saved is the last ggplot plot I created, not the mosaic plot I want. Of course I can manually save in RStudio, but I'm quite sure there is a better way. Any ideas?

Onlyaround <- subset(prepData, preposition=="around")
attach(Onlyaround)
mytable <- table(exp_group, session, result)
ftable(mytable)
mosaic(mytable, shade=TRUE, legend=TRUE, main = "Around by Group")
margin.table(mytable)
ggsave("pics/around_mosaic.png")
detach(Onlyaround) 

Solution

  • ggsave() is in fact the command to save ggplots, so it's no surprise that it doesn't save you mosaic plot. The standard 'R' way to save plots will work just fine:

    jpeg("pics/around_mosaic.png")
    mosaic(mytable, shade=TRUE, legend=TRUE, main = "Around by Group")
    dev.off()