# Load necessary libraries
library(WrightMap)
library(ggplot2)
library(RColorBrewer)
library(rstudioapi)
library(eRm)

# Get the directory where the current R script is located
script_dir <- dirname(rstudioapi::getActiveDocumentContext()$path)

# Set the working directory to the script's directory
setwd(script_dir)

# Specify the file name (replace 'your_file_name.extension' with the actual file name)
file_name <- "WrightMapTRTAM.csv"

# Read the file (adjust the function based on the file type, e.g., read.csv, read.table, etc.)
data <- read.csv(file_name, sep=",", stringsAsFactors=FALSE)
# For other file types, use the appropriate function, e.g., read.table, readRDS, etc.

# Display the first few rows of the data
head(data)
##   item_name item_difficulty Proportion_Correct  id ability ability_se
## 1      AABB           -1.58          0.7663551 P15    2.45       1.48
## 2       ABB           -1.79          0.7943925 P16    2.45       1.48
## 3       ABC           -0.94          0.6635514 P17    1.29       0.88
## 4      ABC2           -1.46          0.7476636 P18    1.29       0.88
## 5      ABCD           -0.57          0.5981308 P19    1.29       0.88
## 6       AAB           -1.72          0.7850467  P8    2.45       1.48
# Extract item difficulties and abilities
item_difficulties <- data$item_difficulty[1:19]
abilities <- data$ability

# Extract item names from the first 31 rows (assuming they belong to items)
item_names <- data$item_name[1:19]

item_shapes <- c(rep(1, 5), rep(2, 7), rep(0, 7))
dim_colors <- c(rep("red", 5), rep("blue", 7), rep("green", 7))
label_colors <- c("red","blue", "green")  # ID Pattern = red, Missing = orange, Extend = blue, Abstract = green, ID Unit = purple
dim_labels <- c("ID Pattern", "Extend", "Abstract")

# Generate the Wright Map
cat("Saving Wright Map to:", "wright_map_TR.png", "\n")
## Saving Wright Map to: wright_map_TR.png
png("wright_map_TR.png", width = 3500, height = 2250, res = 400)

wrightMap(
  thetas = abilities,
  item_difficulties,
  item.labels = item_names,      # Use simplified labels here
  item.prop = 0.8,
  thr.lab.text = item_names,
  thr.lab.cex = 0.6,
  thr.lab.pos = 1,   # defines where the label shown according to symbol
  thr.sym.pch = item_shapes,
#  dim.names = dim_labels,
  dim.names = "Density Plot",
  dim.color = label_colors,
  show.thr.lab = TRUE, # Use to show labels on data points
  thr.sym.col.fg = dim_colors,
  thr.sym.col.bg = dim_colors,
  thr.sym.cex = 1.2,
  cex.axis = 1,
  cex.lab = 1,
  person.side = personDens,
  label.items.row = 0,
  main.title = NULL,   # The title dyanmically created below
)
##        [,1]
##  [1,] -1.58
##  [2,] -1.79
##  [3,] -0.94
##  [4,] -1.46
##  [5,] -0.57
##  [6,] -1.72
##  [7,] -2.16
##  [8,] -1.79
##  [9,] -1.40
## [10,] -1.58
## [11,] -1.40
## [12,] -0.62
## [13,] -0.88
## [14,] -1.52
## [15,] -1.58
## [16,] -0.94
## [17,] -1.52
## [18,] -0.47
## [19,] -1.22
# Print plot boundaries for debugging
print(par("usr"))  # Print x_min, x_max, y_min, y_max
## [1] -0.04  1.04 -0.04  1.04
# Identify plot boundaries
plot_limits <- par("usr")  # Returns c(x_min, x_max, y_min, y_max)

# Add the legend outside the plot
par(xpd = TRUE)  # Allow plotting outside the clipping region

# Add a Legend
legend(
      x = mean(par("usr")[1:2])-0.1,      # Center horizontally
      y = par("usr")[3],        # Place below the plot area
      legend = dim_labels, 
      pch = c(1,2,0), 
      col = c("red", "blue", "green"),
      horiz = TRUE,                   # Make legend horizontal
      title = "Item Types", 
      cex = 0.8)

#Add a dynamic title
title(main = "Wright Map for TR Data", line = 2, cex.main = 1)  # Adjust line and size

dev.off()
## quartz_off_screen 
##                 2