## Necessary packages to install
install.packages("readxl")
install.packages("ggplot2")
install.packages("ggraph")
install.packages("dplyr")
install.packages("tidyr")
install.packages("tidyverse")
install.packages("plotly")
install.packages("knitr")
install.packages("kableExtra")
install.packages("psych")
# 1. Set a working directory to where the .csv file is located
Please set your working directory before you run the code
setwd("path/to/your/directory")
# 2. Loading all the necessary packages
library(readxl)
library(ggplot2)
library(ggraph)
library(dplyr)
library(tidyr)
library(tidyverse)
library(plotly)
library(knitr)
library(kableExtra)
library(psych)
# Necessary packages
packages_to_install <- c("readxl", "ggplot2", "ggraph", "dplyr", "tidyr", "tidyverse", "plotly",
"knitr","KableExtra")
# Install and load packages
install_and_load <- function(package) {
if (!require(package, character.only = TRUE, quietly = TRUE)) {
install.packages(package, dependencies = TRUE)
library(package, character.only = TRUE)
}
}
# Apply the function to install and load each package
invisible(sapply(packages_to_install, install_and_load))
3. Read the CSV file
# Read the CSV file
data <- read.csv("C:/Users/svudaru/Desktop/R files/SampleDataforReportingPipeline.csv")
#removing missing values from the data
data <- na.omit(data)
#printing data summary
summary(data) # you can see there are no more missing values
## subjectID Group Condition1_Item1 Condition1_Item2
## Min. : 1.00 Min. :1.000 Min. :1.000 Min. :1.00
## 1st Qu.: 25.50 1st Qu.:1.000 1st Qu.:3.000 1st Qu.:2.00
## Median : 50.00 Median :2.000 Median :5.000 Median :4.00
## Mean : 50.29 Mean :1.535 Mean :4.414 Mean :3.99
## 3rd Qu.: 74.50 3rd Qu.:2.000 3rd Qu.:6.000 3rd Qu.:6.00
## Max. :101.00 Max. :2.000 Max. :7.000 Max. :7.00
## Condition1_Item3 Condition1_Item4 Condition1_Item5 Condition1_Item6
## Min. :1.000 Min. :1.000 Min. :1.000 Min. :1.000
## 1st Qu.:3.000 1st Qu.:2.000 1st Qu.:2.000 1st Qu.:2.000
## Median :5.000 Median :3.000 Median :4.000 Median :4.000
## Mean :4.455 Mean :3.697 Mean :4.071 Mean :3.848
## 3rd Qu.:7.000 3rd Qu.:5.000 3rd Qu.:6.000 3rd Qu.:6.000
## Max. :7.000 Max. :7.000 Max. :7.000 Max. :7.000
## Condition1_Item7 Condition1_Item8 Condition2_Item1 Condition2_Item2
## Min. :1.000 Min. :1.000 Min. :5.000 Min. :5.000
## 1st Qu.:2.000 1st Qu.:2.000 1st Qu.:5.000 1st Qu.:5.000
## Median :4.000 Median :3.000 Median :6.000 Median :6.000
## Mean :3.919 Mean :3.596 Mean :5.909 Mean :5.949
## 3rd Qu.:6.000 3rd Qu.:5.000 3rd Qu.:7.000 3rd Qu.:7.000
## Max. :7.000 Max. :7.000 Max. :7.000 Max. :7.000
## Condition2_Item3 Condition2_Item4 Condition2_Item5 Condition2_Item6
## Min. :5.000 Min. :5.00 Min. :5.00 Min. :5.000
## 1st Qu.:5.000 1st Qu.:5.00 1st Qu.:5.00 1st Qu.:5.500
## Median :6.000 Median :6.00 Median :6.00 Median :6.000
## Mean :6.121 Mean :6.04 Mean :5.99 Mean :6.141
## 3rd Qu.:7.000 3rd Qu.:7.00 3rd Qu.:7.00 3rd Qu.:7.000
## Max. :7.000 Max. :7.00 Max. :7.00 Max. :7.000
## Condition2_Item7 Condition2_Item8
## Min. :5.00 Min. :5.00
## 1st Qu.:5.00 1st Qu.:5.00
## Median :6.00 Median :6.00
## Mean :5.97 Mean :6.01
## 3rd Qu.:7.00 3rd Qu.:7.00
## Max. :7.00 Max. :7.00
4. Creating a table showing the N, mean, standard deviation, and
standard error
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyr)
# 4. Creating a table showing the N, mean, standard deviation, and standard error
# Melt the data frame to long format
data_long <- data %>%
pivot_longer(
cols = starts_with("Condition"),
names_to = c("Condition", ".value"),
names_pattern = "Condition(\\d+)_(Item\\d+)"
)
5 a Plot standard error as error bars with white bars for Condition
1 and gray bars for Condition 2
library(ggplot2)
# 5 a Plot standard error as error bars with white bars for Condition 1 and gray bars for Condition 2
condition_levels <- unique(result_table$Condition)
# Plot standard error as error bars with white bars for Condition 1 and gray bars for Condition 2
ggplot(result_table, aes(x = Item, y = Mean, group = Condition, fill = Condition)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.8), color = "black") +
geom_errorbar(aes(ymin = Mean - SE, ymax = Mean + SE), position = position_dodge(width = 0.8), width = 0.25, color = "black") +
scale_fill_manual(values = setNames(c("white", "gray"), condition_levels)) +
labs(title = "Error Bar Plot of Mean Values with Standard Error",
x = "Item",
y = "Mean Value") +
theme_minimal()

5b Create interleaved bar graph for mean of each item for Condition
1 and Condition 2, collapsing across Group
# 5b Create interleaved bar graph for mean of each item for Condition 1 and Condition 2, collapsing across Group
ggplot(result_table, aes(x = Item, y = Mean, fill = Condition)) +
geom_bar(stat = "identity", position = "dodge", width = 0.8) +
labs(title = "Interleaved Bar Graph of Mean Values for Condition 1 and Condition 2 (Collapsed Across Group)",
x = "Item",
y = "Mean Value") +
theme_minimal()

Filter for Group 1 participants
# Filter for Group 1 participants
result_table_group1 <- result_table %>%
filter(Group == 1)
# Create the same bar graph as in 4 for just Group 1 participants
ggplot(result_table_group1, aes(x = Item, y = Mean, group = Condition, fill = Condition)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.8), color = "black") +
geom_errorbar(aes(ymin = Mean - SE, ymax = Mean + SE), position = position_dodge(width = 0.8), width = 0.25, color = "black") +
labs(title = "Error Bar Plot of Mean Values with Standard Error (Group 1)",
x = "Item",
y = "Mean Value") +
theme_minimal()
