data2 <- read.csv("./Study 2.csv")
# Set up the plotting area to have two plots side by side
par(mfrow = c(1, 2), mgp = c(1, 0.1, 0))
# Plot the first histogram for Relatedness Difference Scores
hist(data2$BMPN_Diff,
main = "Distribution of Relatedness Difference Scores",
xlab = "Relatedness Difference Score (T2 - T1)",
ylab = "Frequency",
ylim = c(0, 80),
xlim = c(-5.7, 4),
xaxs = "i", # Connect x-axis to y-axis
yaxs = "i", # Connect y-axis to x-axis
breaks = 18, # Increase the number of bars
col = "gray",
cex.axis = 0.7,
tck = -0.015, # Make tick marks shorter
xaxt = 'n', # Hide the default x-axis
cex.main = 0.8, # Decrease the size of the main title
cex.lab = 0.7, # Decrease the size of the axis labels
las = 1 # Make y-axis labels horizontal
)
axis(1, at = seq(-4, 4, by = 2),
labels = seq(-4, 4, by = 2),
cex.axis = 0.7, #Make the axis annotation smaller
tck = -0.015 # Make tick marks shorter
)
# Add a line along the x-axis from -6 to -5
segments(x0 = -6, y0 = 0, x1 = -5, y1 = 0, col = "black", lwd = 1)
# Plot the second histogram for Loneliness Difference Scores
hist(data2$Lonely_Diff,
main = "Distribution of Loneliness Scores",
xlab = "Loneliness Difference Score (T2 - T1)",
ylab = "Frequency",
ylim = c(0, 50),
xlim = c(-2, 2),
xaxs = "i", # Connect x-axis to y-axis
yaxs = "i", # Connect y-axis to x-axis
breaks = 28, # Increase the number of bars
col = "gray",
tck = -0.015, # Make tick marks shorter
cex.axis = 0.7, # Make the axis annotation smaller
cex.main = 0.8, # Decrease the size of the main title
cex.lab = 0.7, # Decrease the size of the axis labels
las = 1 # Make y-axis labels horizontal
)

library(cowplot)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ✖ lubridate::stamp() masks cowplot::stamp()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# Load the data
data1 <- read.csv("./Study 1 data.csv")
data2 <- read.csv("./Study 2.csv")
# Calculate percentiles for Study 1
study1_extraversion_25 <- quantile(data1$EXTRAVERSION, 0.25)
study1_extraversion_75 <- quantile(data1$EXTRAVERSION, 0.75)
# Create "ExtraversionGroup" for Study 1
study1_data <- data1 %>%
mutate(ExtraversionGroup = case_when(
EXTRAVERSION <= study1_extraversion_25 ~ "Most Introverted",
EXTRAVERSION >= study1_extraversion_75 ~ "Most Extraverted",
TRUE ~ "Moderate"
)) %>%
filter(ExtraversionGroup != "Moderate")
# Reshape the Study 1 data to long format
study1_long <- study1_data %>%
pivot_longer(cols = starts_with("SCAVERAGE.T"),
names_to = "Time",
values_to = "SocialConnectedness") %>%
mutate(Time = recode(Time, "SCAVERAGE.T1" = "Before Pandemic", "SCAVERAGE.T2" = "During Pandemic"))
# Summarize Study 1 data
study1_summary <- study1_long %>%
group_by(ExtraversionGroup, Time) %>%
summarise(MeanConnectedness = mean(SocialConnectedness),
SE = sd(SocialConnectedness) / sqrt(n()),
CI_Lower = MeanConnectedness - qt(0.975, df=n()-1) * SE,
CI_Upper = MeanConnectedness + qt(0.975, df=n()-1) * SE,
.groups = 'drop')
# Calculate percentiles for Study 2
study2_extraversion_25 <- quantile(data2$T1Extraversion, 0.25)
study2_extraversion_75 <- quantile(data2$T1Extraversion, 0.75)
# Create "ExtraversionGroup" for Study 2
study2_data <- data2 %>%
mutate(ExtraversionGroup = case_when(
T1Extraversion <= study2_extraversion_25 ~ "Most Introverted",
T1Extraversion >= study2_extraversion_75 ~ "Most Extraverted",
TRUE ~ "Moderate"
)) %>%
filter(ExtraversionGroup != "Moderate")
# Reshape the Study 2 data to long format
study2_long <- study2_data %>%
pivot_longer(cols = c("T1Lonely", "T2Lonely"),
names_to = "Time",
values_to = "Loneliness") %>%
mutate(Time = recode(Time, "T1Lonely" = "Before Pandemic", "T2Lonely" = "During Pandemic"))
# Summarize Study 2 data
study2_summary <- study2_long %>%
group_by(ExtraversionGroup, Time) %>%
summarise(MeanLoneliness = mean(Loneliness),
SE = sd(Loneliness) / sqrt(n()),
CI_Lower = MeanLoneliness - qt(0.975, df=n()-1) * SE,
CI_Upper = MeanLoneliness + qt(0.975, df=n()-1) * SE,
.groups = 'drop')
# Create the left plot
p1 <- ggplot(study1_summary, aes(x = Time, y = MeanConnectedness, group = ExtraversionGroup, linetype = ExtraversionGroup)) +
geom_line(size = 1) +
geom_point(size = 3) +
geom_errorbar(aes(ymin = CI_Lower, ymax = CI_Upper), width = 0.2) +
labs(title = "Social Connectedness Changes
Based on Extraversion", x = "Time", y = "Mean Social Connectedness") +
scale_y_continuous(
limits = c(3.17, 5.1), # Set y-axis limits
breaks = seq(3.5, 5, by = 0.5)) + # Set tick marks
theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
# Create the right plot
p2 <- ggplot(study2_summary, aes(x = Time, y = MeanLoneliness, group = ExtraversionGroup, linetype = ExtraversionGroup)) +
geom_line(size = 1) +
geom_point(size = 3) +
geom_errorbar(aes(ymin = CI_Lower, ymax = CI_Upper), width = 0.2) +
labs(title = "Loneliness Changes
Based on Extraversion", x = "Time", y = "Mean Loneliness") +
scale_y_continuous(
limits = c(1.17, 3.1), # Set y-axis limits
breaks = seq(1.5, 3, by = 0.5)) + # Set tick marks
theme_minimal()
p1 <- p1 +
theme(
plot.title = element_text(size = 12, # Smaller title
hjust = 0.5), # Centralise the title
legend.position = "none", # Hide legend
axis.line = element_line(color = "grey", size = 1.1), # Stylise axis lines
axis.ticks = element_line(color = "grey", size = 1.1), # Stylise tick marks
panel.grid.major = element_blank(), # Remove major grid lines
panel.grid.minor = element_blank(), # Remove minor grid lines
)
## Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
p2 <- p2 +
theme(
plot.title = element_text(size = 12, # Smaller title
hjust = 0.5), # Centralise the title
legend.position = "right", # Place legend on the right
legend.title = element_blank(),
axis.line = element_line(color = "grey", size = 1.1), # Stylise axis lines
axis.ticks = element_line(color = "grey", size = 1.1), # Stylise tick marks
panel.grid.major = element_blank(), # Remove major grid lines
panel.grid.minor = element_blank() # Remove minor grid lines
)
# Arrange the plots side by side with adjusted widths
final_plot <- plot_grid(p1, p2, labels = NULL, ncol = 2, rel_widths = c(1, 1.61))
# Display the final combined plot
print(final_plot)
