Saturday, April 19, 2025

(R) Visually Presenting the Results of a Two-Way ANOVA Model

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

Satisfaction = c(7, 2, 10, 2, 2, 8, 5, 1, 3, 10, 9, 10, 3, 10, 8, 7, 5, 6, 4, 10, 3, 6, 4, 7, 1, 5, 5, 2, 2, 2)

StudyTime = c('One Hour', 'One Hour', 'One Hour', 'One Hour', 'One Hour', 'One Hour', 'One Hour', 'One Hour', 'One Hour', 'One Hour', 'Two Hours', 'Two Hours', 'Two Hours', 'Two Hours', 'Two Hours', 'Two Hours', 'Two Hours', 'Two Hours', 'Two Hours', 'Two Hours', 'Three Hours', 'Three Hours', 'Three Hours', 'Three Hours', 'Three Hours', 'Three Hours', 'Three Hours', 'Three Hours', 'Three Hours', 'Three Hours')

School = c('SchoolA', 'SchoolA', 'SchoolA', 'SchoolA', 'SchoolA', 'SchoolB', 'SchoolB', 'SchoolB', 'SchoolB', 'SchoolB', 'SchoolA', 'SchoolA', 'SchoolA', 'SchoolA', 'SchoolA', 'SchoolB', 'SchoolB', 'SchoolB', 'SchoolB', 'SchoolB', 'SchoolA', 'SchoolA', 'SchoolA', 'SchoolA', 'SchoolA', 'SchoolB', 'SchoolB', 'SchoolB', 'SchoolB', 'SchoolB')

School_Sat = data.frame(Satisfaction, StudyTime, School)


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

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

# Load Package #

library(ggpubr)


# Mean and Standard Error #

ggline(subset(School_Sat), 
x = "StudyTime",
y = "Satisfaction",
color = "School",
add = c("mean_se") # add mean and standard error #
) +
labs(y = "Satisfaction")


# Mean Only w/ Title #

ggline(subset(School_Sat), 
x = "StudyTime",
y = "Satisfaction",
color = "School",
add = c("mean") # add mean only #
) +
labs(y = "Satisfaction") +
ggtitle("Your Title Here") +
theme(plot.title = element_text(hjust = 0.5))

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

In each instance of the output, different options as it pertains to the ggplot() function, are specified. In the first example, the graphic contains 95% CI bars. This can also be disabled, if necessary.

To create a customized graphic which contains attributes more specific to your research, please consult the "ggpubr" manual. This can be found online.

No comments:

Post a Comment

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