# Load required packages
packages <- c("ggplot2", "tidyverse", "dplyr", "ggpubr", "see")
lapply(packages, function(x) if (!require(x, character.only = TRUE)) install.packages(x))
## Loading required package: ggplot2
## Loading required package: tidyverse
## Warning: package 'lubridate' was built under R version 4.3.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ lubridate 1.9.4     ✔ tibble    3.2.1
## ✔ purrr     1.0.2     ✔ tidyr     1.3.1
## ── 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
## Loading required package: ggpubr
## 
## Loading required package: see
## Warning: package 'see' was built under R version 4.3.3
## [[1]]
## NULL
## 
## [[2]]
## NULL
## 
## [[3]]
## NULL
## 
## [[4]]
## NULL
## 
## [[5]]
## NULL
library(ggplot2)
library(tidyverse)
library(dplyr)
library(ggpubr)
library(see)

CAM<-read.csv("Violin_Plot_Data.csv")
## Warning in read.table(file = file, header = header, sep = sep, quote = quote, :
## incomplete final line found by readTableHeader on 'Violin_Plot_Data.csv'
print(CAM)
##         F1Performance  Repeat1  Repeat2  Repeat3  Repeat4  Repeat5  Repeat6
## 1  SVMWithGradCAMMaps 0.670051 0.701571 0.680628 0.710660 0.648649 0.715686
## 2 SVMWithDeepShapMaps 0.673913 0.610390 0.630872 0.618357 0.662577 0.608696
##    Repeat7  Repeat8  Repeat9 Repeat10 Repeat11 Repeat12 Repeat13 Repeat14
## 1 0.713568 0.684932 0.699029 0.687500 0.720812 0.716418 0.666667 0.683417
## 2 0.623529 0.642857 0.607477 0.645833 0.631579 0.660099 0.662420 0.610778
##   Repeat15 Repeat16 Repeat17 Repeat18 Repeat19 Repeat20
## 1 0.666667 0.663317 0.691943 0.680412 0.686869 0.686551
## 2 0.701754 0.659091 0.577540 0.666667 0.678571 0.596685
head(CAM)
##         F1Performance  Repeat1  Repeat2  Repeat3  Repeat4  Repeat5  Repeat6
## 1  SVMWithGradCAMMaps 0.670051 0.701571 0.680628 0.710660 0.648649 0.715686
## 2 SVMWithDeepShapMaps 0.673913 0.610390 0.630872 0.618357 0.662577 0.608696
##    Repeat7  Repeat8  Repeat9 Repeat10 Repeat11 Repeat12 Repeat13 Repeat14
## 1 0.713568 0.684932 0.699029 0.687500 0.720812 0.716418 0.666667 0.683417
## 2 0.623529 0.642857 0.607477 0.645833 0.631579 0.660099 0.662420 0.610778
##   Repeat15 Repeat16 Repeat17 Repeat18 Repeat19 Repeat20
## 1 0.666667 0.663317 0.691943 0.680412 0.686869 0.686551
## 2 0.701754 0.659091 0.577540 0.666667 0.678571 0.596685
data_long <- CAM %>%
  pivot_longer(
    cols = starts_with("Repeat"),
    names_to = "Repeat", 
    values_to = "values")
head(data_long)
## # A tibble: 6 × 3
##   F1Performance      Repeat  values
##   <chr>              <chr>    <dbl>
## 1 SVMWithGradCAMMaps Repeat1  0.670
## 2 SVMWithGradCAMMaps Repeat2  0.702
## 3 SVMWithGradCAMMaps Repeat3  0.681
## 4 SVMWithGradCAMMaps Repeat4  0.711
## 5 SVMWithGradCAMMaps Repeat5  0.649
## 6 SVMWithGradCAMMaps Repeat6  0.716
#Full violins
ggplot(data_long, aes(x = F1Performance, y = values, fill = F1Performance)) +
  geom_jitter(
    position = position_jitter(0.1),
    aes(color = F1Performance),
    size = 6,
    alpha = 0.8
  ) +
  geom_violin(
    size = 2,
    alpha = 0.5,
    draw_quantiles = c(0.25, 0.5, 0.75),
    quantile.size = 2
  ) +
  coord_flip() +
  scale_fill_manual(values = c("magenta4", "darkorange2")) +
  scale_color_manual(values = c("magenta4", "darkorange2")) +
  stat_summary(
    fun = median,
    geom = "point",
    shape = 21,
    size = 3,
    fill = "white",
    color = "black",
    stroke = 1.5
  ) +
  scale_y_continuous(
    limits = c(min(data_long$values), max(data_long$values)),
    breaks = seq(min(data_long$values), max(data_long$values), by = 0.02),
    labels = scales::number_format(accuracy = 0.02)
  ) +
  theme_minimal() +
  theme(
    legend.title = element_text(face = "bold", size = 14),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    axis.title.y = element_blank(),
    axis.line.x = element_line(size = 2, color = "black"),
    plot.title = element_text(hjust = 0.5, face = "bold"),
    panel.grid.major.y = element_blank(),
    panel.grid.minor.x = element_blank(),
    panel.grid.major.x = element_line(color = "grey", linetype = "dashed", size = 1.5),
    legend.position = "none"
  ) +
  geom_text(
    aes(x = "SVMWithGradCAMMaps", label = "SVM + GRAD-CAM++", y = 0.64),
    vjust = -4.5,
    color = "darkorange2",
    size = 4.5
  ) +
  geom_text(
    aes(x = "SVMWithDeepShapMaps", y = 0.6, label = "SVM + Deep SHAP"),
    vjust = -3.5,
    color = "magenta4",
    size = 4.5
  ) +
  ylab("F1") +
  ggtitle("Fig.7. Grad-CAM++ saliency maps capture unique predictive information.")
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning in geom_violin(size = 2, alpha = 0.5, draw_quantiles = c(0.25, 0.5, :
## Ignoring unknown parameters: `quantile.size`
## Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_point()`).

#Half Violins
ggplot(data_long, aes(x = F1Performance, y = values, fill = F1Performance)) +
  geom_jitter(
    position = position_jitter(0.1),
    aes(color = F1Performance),
    size = 6,
    alpha = 0.8
  ) +
  geom_violinhalf(
    size = 2,
    alpha = 0.5,
    draw_quantiles = c(0.25, 0.5, 0.75),
    quantile.size = 2
  ) +
  coord_flip() +
  scale_fill_manual(values = c("magenta4", "darkorange2")) +
  scale_color_manual(values = c("magenta4", "darkorange2")) +
  stat_summary(
    fun = median,
    geom = "point",
    shape = 21,
    size = 3,
    fill = "white",
    color = "black",
    stroke = 1.5
  ) +
  scale_y_continuous(
    limits = c(min(data_long$values), max(data_long$values)),
    breaks = seq(min(data_long$values), max(data_long$values), by = 0.02),
    labels = scales::number_format(accuracy = 0.02)
  ) +
  theme_minimal() +
  theme(
    legend.title = element_text(face = "bold", size = 14),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    axis.title.y = element_blank(),
    axis.line.x = element_line(size = 2, color = "black"),
    plot.title = element_text(hjust = 0.5, face = "bold"),
    panel.grid.major.y = element_blank(),
    panel.grid.minor.x = element_blank(),
    panel.grid.major.x = element_line(color = "grey", linetype = "dashed", size = 1.5),
    legend.position = "none"
  ) +
  geom_text(
    aes(
      x = "SVMWithGradCAMMaps",
      label = "SVM + GRAD-CAM++",
      y = 0.64
    ),
    vjust = -4.5,
    color = "darkorange2",
    size = 4.5
  ) +
  geom_text(
    aes(
      x = "SVMWithDeepShapMaps",
      y = 0.6,
      label = "SVM + Deep SHAP"
    ),
    vjust = -3.5,
    color = "magenta4",
    size = 4.5
  ) +
  ylab("F1") +
  ggtitle("Fig. 7. Grad-CAM++ saliency maps capture unique predictive information.")
## Warning in geom_violinhalf(size = 2, alpha = 0.5, draw_quantiles = c(0.25, : Ignoring unknown parameters: `size`, `draw_quantiles`, and `quantile.size`
## Removed 1 row containing missing values or values outside the scale range
## (`geom_point()`).

#Box and Violin
ggplot(data_long, aes(x = F1Performance, y = values, fill = F1Performance)) +
  geom_jitter(
    position = position_jitter(0.1),
    aes(color = F1Performance),
    size = 6,
    alpha = 0.8
  ) +
  geom_violinhalf(
    size = 2,
    alpha = 0.5,
    draw_quantiles = c(0.25, 0.5, 0.75),
    quantile.size = 2
  ) +
  geom_boxplot(
    aes(color = F1Performance),
    width = 0.4,
    alpha = 0.3,
    outlier.shape = NA
  ) +
  coord_flip() +
  scale_fill_manual(values = c("magenta4", "darkorange2")) +
  scale_color_manual(values = c("magenta4", "darkorange2")) +
  stat_summary(
    fun = median,
    geom = "point",
    shape = 21,
    size = 3,
    fill = "white",
    color = "black",
    stroke = 1.5
  ) +
  scale_y_continuous(
    limits = c(min(data_long$values), max(data_long$values)),
    breaks = seq(min(data_long$values), max(data_long$values), by = 0.02),
    labels = scales::number_format(accuracy = 0.02)
  ) +
  theme_minimal() +
  theme(
    legend.title = element_text(face = "bold", size = 14),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    axis.title.y = element_blank(),
    axis.line.x = element_line(size = 2, color = "black"),
    plot.title = element_text(hjust = 0.5, face = "bold"),
    panel.grid.major.y = element_blank(),
    panel.grid.minor.x = element_blank(),
    panel.grid.major.x = element_line(color = "grey", linetype = "dashed", size = 1.5),
    legend.position = "none"
  ) +
  geom_text(
    aes(x = "SVMWithGradCAMMaps", label = "SVM + GRAD-CAM++", y = 0.64),
    vjust = -4.5,
    color = "darkorange2",
    size = 4.5
  ) +
  geom_text(
    aes(x = "SVMWithDeepShapMaps", y = 0.6, label = "SVM + Deep SHAP"),
    vjust = -3.5,
    color = "magenta4",
    size = 4.5
  ) +
  ylab("F1") +
  ggtitle("Fig.7. Grad-CAM++ saliency maps capture unique predictive information.")
## Warning in geom_violinhalf(size = 2, alpha = 0.5, draw_quantiles = c(0.25, : Ignoring unknown parameters: `size`, `draw_quantiles`, and `quantile.size`
## Removed 1 row containing missing values or values outside the scale range
## (`geom_point()`).