Monday, May 12, 2025

(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. 

No comments:

Post a Comment

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