Monday, May 12, 2025

(R) Visually Presenting the Results of a Chi-Squared Test


In this article, I’ll demonstrate how to create a Chi-Square visualization in order to enhance your research findings.

Example:

In order for this code to function, your data must be structured in a manner which resembles the following graphic:


The following code below will create the example’s associated data frame with the R-Programming Language.

Smoking = c('Smoker', 'Smoker', 'Smoker', 'Smoker', 'Smoker', 'Smoker', 'Smoker', 'Non-Smoker', 'Non-Smoker', 'Non-Smoker')

Obesity = c('Not Obese', 'Not Obese', 'Obese', 'Obese', 'Obese', 'Obese', 'Obese', 'Obese', 'Not Obese', 'Not Obese')

Smoker = data.frame(Smoking, Obesity)


##############################################################

# Getting the Libraries in Order (for Graphical Output) #

library(ggplot2)
library(dplyr)

# Assuming your dataset is called Smoker and has columns Smoking and Obesity #

Smoker <- Smoker %>%
group_by(Smoking, Obesity) %>%
tally() %>%
ungroup() %>%
group_by(Smoking) %>%
mutate(percentage = n / sum(n)) %>%
ungroup()

Smoker$percentage <- Smoker$percentage * 100


# Re-level Obesity so "Obese" appears first #

Smoker$Obesity <- factor(Smoker$Obesity, levels = c("Obese", "Not Obese"))

# Now plot the bar chart with percentage values #

ggplot(Smoker, aes(x = Smoking, y = percentage, fill = Obesity)) +
geom_bar(stat = "identity", position = "dodge") + # Use stat = "identity" since you already calculated the percentages #
scale_y_continuous(labels = scales::percent_format(scale = 1)) + # Format y-axis as percentage #
labs(y = "Percentage") +
theme_minimal()


# Relevel Smoking so "Smoker" appears first #

Smoker$Smoking <- factor(Smoker$Smoking, levels = c("Smoker", "Non-Smoker"))

# Now plot the bar chart with percentage values #

ggplot(Smoker, aes(x = Smoking, y = percentage, fill = Obesity)) +
geom_bar(stat = "identity", position = "dodge") + # Use stat = "identity" since you already calculated the percentages #
scale_y_continuous(labels = scales::percent_format(scale = 1)) + # Format y-axis as percentage #
labs(y = "Percentage") +
theme_minimal()

##############################################################

Until next time,

-RD  

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.