# Chi-square tests for #of the difference between two population proportions library(dplyr) library(ggplot2) library(reshape2) library(DescTools) #create contingency table airtab = as.table(matrix( c(258,375,210, 162,155,190), ncol=2, nrow=3, dimnames = list(c("Sydney","Singapore","Jakarta"), c("Online","Airport")))) airtab #set up data to generate bar charts airtab_m <- melt(airtab, varnames = c("City", "Method"), id.vars = "City") #set up data frame to generate bar charts ggplot(airtab_m %>% group_by(City) %>% mutate(Percentage = round(value/sum(value),2)), aes(x=City, y = Percentage, fill = Method, cumulative = TRUE)) + geom_col() + geom_text(aes(label = paste0(Percentage*100,"%")), position = position_stack(vjust = 0.5)) #column percentages PercTable(airtab, row.vars = NULL, col.vars = NULL, justify = "right", freq = TRUE, rfrq = "001", expected = FALSE, residuals = FALSE, stdres = FALSE, margins = c(1,2), digits = 0) #row percentages PercTable(airtab, row.vars = NULL, col.vars = NULL, justify = "right", freq = TRUE, rfrq = "010", expected = FALSE, residuals = FALSE, stdres = FALSE, margins = c(1,2), digits = 0) #expected frequencies, residuals PercTable(airtab, row.vars = NULL, col.vars = NULL, justify = "right", freq = TRUE, rfrq = "000", expected = TRUE, residuals = TRUE, stdres = FALSE, margins = c(1,2), digits = NULL) #chi-square test prop.test(airtab, correct=FALSE) chisq.test(airtab, correct=FALSE)