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

# 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 <- "WrightMapUS.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  id ability ability_se
## 1       ABB           -1.28 233    2.04       0.64
## 2       ABC           -1.23 168    1.78       0.64
## 3      AABB           -0.98 189    1.78       0.64
## 4      ABC2           -0.91 191    1.78       0.64
## 5      ABCD           -0.75 225    1.65       0.61
## 6        AB           -2.51 231    1.65       0.61
# Load the data

# Extract item difficulties and abilities
item_difficulties <- data$item_difficulty[1:31]
abilities <- data$ability

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

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

# Generate the Wright Map
cat("Saving Wright Map to:", "wright_map_US.png", "\n")
## Saving Wright Map to: wright_map_US.png
png("wright_map_US.png", width = 7000, height = 3500, 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.70,
  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,
  cex.axis = 1,
  cex.lab = 1,
  person.side = personDens,
  label.items.row = 0,
  main.title = NULL,   # The title dyanmically created below
)
##        [,1]
##  [1,] -1.28
##  [2,] -1.23
##  [3,] -0.98
##  [4,] -0.91
##  [5,] -0.75
##  [6,] -2.51
##  [7,] -2.40
##  [8,] -1.77
##  [9,] -1.76
## [10,] -1.44
## [11,] -1.17
## [12,] -1.06
## [13,] -0.89
## [14,] -0.80
## [15,] -0.66
## [16,] -0.63
## [17,] -0.44
## [18,] -0.44
## [19,] -0.85
## [20,] -0.71
## [21,] -0.67
## [22,] -0.56
## [23,] -0.53
## [24,] -0.49
## [25,] -0.14
## [26,] -0.05
## [27,]  0.01
## [28,]  0.08
## [29,]  1.35
## [30,]  1.51
## [31,]  1.68
# 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,3,2,0,4), 
      col = c("red","orange", "blue", "green", "purple"),
      horiz = TRUE,                   # Make legend horizontal
      title = "Item Types", 
      cex = 0.8)

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

dev.off()
## quartz_off_screen 
##                 2