# Load the necessary libraries
pacman::p_load(pacman, dplyr, tidyr, ggplot2, readr)

# Load the data
DVT_TH_Characteristics <- read_csv("DVT_TH_Characteristics.csv")
## Rows: 13 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Variable, DVT Group (n=36), Non-DVT Group (n=146)
## dbl (2): χ2/t, P-value
## 
## ℹ 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.
DVT_TH_Characteristics %>%
  select_if(is.factor) %>%
  summarise(across(everything(), ~ list(table(.))))
## # A tibble: 1 × 0
# Lollipop Chart for P-Values
ggplot(DVT_TH_Characteristics, aes(x = reorder(Variable, `P-value`), y = `P-value`)) +
  geom_point(color = "blue", size = 3) +
  geom_segment(aes(x = Variable, xend = Variable, y = 0, yend = `P-value`), color = "blue") +
  labs(title = "Significance of Differences between Groups (P-values)",
       x = "Characteristic",
       y = "P-value") +
  theme() +
  coord_flip()

# Bar plot with the odds ratios and their corresponding confidence intervals, to display the risk factors for DVT after THA
# Create the data 
data <- data.frame(
  RiskFactor = c("Age > 70 years", "Diabetes", "Duration of surgery ≥ 120 minutes",
                 "Cemented prosthesis", "Bilateral joint replacements", 
                 "BMI ≥ 28 kg/m²", "Duration of days in bed > 3 days"),
  OR = c(4.406, 3.949, 3.081, 2.435, 2.272, 2.275, 1.566),
  CI_lower = c(1.744, 1.284, 1.293, 1.104, 1.402, 1.181, 1.182),
  CI_upper = c(6.134, 5.279, 5.308, 4.315, 4.423, 4.531, 1.994),
  p_value = c(0.016, 0.007, 0.026, 0.042, 0.033, 0.023, 0.037)
)

# Plotting
ggplot(data, aes(x = reorder(RiskFactor, OR), y = OR)) +
  geom_bar(stat = "identity", fill = "skyblue") +
  geom_errorbar(aes(ymin = CI_lower, ymax = CI_upper), width = 0.2) +
  coord_flip() +  # Flip the coordinates for better readability
  labs(title = "Risk Factors for DVT after THA",
       x = "Risk Factor",
       y = "Odds Ratio (OR)",
       caption = "Data Source: Logistic Regression Analysis") +
  theme() +
  theme(plot.title = element_text(hjust = 0.5))