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.

Saturday, April 12, 2025

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

So, you’ve appropriately applied the one-way ANOVA methodology within the R-Programming Language – and you’ve already reported your results within the APA format. However, you now require a visualization of your data.

Example:

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


All of the factors must be contained within a single variable (Group), and all of the associated observations must be contained with a corresponding variable (Observation).

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

Group = c('A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'D', 'D', 'D', 'D', 'D', 'D', 'D', 'D', 'D', 'D', 'D', 'D', 'D', 'D', 'D')

Observation = c(3, 6, 6, 2, 4, 6, 5, 4, 7, 7, 6, 2, 2, 4, 7, 7, 6, 6, 7, 7, 7, 7, 7, 6, 6, 7, 7, 5, 2, 5, 4, 4, 7, 4, 4, 5, 5, 6, 7, 6, 1, 1, 6, 1, 5, 7, 7, 7, 4, 6, 4, 2, 3, 7, 7, 7, 5, 7, 7, 7, 7, 7, 6, 7, 6, 7, 7, 7, 7, 7, 4, 6, 7, 5, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 1)

ANOVA_TEST = data.frame(Group, Observation)


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

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

library(ggplot2)

library(gplots)

## Requires library 'ggplot2' ##

ggplot(ANOVA_TEST) +
aes(x = Group, y = Observation, color = Group, shape = Group) + ## Each group has its own color and each group has its own shape. Remove "shape = " to surpress this ##
geom_jitter(width = 0.2, alpha = 0.6) + ## Tighten up the group width ##
stat_summary(fun = mean, geom = "point", shape = 95, size = 6) + # color mapped by Group ## 
## The "-" found in each group's y-axis values, is the mean value for that group ##
theme(legend.position = "right") ## To remove the legend, select legend.position = "none" ##

## Requires library 'gplots' ##

ANOVA_TEST$Group = factor(ANOVA_TEST$Group)

plotmeans(ANOVA_TEST$Observation ~ ANOVA_TEST$Group,
xlab = "Watch Type\n(Error Bars: 95% CI)", # x-axis label
ylab = "Observation",
main = "Place Your Title Here",
pch = 16) # Solid points #

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

Output


In the above graphic, each group variable (factor) displayed on the x-axis, has its corresponding observational values displayed on the y-axis. Each group’s observations possess a different color and shape. The single dash “-“ found within each group amongst its observation points, represents that group’s mean value. 


This graphic presents a different visualization approach. Each group on the x-axis has its number of corresponding observations displayed above each axis point (n=). The solid black dots represent the mean of each group on the x-axis, while the blue lines represent the range of associated confidence interval values.

These visualizations, along with your results reported within the APA format, should assist you in presenting your findings to readers in the most comprehensive manner possible. There are different customization options as it pertains to each graphing function. However, you’ll need to do additional research to determine what best works for your needs.

Until next time,

-RD