#upload data
Seed_measurements <- read.csv("Seed measurements.csv")

# Load necessary libraries
library(ggplot2)
library(tidyr)

# Sample data creation 
Seed_measurements <- data.frame(
  Group = c(rep("LAMA", 60), rep("NORA", 60), rep("HYBR", 60)),
  Shell_Height = runif(180, 20, 60),
  Shell_Width = runif(180, 5, 15),
  Shell_Length = runif(180, 15, 40)
)

# Reshape the data to long format for ggplot
data_long <- Seed_measurements %>%
  pivot_longer(cols = c(Shell_Height, Shell_Width, Shell_Length), 
               names_to = "Shell_Type", 
               values_to = "Measurement")
# Create a combined boxplot
ggplot(data_long, aes(x = Group, y = Measurement, fill = Shell_Type)) +
  geom_boxplot(outlier.color = "red", outlier.shape = 21) +
  labs(
    title = "Two-way ANOVA Box Plot: Shell Measurements by Group",
    x = "Group",
    y = "Measurement",
    fill = "Shell Type"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 14, face = "bold"),
    legend.position = "right",
    panel.border = element_rect(color = "black", fill = NA, size = 1)  
  ) +
  scale_fill_brewer(palette = "Set3")
## Warning: The `size` argument of `element_rect()` 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.

# Perform two-way ANOVA
anova_result <- aov(Measurement ~ Group * Shell_Type, data = data_long)

# Display the ANOVA table
summary(anova_result)
##                   Df Sum Sq Mean Sq F value Pr(>F)    
## Group              2     70      35   0.524  0.593    
## Shell_Type         2  81628   40814 607.033 <2e-16 ***
## Group:Shell_Type   4    554     138   2.058  0.085 .  
## Residuals        531  35702      67                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
par(mfrow = c(2, 2))  
plot(anova_result)

# Perform Tukey's HSD test
tukey_result <- TukeyHSD(anova_result)

# Display Tukey results
print(tukey_result)
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = Measurement ~ Group * Shell_Type, data = data_long)
## 
## $Group
##                 diff       lwr      upr     p adj
## LAMA-HYBR -0.8437366 -2.875163 1.187690 0.5922592
## NORA-HYBR -0.6514606 -2.682887 1.379966 0.7315067
## NORA-LAMA  0.1922760 -1.839151 2.223703 0.9730891
## 
## $Shell_Type
##                                diff       lwr       upr p adj
## Shell_Length-Shell_Height -13.47874 -15.51016 -11.44731     0
## Shell_Width-Shell_Height  -30.06270 -32.09412 -28.03127     0
## Shell_Width-Shell_Length  -16.58396 -18.61539 -14.55253     0
## 
## $`Group:Shell_Type`
##                                            diff        lwr         upr
## LAMA:Shell_Height-HYBR:Shell_Height  -2.7536082  -7.416389   1.9091722
## NORA:Shell_Height-HYBR:Shell_Height  -3.8638466  -8.526627   0.7989337
## HYBR:Shell_Length-HYBR:Shell_Height -16.5948799 -21.257660 -11.9320995
## LAMA:Shell_Length-HYBR:Shell_Height -15.9941102 -20.656891 -11.3313298
## NORA:Shell_Length-HYBR:Shell_Height -14.4646730 -19.127453  -9.8018926
## HYBR:Shell_Width-HYBR:Shell_Height  -32.0688101 -36.731591 -27.4060297
## LAMA:Shell_Width-HYBR:Shell_Height  -32.4471816 -37.109962 -27.7844012
## NORA:Shell_Width-HYBR:Shell_Height  -32.2895523 -36.952333 -27.6267719
## NORA:Shell_Height-LAMA:Shell_Height  -1.1102385  -5.773019   3.5525419
## HYBR:Shell_Length-LAMA:Shell_Height -13.8412717 -18.504052  -9.1784913
## LAMA:Shell_Length-LAMA:Shell_Height -13.2405020 -17.903282  -8.5777216
## NORA:Shell_Length-LAMA:Shell_Height -11.7110648 -16.373845  -7.0482844
## HYBR:Shell_Width-LAMA:Shell_Height  -29.3152019 -33.977982 -24.6524216
## LAMA:Shell_Width-LAMA:Shell_Height  -29.6935734 -34.356354 -25.0307930
## NORA:Shell_Width-LAMA:Shell_Height  -29.5359441 -34.198724 -24.8731637
## HYBR:Shell_Length-NORA:Shell_Height -12.7310332 -17.393814  -8.0682529
## LAMA:Shell_Length-NORA:Shell_Height -12.1302635 -16.793044  -7.4674832
## NORA:Shell_Length-NORA:Shell_Height -10.6008263 -15.263607  -5.9380459
## HYBR:Shell_Width-NORA:Shell_Height  -28.2049635 -32.867744 -23.5421831
## LAMA:Shell_Width-NORA:Shell_Height  -28.5833350 -33.246115 -23.9205546
## NORA:Shell_Width-NORA:Shell_Height  -28.4257056 -33.088486 -23.7629253
## LAMA:Shell_Length-HYBR:Shell_Length   0.6007697  -4.062011   5.2635501
## NORA:Shell_Length-HYBR:Shell_Length   2.1302069  -2.532573   6.7929873
## HYBR:Shell_Width-HYBR:Shell_Length  -15.4739303 -20.136711 -10.8111499
## LAMA:Shell_Width-HYBR:Shell_Length  -15.8523017 -20.515082 -11.1895213
## NORA:Shell_Width-HYBR:Shell_Length  -15.6946724 -20.357453 -11.0318920
## NORA:Shell_Length-LAMA:Shell_Length   1.5294372  -3.133343   6.1922176
## HYBR:Shell_Width-LAMA:Shell_Length  -16.0747000 -20.737480 -11.4119196
## LAMA:Shell_Width-LAMA:Shell_Length  -16.4530714 -21.115852 -11.7902910
## NORA:Shell_Width-LAMA:Shell_Length  -16.2954421 -20.958222 -11.6326617
## HYBR:Shell_Width-NORA:Shell_Length  -17.6041372 -22.266918 -12.9413568
## LAMA:Shell_Width-NORA:Shell_Length  -17.9825086 -22.645289 -13.3197283
## NORA:Shell_Width-NORA:Shell_Length  -17.8248793 -22.487660 -13.1620989
## LAMA:Shell_Width-HYBR:Shell_Width    -0.3783715  -5.041152   4.2844089
## NORA:Shell_Width-HYBR:Shell_Width    -0.2207422  -4.883523   4.4420382
## NORA:Shell_Width-LAMA:Shell_Width     0.1576293  -4.505151   4.8204097
##                                         p adj
## LAMA:Shell_Height-HYBR:Shell_Height 0.6557898
## NORA:Shell_Height-HYBR:Shell_Height 0.1970877
## HYBR:Shell_Length-HYBR:Shell_Height 0.0000000
## LAMA:Shell_Length-HYBR:Shell_Height 0.0000000
## NORA:Shell_Length-HYBR:Shell_Height 0.0000000
## HYBR:Shell_Width-HYBR:Shell_Height  0.0000000
## LAMA:Shell_Width-HYBR:Shell_Height  0.0000000
## NORA:Shell_Width-HYBR:Shell_Height  0.0000000
## NORA:Shell_Height-LAMA:Shell_Height 0.9981554
## HYBR:Shell_Length-LAMA:Shell_Height 0.0000000
## LAMA:Shell_Length-LAMA:Shell_Height 0.0000000
## NORA:Shell_Length-LAMA:Shell_Height 0.0000000
## HYBR:Shell_Width-LAMA:Shell_Height  0.0000000
## LAMA:Shell_Width-LAMA:Shell_Height  0.0000000
## NORA:Shell_Width-LAMA:Shell_Height  0.0000000
## HYBR:Shell_Length-NORA:Shell_Height 0.0000000
## LAMA:Shell_Length-NORA:Shell_Height 0.0000000
## NORA:Shell_Length-NORA:Shell_Height 0.0000000
## HYBR:Shell_Width-NORA:Shell_Height  0.0000000
## LAMA:Shell_Width-NORA:Shell_Height  0.0000000
## NORA:Shell_Width-NORA:Shell_Height  0.0000000
## LAMA:Shell_Length-HYBR:Shell_Length 0.9999814
## NORA:Shell_Length-HYBR:Shell_Length 0.8887713
## HYBR:Shell_Width-HYBR:Shell_Length  0.0000000
## LAMA:Shell_Width-HYBR:Shell_Length  0.0000000
## NORA:Shell_Width-HYBR:Shell_Length  0.0000000
## NORA:Shell_Length-LAMA:Shell_Length 0.9837402
## HYBR:Shell_Width-LAMA:Shell_Length  0.0000000
## LAMA:Shell_Width-LAMA:Shell_Length  0.0000000
## NORA:Shell_Width-LAMA:Shell_Length  0.0000000
## HYBR:Shell_Width-NORA:Shell_Length  0.0000000
## LAMA:Shell_Width-NORA:Shell_Length  0.0000000
## NORA:Shell_Width-NORA:Shell_Length  0.0000000
## LAMA:Shell_Width-HYBR:Shell_Width   0.9999995
## NORA:Shell_Width-HYBR:Shell_Width   1.0000000
## NORA:Shell_Width-LAMA:Shell_Width   1.0000000
plot(tukey_result, las = 2)