(1) R packages install
install.packages("tidyverse")
library(tidyverse)
install.packages("ggrepel")
library(ggrepel)
install.packages("janitor")
library(janitor)
install.packages("plotly")
library(plotly)
(2) Import dataset
# import csv file
TSCA <- read_csv('TSCA.csv')
## Rows: 68640 Columns: 36
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (36): INPUT, FOUND_BY, DTXSID, PREFERRED_NAME, SMILES, MOLECULAR_FORMULA...
##
## ℹ 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.
(3) organized the dataset (similar to tutorial 1)
# %>% is called the forward pipe operator
TSCA <- TSCA %>%
janitor::clean_names() %>% # cleans names of a data frame
# rename columns
rename(log_KOW = octanol_water_partition_logp_opera_pred, log_KOA = octanol_air_partition_coeff_logkoa_opera_pred, BCF = bioconcentration_factor_opera_pred
) %>%
#convert character vectors into numeric vectors
mutate(log_KOW = as.numeric(log_KOW), log_KOA = as.numeric(log_KOA), BCF = as.numeric(BCF)
) %>%
# calculate log_KAW from log_KOW and log_KOA
mutate(log_KAW = log_KOW - log_KOA) %>%
drop_na(log_KOW) #remove the NA value from the columns
## Warning: There were 3 warnings in `mutate()`.
## The first warning was:
## ℹ In argument: `log_KOW = as.numeric(log_KOW)`.
## Caused by warning:
## ! NAs introduced by coercion
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 2 remaining warnings.
(4) making scatter plot
#making plot with following components, log KOW as the y-axis, log KOA as the x-axis, log BCF as the colour gradient, high-contrast gradient colours which make any trends easy to see, and properly labelled axes and legend
ggplot(data = TSCA, mapping = aes(x = log_KOA, y = log_KOW, colour = log(BCF)))+
geom_point(alpha = 0.5)+
labs(x = "log KOA", y = "log KOW", colour = "logBCF")+ #axis labels
scale_colour_gradientn(colours=rainbow(8))+
theme_classic()

(5) adding lines on scatter plot
#To add a line on the plot, you need to make a data frame first.
org.bound <- tibble(log_KOA = c(7,7,7,10,14), log_KAW = c(15,5,-2,-5,-10))
air.bound <- tibble(log_KOA = c(-5,0,5,5,5), log_KAW = c(-1,-1,-1,10,15))
water.bound <- tibble(log_KOA = c(-5,0,6,8,12), log_KAW = c(-4,-4,-4,-6,-10))
acbap <- tibble(log_KOA = c(6,6,10.5,12.5,10,8, 6), log_KAW = c(1,-3,-7,-7,-3,1,1))
#add the created data frames into scatter plot
ggplot(data = TSCA, mapping = aes(x = log_KOA, y = log_KAW, colour = log(BCF)))+
geom_point(alpha = 0.5) + #Converts plot to a scatter plot
geom_path(data = org.bound, inherit.aes = FALSE, linetype = "longdash", size = 1, mapping = aes(x = log_KOA, y = log_KAW))+
geom_path(data = air.bound, inherit.aes = FALSE, linetype = "longdash", size = 1,mapping = aes(x = log_KOA, y = log_KAW))+
geom_path(data = water.bound, inherit.aes = FALSE, linetype = "longdash", size = 1,mapping = aes(x = log_KOA, y = log_KAW))+
geom_path(data = acbap, inherit.aes = FALSE, linetype = "solid", size = 1,mapping = aes(x = log_KOA, y = log_KAW))+
labs(x = 'logKOA',y = 'logKAW')+ #axis labels
labs(colour = 'logBCF')+ #legend label
scale_colour_gradientn(colours=rainbow(8))+
annotate(geom = "text", x = -3, y = -12, label = "Water")+
annotate(geom = "text", x = -3, y = 10, label = "Air")+
annotate(geom = "text", x = 10, y = 10, label = "Organics")+
annotate(geom = "text", x = 12, y = -2, label = "ACBAP Region")+
theme_classic() #Simplifies layout and removes gridlines
## 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.

(6) Adding PCBs on the scatter plot
# to add PCBs on the plot, you need to import the PCBs data set from **Assignment 1**
PCBs <- read.csv("compToxPCB.csv") #Read file
#regenerate the scatter plot and adding the PCBs data set
ggplot(data = TSCA, mapping = aes(x = log_KOA, y = log_KAW, colour = log(BCF)))+
geom_point(alpha = 0.5) + #Converts plot to a scatter plot
geom_path(data = org.bound, inherit.aes = FALSE, linetype = "longdash", size = 1, mapping = aes(x = log_KOA, y = log_KAW))+
geom_path(data = air.bound, inherit.aes = FALSE, linetype = "longdash", size = 1,mapping = aes(x = log_KOA, y = log_KAW))+
geom_path(data = water.bound, inherit.aes = FALSE, linetype = "longdash", size = 1,mapping = aes(x = log_KOA, y = log_KAW))+
geom_path(data = acbap, inherit.aes = FALSE, linetype = "solid", size = 1,mapping = aes(x = log_KOA, y = log_KAW))+
labs(x = 'logKOA',y = 'logKAW', colour = 'logBCF')+ #Custom legend label
scale_colour_gradientn(colours=rainbow(8))+ #Custom axis labels
geom_point(data = PCBs, #plot PCBs on the scatter plot
aes(x=log_KOA,y= log_KOW - log_KOA),
colour = "blue", size = 0.3)+
annotate(geom = "text", x = -3, y = -12, label = "Water")+
annotate(geom = "text", x = -3, y = 10, label = "Air")+
annotate(geom = "text", x = 10, y = 10, label = "Organics")+
annotate(geom = "text", x = 12, y = -2, label = "ACBAP Region")+
theme_classic() #Simplifies layout and removes gridlines

(7) Making the interacitve scatter plot
# copy and paste the ggplot code you wrote chunk #6 into bracket to run "ggplotly" function
ggplotly(ggplot(data = TSCA, mapping = aes(x = log_KOA, y = log_KAW, colour = log(BCF)))+
geom_point(alpha = 0.5) + #Converts plot to a scatter plot
geom_path(data = org.bound, inherit.aes = FALSE, linetype = "longdash", size = 1, mapping = aes(x = log_KOA, y = log_KAW))+
geom_path(data = air.bound, inherit.aes = FALSE, linetype = "longdash", size = 1,mapping = aes(x = log_KOA, y = log_KAW))+
geom_path(data = water.bound, inherit.aes = FALSE, linetype = "longdash", size = 1,mapping = aes(x = log_KOA, y = log_KAW))+
geom_path(data = acbap, inherit.aes = FALSE, linetype = "solid", size = 1,mapping = aes(x = log_KOA, y = log_KAW))+
labs(x = 'logKOA',y = 'logKAW', colour = 'logBCF')+ #Custom legend label
scale_colour_gradientn(colours=rainbow(8))+ #Custom axis labels
geom_point(data = PCBs, #plot PCBs on the scatter plot
aes(x=log_KOA,y= log_KOW - log_KOA),
colour = "blue", size = 0.3)+
annotate(geom = "text", x = -3, y = -12, label = "Water")+
annotate(geom = "text", x = -3, y = 10, label = "Air")+
annotate(geom = "text", x = 10, y = 10, label = "Organics")+
annotate(geom = "text", x = 12, y = -2, label = "ACBAP Region")+
theme_classic()) #Simplifies layout and removes gridlines)