#Chi-Square test of Independence library(dplyr) library(ggplot2) library(reshape2) library(DescTools) #create contingency table fftab = as.table(matrix(c(62, 58, 48, 22, 15, 29, 5, 7, 10, 73, 75, 103), ncol=4, nrow=3, dimnames = list(c("Sydney","Singapore","Jakarta"), c("Bronze","Silver", "Gold","None")))) fftab #set up data to generate bar charts fftab_m <- melt(fftab, varnames = c("City", "Status"), id.vars = "City") #set up data frame to generate bar charts ggplot(fftab_m %>% group_by(City) %>% mutate(Percentage = round(value/sum(value),3)), aes(x=City, y = Percentage, fill = Status, cumulative = TRUE)) + geom_col() + geom_text(aes(label = paste0(Percentage*100,"%")), position = position_stack(vjust = 0.5)) #column percentages PercTable(fftab, 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(fftab, 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(fftab, 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 chisq.test(fftab, correct=FALSE)