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  

(R) Visually Presenting the Results of a T-Test


In this article, I’ll demonstrate how to create a T-Test 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.

Group = c('Original_Watch', 'Original_Watch', 'Original_Watch', 'Original_Watch', 'Original_Watch', 'Original_Watch', 'Original_Watch', 'Original_Watch', 'Original_Watch', 'Original_Watch', 'Original_Watch', 'Original_Watch', 'New_Watch', 'New_Watch', 'New_Watch', 'New_Watch', 'New_Watch', 'New_Watch', 'New_Watch', 'New_Watch', 'New_Watch', 'New_Watch', 'New_Watch', 'New_Watch')

Observation = c(376, 293, 210, 264, 297, 380, 398, 303, 324, 368, 382, 309, 337, 341, 316, 351, 371, 440, 312, 416, 445, 354, 444, 326)

Watch = data.frame(Group, Observation)

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

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

# load package

library(ggplot2)

# Specify the order of the groups #

Watch$Group <- factor(Watch$Group, levels = c("Original_Watch", "New_Watch"))


# Graph w/o Title #

ggplot(data = Watch, aes(x = Observation, y = Group, fill = Group)) +
geom_bar(stat = 'summary') +
geom_errorbar(stat = 'summary', width = 0.2) +
theme(legend.position = "none") +
coord_flip()


# Graph with Title #

ggplot(data = Watch, aes(x = Observation, y = Group, fill = Group)) +
geom_bar(stat = 'summary') +
geom_errorbar(stat = 'summary', width = 0.2) +
coord_flip() +
theme(legend.position = "none",
plot.title = element_text(hjust = 0.5)) +
labs(title = "Your Title Here")


# Graph with Secondary Y Label #

ggplot(data = Watch, aes(x = Observation, y = Group, fill = Group)) +
geom_bar(stat = 'summary') +
geom_errorbar(stat = 'summary', width = 0.2) +
coord_flip() +
theme(
legend.position = "none",
plot.title = element_text(hjust = 0.5)
) +
labs(
title = "Your Title Here",
x = "Observation",
y = "Watch Type\n\n(Error Bars: 95% CI)"
)

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


In each instance of the output, different options as it pertains to the ggplot() function are specified. In the examples above, the 95% CI bars are the shared attribute amongst all outputs. This can also be disabled, if necessary.