MODULE 2:Practice Assignment

#Task 1: Scatter with Reg
data("USArrests")
head(USArrests)
##            Murder Assault UrbanPop Rape
## Alabama      13.2     236       58 21.2
## Alaska       10.0     263       48 44.5
## Arizona       8.1     294       80 31.0
## Arkansas      8.8     190       50 19.5
## California    9.0     276       91 40.6
## Colorado      7.9     204       78 38.7
?USArrests

library(ggplot2)

ggplot(USArrests, aes(x=Murder,y=Assault))+
  geom_point(color="#660000", size=2)+
  geom_smooth(aes(color="#660000"),method=lm)+
  ggtitle("Scatter Plot of Assault vs. Murder Rate")+
  ylab("Assault Rate")+
  xlab("Murder Rate")+
  theme_minimal()+
  theme(legend.position="none")

#Task 2: Line
library(dplyr)

##Mean
USArrests$state <- rownames(USArrests)

USArrests$AVGCrimeRate <- rowMeans(USArrests[, c("Murder", "Assault", "Rape")])

head(USArrests)
##            Murder Assault UrbanPop Rape      state AVGCrimeRate
## Alabama      13.2     236       58 21.2    Alabama     90.13333
## Alaska       10.0     263       48 44.5     Alaska    105.83333
## Arizona       8.1     294       80 31.0    Arizona    111.03333
## Arkansas      8.8     190       50 19.5   Arkansas     72.76667
## California    9.0     276       91 40.6 California    108.53333
## Colorado      7.9     204       78 38.7   Colorado     83.53333
##Line Plot
ggplot(data=USArrests, aes(x=state,y=AVGCrimeRate, group=1))+
  geom_line(size=1, color="black")+
  geom_point(color = "steelblue", size = 2.5, shape = 16)+
  ggtitle("Line Plot of Average Crime Rate by State")+
  xlab("State")+
  ylab("Average Crime Rate")+
  theme_classic()+
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

MODULE 3: Box and Violin

##downloading the data
CAM<-read.csv("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
##Installing new packages
packages<-c("ggplot2","readr","tidyverse","dplyr","ggpubr","see")
install.packages("ggplot2")
library(ggplot2)
library(readr)
library(tidyverse)
library(dplyr)
library(ggpubr)
install.packages("see")
library(see)

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
##Figure recreation
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.")

##Half-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)+
  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.")

#Box+violin Plot

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.")