knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(GGally)
GGally::ggtable()
The purpose of this function is to quickly plot cross-tabulated tables of discrete variables.
To display tables with the number of observations, simply indicate variables to present in columns and in rows.
data(tips) ggtable(tips, "smoker", c("day", "time", "sex")) ggtable(tips, c("smoker", "time"), c("day", "time", "sex"))
The cells
argument allows you to control what to display. For proportions of the total, row proportions or columns proportions, simply use "prop"
, "row.prop"
or "col.prop"
.
ggtable(tips, "smoker", c("day", "time", "sex"), cells = "prop") + ggtitle("Proportions of total") ggtable(tips, "smoker", c("day", "time", "sex"), cells = "row.prop") + ggtitle("Row proportions") ggtable(tips, "smoker", c("day", "time", "sex"), cells = "prop") + ggtitle("Column proportions")
Chi-square standardized residuals indicates which cells are over- or underrepresented compared to what would be expected under the independence hypothesis. If the standardized residual is less than -2, the cell’s observed frequency is less than the expected frequency. Greater than 2 and the observed frequency is greater than the expected frequency. Values lower than -3 or higher than 3 indicates a strong effect.
To fill cells with standardized residuals, simply indicate fill = "std.resid"
.
ggtable(tips, "smoker", c("day", "time", "sex"), fill = "std.resid")
You can easily indicate weights to take into account with the weight aesthetic.
d <- as.data.frame(Titanic) ggtable( d, "Survived", c("Age", "Class", "Sex"), mapping = aes(weight = Freq), cells = "row.prop", fill = "std.resid" )
Although ggtable()
is mainly intended to be used with discrete variables, you can also indicate continuous variables. In such case, some summary statistics are displayed.
ggtable(tips, c("smoker", "total_bill"), c("day", "time", "sex", "tip"))
If you need more customization of the output, please refer to ggduo()
.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.