# load packages 
library(tidyverse) # used to clean, manipulate and visualise data 
## ── 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()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(yarrr) # for plot
## Loading required package: jpeg
## Loading required package: BayesFactor
## Loading required package: coda
## Loading required package: Matrix
## 
## Attaching package: 'Matrix'
## 
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
## 
## ************
## Welcome to BayesFactor 0.9.12-4.7. If you have questions, please contact Richard Morey (richarddmorey@gmail.com).
## 
## Type BFManual() to open the manual.
## ************
## Loading required package: circlize
## ========================================
## circlize version 0.4.16
## CRAN page: https://cran.r-project.org/package=circlize
## Github page: https://github.com/jokergoo/circlize
## Documentation: https://jokergoo.github.io/circlize_book/book/
## 
## If you use it in published research, please cite:
## Gu, Z. circlize implements and enhances circular visualization
##   in R. Bioinformatics 2014.
## 
## This message can be suppressed by:
##   suppressPackageStartupMessages(library(circlize))
## ========================================
## 
## yarrr v0.1.5. Citation info at citation('yarrr'). Package guide at yarrr.guide()
## Email me at Nathaniel.D.Phillips.is@gmail.com
## 
## Attaching package: 'yarrr'
## 
## The following object is masked from 'package:ggplot2':
## 
##     diamonds
# Read the raw data file 
data8 <- read_csv(file = "/cloud/project/Study 8 data.csv")
## Rows: 373 Columns: 340
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (340): StartDate, EndDate, Status, IPAddress, Progress, Duration (in sec...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Remove row one and two for clarity 
data8 <- data8[-c(1, 2), ] 

# Apply exclusion critera 
# remove participants who responded twice and keep only first response
# Use Prolific_PID variable 
duplicates <- data8 %>%
  count(Prolific_PID) %>%
  filter(n > 1) %>%
  pull(Prolific_PID) # Will identify P's that appear more than once
data8 <- data8 %>%
  group_by(Prolific_PID) %>%
  slice(1) %>%
  ungroup() # Keep only the first occurrence that appears
# Remove participants who did not consent 
data8 <- data8 %>%
  filter(Consent == 1, na.rm = TRUE)
# remove participants who were not serious 
data8 <- data8 %>%
  filter(Serious_check == 1, na.rm = TRUE)
# remove participants who did not complete 
data8 <- data8 %>%
  filter(Finished == 1, na.rm = TRUE)
# remove participants who failed attention check 
data8 <- data8 %>%
  filter(SC0 >= 4)

# rename condition variable 
colnames(data8)[colnames(data8)=="FL_10_DO"] <- "condition"

# make dataframe for FIGURE 2 HISTOGRAM 
figure2 <- data8 %>% 
  group_by(condition, advancement) %>% 
  summarise(n=n())
## `summarise()` has grouped output by 'condition'. You can override using the
## `.groups` argument.
# recode condition titles 
figure2 <- figure2 %>% 
  mutate(condition = recode(condition, 
                            'Block_1_Generic_Conflict' = 'Conflicting/Generic',
                            'Block_3_Qualified_Conflict' = 'Conflicting/Qualified',
                            'Block_2_Generic_Consistent' = 'Non-conflicing/Generic',
                            'Block_4_Qualified_Consistent' = 'Non-conflicting/Qualified'))

# recode advancement titles 
figure2 <- figure2 %>% 
  mutate(advancement = recode(advancement, 
                            '-1' = 'Less',
                            '0' = 'Same',
                            '1' = 'More'))

# Set the factor levels to ensure correct order
figure2$advancement <- factor(figure2$advancement, levels = c("Less", "Same", "More"))

# Plot the histogram
plot <- ggplot(figure2, aes(x = advancement, y = n, fill = condition)) +
  geom_bar(stat = "identity", position = "dodge") +
  scale_fill_manual(values = c("#333333", "#818181", "#ababab", "#cccccc")) +
  labs(x = "Advancement", y = "Number of Participants", fill = "Condition") +
  theme(axis.title = element_text(size = 7), # Adjust axis titles size
        axis.text = element_text(size = 6),   # Adjust axis text size
        legend.title = element_text(size = 7), # Adjust legend title size
        legend.text = element_text(size = 6))   # Adjust legend text size
print(plot)

# FIGURE 1 PLOT 

#sum the contradiction scores  
#create a new variable called 'contradiction' that is the sum of the six contradition ratings 

# Convert columns to numeric (since they are not already numeric)
data8[, c("contradiction_1", "contradiction_2", "contradiction_3", "contradiction_4", "contradiction_5", "contradiction_6")] <- lapply(data8[, c("contradiction_1", "contradiction_2", "contradiction_3", "contradiction_4", "contradiction_5", "contradiction_6")], as.numeric)
# Calculate row sums
data8$contradiction <- rowSums(data8[, c("contradiction_1", "contradiction_2", "contradiction_3", "contradiction_4", "contradiction_5", "contradiction_6")], na.rm = TRUE)

# make dataframe for FIGURE 1 PLOT 
figure1 <- data8 %>% 
  group_by(contradiction, condition, advancement, confusion) %>% 
  summarise(n=n())
## `summarise()` has grouped output by 'contradiction', 'condition',
## 'advancement'. You can override using the `.groups` argument.
# seperate condition coloumn and rename 
figure1 <- figure1 %>%
  separate(col = condition, into = c("block", "number", "Format", "Conflict")) %>%
  mutate(Conflict = ifelse(Conflict == 'Conflict', 'Conf.', 
                           ifelse(Conflict == 'Consistent', 'Non.Conf.', Conflict)))

# fix non-numeric variables 
figure1$advancement <- as.numeric(figure1$advancement)
figure1 <- na.omit(figure1)

figure1$confusion <- as.numeric(figure1$confusion)
figure1 <- na.omit(figure1)

# Set up the plotting area
par(mfrow = c(2, 2))  # 2 rows, 2 columns

# First plot: Contradiction
pirateplot(formula = contradiction ~ Conflict * Format, 
           data = figure1, 
           inf.method = 'ci', 
           yaxt = "n", 
           ylim = c(0, 30), 
           theme = 1, 
           main = "Contradiction", 
           ylab = "Perceived Contradiction", 
           cex.names = 0.75, 
           cex.lab = 0.9)
axis(2, at = seq(from = 0, to = 30, by = 10))

# Second plot: Advancement
pirateplot(formula = advancement ~ Conflict * Format, 
           data = figure1, 
           inf.method = 'ci', 
           yaxt = "n", 
           theme = 1, 
           main = "Advancement", 
           ylab = "Perceived Scientific Advancement", 
           cex.names = 0.75, 
           cex.lab = 0.9)
axis(2, at = seq(from = -1, to = 1, by = 1))

# Third plot: Confusion
pirateplot(formula = confusion ~ Conflict * Format, 
           data = figure1, 
           inf.method = 'ci', 
           yaxt = "n", 
           ylim = c(1, 5), 
           theme = 1, 
           main = "Confusion", 
           ylab = "Perceived Confusion", 
           cex.names = 0.75, 
           cex.lab = 0.9)
axis(2, at = seq(from = 1, to = 5, by = 1))

# Leave the fourth plot empty
plot.new()