rm(list = ls())
###############################input data_1
dir_path <- "C:\\Users\\liyix\\OneDrive\\Desktop\\bar\\"
dir_path_name <- dir(dir_path,pattern = "*.csv")
dir_path_name
## [1] "2019-07-01-irri_sensi_stat.csv" "stat_chart_0.csv"
###############################bath input data
object_file_list <- list()
for (i in 1:length(dir_path_name)) {
dir_path_file <- paste0(dir_path,dir_path_name[i])
object_file_list[[i]] <- read.csv(dir_path_file,header = T,stringsAsFactors = F)
names(object_file_list)[i] <- dir_path_name[i]
print(colnames(object_file_list[[i]]))
}
## [1] "Var1" "Freq" "Category"
## [1] "X" "Var1" "Freq" "Category"
object_file_list[[2]] <- object_file_list[[2]][,-1]
data_1 <- rbind(object_file_list[[1]],object_file_list[[2]])
data_1
## Var1 Freq Category
## 1 0 38 Sensitization
## 2 1 141 Sensitization
## 3 0 37 Irritation
## 4 1 119 Irritation
## 5 0 1243 carcinogenicity
## 6 1 341 carcinogenicity
## 7 0 752 cardiotoxicity
## 8 1 832 cardiotoxicity
## 9 0 804 developmental.toxicity
## 10 1 780 developmental.toxicity
## 11 0 826 hepatotoxicity
## 12 1 758 hepatotoxicity
## 13 0 822 nephrotoxicity
## 14 1 762 nephrotoxicity
## 15 0 545 neurotoxicity
## 16 1 1039 neurotoxicity
## 17 0 959 reproductive.toxicity
## 18 1 625 reproductive.toxicity
## 19 0 392 skin.toxicity
## 20 1 1192 skin.toxicity
data_2 <- data_1[which(data_1$Category != "skin.toxicity"),]
data_2$Category <- tolower(data_2$Category)
data_2$Category <- gsub("\\.","",data_2$Category)
table(data_2$Category)
##
## carcinogenicity cardiotoxicity developmentaltoxicity
## 2 2 2
## hepatotoxicity irritation nephrotoxicity
## 2 2 2
## neurotoxicity reproductivetoxicity sensitization
## 2 2 2
data_2$Category <- gsub("sensitization","skin sensitization",data_2$Category)
data_2$Category <- gsub("irritation","skin irritation",data_2$Category)
#View(data_4)
#plot
#data_3 <- data_2[grep("skin",data_2$Category),]
#data_4 <- data_2[-grep("skin",data_2$Category),]
library(ggplot2)
#
str(data_2)
## 'data.frame': 18 obs. of 3 variables:
## $ Var1 : int 0 1 0 1 0 1 0 1 0 1 ...
## $ Freq : int 38 141 37 119 1243 341 752 832 804 780 ...
## $ Category: chr "skin sensitization" "skin sensitization" "skin irritation" "skin irritation" ...
#data_2$Freq[data_2$Var1 == "0"] <- -1*data_2$Freq[data_2$Var1 == "0"]
data_3 <- data_2
#View(data_3)
data_3[grep("0",data_3$Var1),]$Freq <- -1*data_3[grep("0",data_3$Var1),]$Freq
max(data_3$Freq) #[1] 1039
## [1] 1039
min(data_3$Freq) #[1] -1243
## [1] -1243
ggplot(data_3, aes(x = Category, y = Freq, fill = factor(Var1), label = Freq)) + xlab("")+ ylab("Number of chemicals")+
geom_bar(stat = "identity", width = 0.8) +
theme(plot.title = element_text(hjust = 0.5),
axis.text.x = element_text(angle = 0, hjust = 0.5,vjust = 0.5,size = 13,color = "black"),
axis.text.y = element_text(angle = 0, hjust = 0,vjust = 0.5,size = 13,color = "black"),
legend.background = element_rect(fill="gray95", size=.5, linetype="dotted"),
panel.border = element_rect(colour = "black", fill=NA, size=0.5),
panel.grid.major.x = element_line(size = 0.5, linetype = 'dashed',
colour = "gray80"),
axis.title.x = element_text(color="black", size=13, face="plain"),
panel.background = element_blank(),
axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = "top", legend.box = "horizontal",
legend.text = element_text(colour="black", size=13,
face="plain"),
legend.title = element_text(colour="black", size=13,
face="plain")) +
scale_y_continuous(expand = c(0,0),limits = c(-1300,1100),
breaks = c(-1000,-500,0,500,1000),
labels = c("1000","500","0","500","1000")) +
coord_flip() +
scale_fill_brewer(palette="Accent",name="Category",
labels=c("Nontoxic", "Toxic"))

##################################################
library(ggpol)
## Warning: package 'ggpol' was built under R version 4.0.5
#install.packages("ggpol")
head(data_3)
## Var1 Freq Category
## 1 0 -38 skin sensitization
## 2 1 141 skin sensitization
## 3 0 -37 skin irritation
## 4 1 119 skin irritation
## 5 0 -1243 carcinogenicity
## 6 1 341 carcinogenicity
str(data_3)
## 'data.frame': 18 obs. of 3 variables:
## $ Var1 : int 0 1 0 1 0 1 0 1 0 1 ...
## $ Freq : num -38 141 -37 119 -1243 ...
## $ Category: chr "skin sensitization" "skin sensitization" "skin irritation" "skin irritation" ...
data_4 <- data_3
ggplot(data_4, aes(x = Category, y = Freq, fill = factor(Var1), label = Freq))+
geom_bar(stat = "identity") +
facet_share(~Var1, dir = "h", scales = "free", reverse_num = F) +
coord_flip(clip = "on") +
scale_color_manual(name = "", values = c(male = "#3E606F", female = "#8C3F4D"))+
#theme_minimal()+
theme(text = element_text(color = "#3A3F4A"),
panel.grid.major.y = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major.x = element_line(linetype = "dotted", size = 0.3, color = "#3A3F4A"),
axis.title = element_blank(),
axis.text.x = element_text(size = 12, color = "#5D646F"),
axis.text.y = element_text(size = 12, color = "#5D646F"),
strip.text = element_text(color = "#5D646F", size = 18, face = "bold", hjust = 0.030),
legend.position = "none")

############################ouput data and plot
ggsave(filename = paste0(Sys.Date(),"-expect_skin_2.tif"), plot = last_plot(),
device = "tiff", path = dir_path,
scale = 1, width = 20, height = 16, units = "cm",dpi = 300,
limitsize = TRUE, compression = "lzw")