# download the packages
library(ggplot2)
library(dplyr)

# load the data of the csv files 
data <- read.csv("Study 1 data.csv")

# Ensure the data is loaded correctly (display first few rows)
head(data)
##   Participant.ID Gender Age LETHAVERAGE.T1 LETHAVERAGE.T2 LethDiff SCAVERAGE.T1
## 1              1  Woman  25            4.8            5.7      0.9      3.21053
## 2              2  Woman  20            3.4            5.0      1.6      4.00000
## 3              3    Man  23            1.8            2.4      0.6      4.94737
## 4              4    Man  26            1.2            5.4      4.2      4.47368
## 5              5  Woman  19            1.9            3.8      1.9      3.68421
## 6              6  Woman  22            5.1            5.8      0.7      1.84211
##   SCAVERAGE.T2   SCdiff EXTRAVERSION SPANE.P SPANE.N SPANE.B SocialDistancing
## 1      2.94737 -0.26316      6.25000       8      27     -19                1
## 2      4.47368  0.47368      5.25000      18      20      -2                1
## 3      4.00000 -0.94737      4.25000      18      15       3                1
## 4      2.36842 -2.10526      2.66667      17      25      -8                1
## 5      3.84211  0.15790      2.91667      18      15       3                1
## 6      2.21053  0.36842      4.16667      16      26     -10                1
##   SixFeet
## 1       0
## 2       3
## 3       0
## 4       3
## 5       2
## 6       0
# Create the histogram. aes(x = SCdiff) specifies that we are plotting the SCdiff variable on the x-axis.
plot1 <- #Problem: didn't match the original data pattern
  ggplot(data, aes(x = SCdiff)) +
  
  # Change the column width and colour
  geom_histogram(binwidth = 0.5, fill = "grey", color = "black") +
  
  # Customise plot with titles and x and y labels.
  labs(title = "Distribution of Social Connectedness Difference Scores",
       x = "Social Connectedness Difference Score (T2 - T1)",
       y = "Frequency") +
  
  # Troubleshooting: the 3 on the left side of the x-axis didn't upload so I customize x-axis ticks in a different way. 
  scale_x_continuous(breaks = seq(-3, 3, by = 1), limits = c(-3, 3)) +  # Customize x-axis ticks to include -3 to 3
  scale_y_continuous(breaks = seq(0, 200, by = 50)) +  # Customize y-axis ticks to include 0, 50, 100, 150
  
  # trouble shooting: the above code didn't add 150 so I used ylim() to set the y-axis limits manually.
  ylim(0, 150) +  # Set y-axis limits
  
  # APA style guidelines, use theme_minimal() function
  # adjust font sizes and plot elements for clarity. 
  theme_minimal(base_size = 15) +
  theme(plot.title = element_text(hjust = 0.5),
        
        # to remove the grey background grid lines 
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.major.y = element_blank(),
        panel.grid.minor.y = element_blank())

print(plot1) 

plot2 <-
# Janes plot. Problem: the y axis limit needs to extend longer
  hist(data$SCdiff,
       main = "Distribution of Social Connectedness Score", # Title of the histogram
       font.main = 1, #Unbolds the defaut bold title: '1' is for plain text 
       xlab = "Social Connectedness Difference Score (T2-T1)",  # Label for the x-axis
       ylim = c(0,180),  
       xlim=c(-3,3), 
       xaxs = "i", # Connect x-axis to y-axis
       yaxs = "i" # Connect y-axis to x-axis
  )

# extended y axis limit
axis(2, at = seq(0, 200, by = 50)) 

print(plot2)
## $breaks
##  [1] -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5  0.0  0.5  1.0  1.5  2.0  2.5
## 
## $counts
##  [1]   2   1   8   3  31  84 154 113  50  15   5   1
## 
## $density
##  [1] 0.008565310 0.004282655 0.034261242 0.012847966 0.132762313 0.359743041
##  [7] 0.659528908 0.483940043 0.214132762 0.064239829 0.021413276 0.004282655
## 
## $mids
##  [1] -3.25 -2.75 -2.25 -1.75 -1.25 -0.75 -0.25  0.25  0.75  1.25  1.75  2.25
## 
## $xname
## [1] "data$SCdiff"
## 
## $equidist
## [1] TRUE
## 
## attr(,"class")
## [1] "histogram"
plot3 <- #change y axis numbers from vertical to horizontal 
hist(data$SCdiff,
     main = "Distribution of Social Connectedness Score", # Title of the histogram
     font.main = 1, # Unbolds the default bold title: '1' is for plain text
     xlab = "Social Connectedness Difference Score (T2-T1)",  # Label for the x-axis
     ylim = c(0,180),  
     xlim = c(-3,3), 
     xaxs = "i", # Connect x-axis to y-axis
     yaxs = "i", # Connect y-axis to x-axis
     las = 1 # Make y-axis labels horizontal
) 

# Customize the y-axis, formatting according to APA guidelines and removing the tick at 0
axis(2, at = seq(50, 200, by = 50), labels = seq(50, 200, by = 50), las = 1, tck = -0.02)

print(plot3)
## $breaks
##  [1] -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5  0.0  0.5  1.0  1.5  2.0  2.5
## 
## $counts
##  [1]   2   1   8   3  31  84 154 113  50  15   5   1
## 
## $density
##  [1] 0.008565310 0.004282655 0.034261242 0.012847966 0.132762313 0.359743041
##  [7] 0.659528908 0.483940043 0.214132762 0.064239829 0.021413276 0.004282655
## 
## $mids
##  [1] -3.25 -2.75 -2.25 -1.75 -1.25 -0.75 -0.25  0.25  0.75  1.25  1.75  2.25
## 
## $xname
## [1] "data$SCdiff"
## 
## $equidist
## [1] TRUE
## 
## attr(,"class")
## [1] "histogram"
plot4 <- #change y axis numbers from vertical to horizontal 
hist(data$SCdiff,
     main = "Distribution of Social Connectedness Score", # Title of the histogram
     font.main = 1, # Unbolds the default bold title: '1' is for plain text
     xlab = "Social Connectedness Difference Score (T2-T1)",  # Label for the x-axis
     ylim = c(0,180),  
     xlim = c(-3,3), 
     xaxs = "i", # Connect x-axis to y-axis
     yaxs = "i", # Connect y-axis to x-axis
     las = 1 # Make y-axis labels horizontal
) 

# Customize the y-axis, formatting according to APA guidelines and removing the tick at 0
axis(2, at = seq(50, 200, by = 50), labels = seq(50, 200, by = 50), las = 1, tck = -0.02)

# Draw a custom axis line without the tick at 0
axis(2, at = 0, labels = FALSE, tck = 0)

print(plot4)
## $breaks
##  [1] -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5  0.0  0.5  1.0  1.5  2.0  2.5
## 
## $counts
##  [1]   2   1   8   3  31  84 154 113  50  15   5   1
## 
## $density
##  [1] 0.008565310 0.004282655 0.034261242 0.012847966 0.132762313 0.359743041
##  [7] 0.659528908 0.483940043 0.214132762 0.064239829 0.021413276 0.004282655
## 
## $mids
##  [1] -3.25 -2.75 -2.25 -1.75 -1.25 -0.75 -0.25  0.25  0.75  1.25  1.75  2.25
## 
## $xname
## [1] "data$SCdiff"
## 
## $equidist
## [1] TRUE
## 
## attr(,"class")
## [1] "histogram"
plot5 <- #extend x-axis with a gap before -3
hist(data$SCdiff,
     main = "Distribution of Social Connectedness Score", # Title of the histogram
     font.main = 1, # Unbolds the default bold title: '1' is for plain text
     xlab = "Social Connectedness Difference Score (T2-T1)",  # Label for the x-axis
     ylim = c(0,180),  
     xlim = c(-3.5, 3), 
     xaxs = "i", # Connect x-axis to y-axis
     yaxs = "i", # Connect y-axis to x-axis
     las = 1 # Make y-axis labels horizontal
) 

# Customize the y-axis, formatting according to APA guidelines and removing the tick at 0
axis(2, at = seq(50, 200, by = 50), labels = seq(50, 200, by = 50), las = 1, tck = -0.02)
# Draw a custom axis line without the tick at 0
axis(2, at = 0, labels = FALSE, tck = 0)

# Customize the x-axis with a gap before -3
axis(1, at = seq(-3, 3, by = 1), labels = c("", -2, -1, 0, 1, 2, 3), tck = -0.02)

print(plot5)
## $breaks
##  [1] -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5  0.0  0.5  1.0  1.5  2.0  2.5
## 
## $counts
##  [1]   2   1   8   3  31  84 154 113  50  15   5   1
## 
## $density
##  [1] 0.008565310 0.004282655 0.034261242 0.012847966 0.132762313 0.359743041
##  [7] 0.659528908 0.483940043 0.214132762 0.064239829 0.021413276 0.004282655
## 
## $mids
##  [1] -3.25 -2.75 -2.25 -1.75 -1.25 -0.75 -0.25  0.25  0.75  1.25  1.75  2.25
## 
## $xname
## [1] "data$SCdiff"
## 
## $equidist
## [1] TRUE
## 
## attr(,"class")
## [1] "histogram"
plot6 <- # Missing section of black line on x axis Add a black line along the x-axis
hist(data$SCdiff,
     main = "Distribution of Social Connectedness Score", # Title of the histogram
     font.main = 1, # Unbolds the default bold title: '1' is for plain text
     xlab = "Social Connectedness Difference Score (T2-T1)",  # Label for the x-axis
     ylim = c(0,180),  
     xlim = c(-3.5, 3), 
     xaxs = "i", # Connect x-axis to y-axis
     yaxs = "i", # Connect y-axis to x-axis
     las = 1 # Make y-axis labels horizontal
) 

# Customize the y-axis, formatting according to APA guidelines and removing the tick at 0
axis(2, at = seq(50, 200, by = 50), labels = seq(50, 200, by = 50), las = 1, tck = -0.02)

# Customize the x-axis with a gap before -3
axis(1, at = seq(-3, 3, by = 1), labels = c("", -2, -1, 0, 1, 2, 3), tck = -0.02)

# Add a black line along the x-axis
abline(h = 0, col = "black", lwd = 2)

print(plot6)
## $breaks
##  [1] -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5  0.0  0.5  1.0  1.5  2.0  2.5
## 
## $counts
##  [1]   2   1   8   3  31  84 154 113  50  15   5   1
## 
## $density
##  [1] 0.008565310 0.004282655 0.034261242 0.012847966 0.132762313 0.359743041
##  [7] 0.659528908 0.483940043 0.214132762 0.064239829 0.021413276 0.004282655
## 
## $mids
##  [1] -3.25 -2.75 -2.25 -1.75 -1.25 -0.75 -0.25  0.25  0.75  1.25  1.75  2.25
## 
## $xname
## [1] "data$SCdiff"
## 
## $equidist
## [1] TRUE
## 
## attr(,"class")
## [1] "histogram"