Introduction

Descriptive Analysis on solubility trends, acid/base reactions, and the logic behind compound separation.


Load Libraries

library(ggplot2)
library(dplyr)
library(tidyr)

pKa_benzoic <- 4.2
pKa_amine <- 5.0
pH_values <- seq(0, 14, by=0.5)

frac_benzoic <- 1 / (1 + 10^(pKa_benzoic - pH_values))
frac_amine <- 1 / (1 + 10^(pH_values - pKa_amine))

plot(pH_values, frac_benzoic, type="b", col="red", lwd=2,
     ylim=c(0,1), xlab="pH", ylab="Fraction Ionized",
     main="Fraction Ionized vs pH")
lines(pH_values, frac_amine, type="b", col="blue", lwd=2)
legend("right", legend=c("Benzoic acid","Ethyl 4-aminobenzoate"),
       col=c("red","blue"), lwd=2)

#------------# Fraction Ionized Table

data.frame(
  pH = pH_values,
  Benzoic_acid = round(frac_benzoic, 2),
  Ethyl_4_aminobenzoate = round(frac_amine, 2)
)
##      pH Benzoic_acid Ethyl_4_aminobenzoate
## 1   0.0         0.00                  1.00
## 2   0.5         0.00                  1.00
## 3   1.0         0.00                  1.00
## 4   1.5         0.00                  1.00
## 5   2.0         0.01                  1.00
## 6   2.5         0.02                  1.00
## 7   3.0         0.06                  0.99
## 8   3.5         0.17                  0.97
## 9   4.0         0.39                  0.91
## 10  4.5         0.67                  0.76
## 11  5.0         0.86                  0.50
## 12  5.5         0.95                  0.24
## 13  6.0         0.98                  0.09
## 14  6.5         1.00                  0.03
## 15  7.0         1.00                  0.01
## 16  7.5         1.00                  0.00
## 17  8.0         1.00                  0.00
## 18  8.5         1.00                  0.00
## 19  9.0         1.00                  0.00
## 20  9.5         1.00                  0.00
## 21 10.0         1.00                  0.00
## 22 10.5         1.00                  0.00
## 23 11.0         1.00                  0.00
## 24 11.5         1.00                  0.00
## 25 12.0         1.00                  0.00
## 26 12.5         1.00                  0.00
## 27 13.0         1.00                  0.00
## 28 13.5         1.00                  0.00
## 29 14.0         1.00                  0.00
#------------------# Solubility Table

solubility <- data.frame(
  Compound = c("Acetic Acid","Phenol","Aniline","Naphthalene"),
  Water = c("soluble","slightly soluble","insoluble","insoluble"),
  NaOH = c("reacts","reacts","insoluble","insoluble"),
  NaHCO3 = c("reacts","no reaction","insoluble","insoluble"),
  HCl = c("insoluble","insoluble","reacts","insoluble"),
  Ethyl_Acetate = c("soluble","soluble","soluble","soluble")
)
solubility
##      Compound            Water      NaOH      NaHCO3       HCl Ethyl_Acetate
## 1 Acetic Acid          soluble    reacts      reacts insoluble       soluble
## 2      Phenol slightly soluble    reacts no reaction insoluble       soluble
## 3     Aniline        insoluble insoluble   insoluble    reacts       soluble
## 4 Naphthalene        insoluble insoluble   insoluble insoluble       soluble
#--------------------------# Solubility by Solvent
solubility_long <- solubility %>%
  pivot_longer(cols = Water:Ethyl_Acetate, names_to="Solvent", values_to="Solubility")

ggplot(solubility_long, aes(x=Compound, fill=Solubility)) +
  geom_bar(position="dodge") +
  facet_wrap(~Solvent) +
  theme_minimal(base_size=12) +
  labs(title="Compound Solubility in Different Solvents", y="Count (Qualitative)")

#-------------------------------# Partition Schematic
# Define compounds and their layer at different pH
compounds <- data.frame(
  Compound = c("Benzoic acid","Ethyl 4-aminobenzoate","Naphthalene"),
  Layer_low_pH = c("Organic","Organic","Organic"),
  Layer_high_pH = c("Aqueous","Organic","Organic")
)

# Convert to long format for plotting
compounds_long <- compounds %>%
  pivot_longer(cols = c(Layer_low_pH, Layer_high_pH),
               names_to = "Condition", values_to = "Layer") %>%
  mutate(y_pos = case_when(
    Compound == "Benzoic acid" ~ 3,
    Compound == "Ethyl 4-aminobenzoate" ~ 2,
    Compound == "Naphthalene" ~ 1
  ),
  Condition = factor(Condition, levels=c("Layer_low_pH","Layer_high_pH")))

# Plot schematic
ggplot() +
  geom_rect(aes(xmin=0.5, xmax=1.5, ymin=0, ymax=2), fill="lightblue", alpha=0.3) + # Aqueous
  geom_rect(aes(xmin=0.5, xmax=1.5, ymin=2, ymax=4), fill="lightgray", alpha=0.3) + # Organic
  geom_point(data=compounds_long, aes(x=as.numeric(Condition), y=y_pos, color=Compound), size=5) +
  scale_x_continuous(breaks=c(1,2), labels=c("Low pH","High pH")) +
  scale_y_continuous(breaks=c(1,2,3), labels=c("Naphthalene","Ethyl 4-aminobenzoate","Benzoic acid")) +
  labs(x="Condition", y="Compound", title="Layer Partitioning at Different pH") +
  theme_minimal(base_size = 12) +
  theme(panel.grid=element_blank())

# Conclusions
# Acid base reactions change a compound’s solubility, letting acids or bases form ions 
# that dissolve in the aqueous layer, while neutral compounds stay in the organic layer. 
# The results match predictions based on pKa values and functional groups, confirming 
# that acid base extraction effectively separates compounds.

## Lab Document