--- title: "Floral Morphology and Species Separation in *Iris*" subtitle: "A reproducible analysis using RMarkdown" author: "Your Name" date: "`r Sys.Date()`" output: html_document: toc: true toc_float: true toc_depth: 3 code_folding: show --- ```{r setup, include=FALSE} knitr::opts_chunk$set( echo = TRUE, message = FALSE, warning = FALSE, fig.width = 7, fig.height = 5 ) library(ggplot2) ``` ## Background The *Iris* dataset was collected by Edgar Anderson in 1935 on the Gaspé Peninsula, Quebec, and later used by Ronald Fisher to introduce linear discriminant analysis. It contains `r nrow(iris)` observations across four floral traits — sepal length, sepal width, petal length, and petal width — measured on three species: *I. setosa*, *I. versicolor*, and *I. virginica*. The central question here is straightforward: **do these species occupy distinct regions of morphological space, and which traits best separate them?** This matters ecologically because morphological divergence in floral traits is often linked to pollinator specialisation and reproductive isolation. --- ## Morphological overview ### Species means ```{r summary-stats} means <- aggregate(. ~ Species, data = iris, FUN = mean) knitr::kable( means, digits = 2, caption = "Mean floral trait values by species (cm)." ) ``` *I. setosa* stands out immediately: its petal dimensions are roughly three times smaller than those of the other two species. Sepal width shows the opposite pattern, with *setosa* having the broadest sepals. ### Trait distributions {.tabset} ```{r species-cols, include=FALSE} sp_cols <- c( "setosa" = "#4E79A7", "versicolor" = "#F28E2B", "virginica" = "#59A14F" ) ``` #### Petal length ```{r plot-petal-length, fig.cap="Petal length distributions by species. The near-complete separation of *setosa* from the other two species is visible even in a simple boxplot."} ggplot(iris, aes(x = Species, y = Petal.Length, fill = Species)) + geom_boxplot(alpha = 0.7, outlier.shape = 21) + geom_jitter(aes(colour = Species), width = 0.15, size = 1.5, alpha = 0.6) + scale_fill_manual(values = sp_cols) + scale_colour_manual(values = sp_cols) + labs( x = NULL, y = "Petal length (cm)", caption = "Each point is one observation." ) + theme_classic(base_size = 13) + theme(legend.position = "none") ``` #### Sepal length ```{r plot-sepal-length, fig.cap="Sepal length distributions by species. The three species overlap considerably more than for petal length."} ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) + geom_boxplot(alpha = 0.7, outlier.shape = 21) + geom_jitter(aes(colour = Species), width = 0.15, size = 1.5, alpha = 0.6) + scale_fill_manual(values = sp_cols) + scale_colour_manual(values = sp_cols) + labs( x = NULL, y = "Sepal length (cm)", caption = "Each point is one observation." ) + theme_classic(base_size = 13) + theme(legend.position = "none") ``` #### Morphological space ```{r plot-scatter, fig.cap="Sepal length vs petal length coloured by species. Petal length separates the three species far more cleanly than sepal length alone."} ggplot(iris, aes(x = Sepal.Length, y = Petal.Length, colour = Species, fill = Species)) + geom_point(size = 2.5, alpha = 0.8, shape = 21, stroke = 0.3, colour = "white") + geom_smooth(method = "lm", se = TRUE, alpha = 0.15, linewidth = 0.8) + scale_fill_manual(values = sp_cols) + labs( x = "Sepal length (cm)", y = "Petal length (cm)", colour = "Species", fill = "Species" ) + theme_classic(base_size = 13) ``` --- ## Do species differ significantly in petal length? Petal length shows the clearest visual separation. A one-way ANOVA tests whether the observed differences in group means are larger than expected by chance. ```{r anova} fit <- aov(Petal.Length ~ Species, data = iris) summary(fit) ``` ```{r anova-inline, include=FALSE} f_val <- summary(fit)[[1]]["Species", "F value"] p_val <- summary(fit)[[1]]["Species", "Pr(>F)"] r2 <- summary(fit)[[1]]["Species", "Sum Sq"] / sum(summary(fit)[[1]][, "Sum Sq"]) ``` The ANOVA is highly significant (F = `r round(f_val, 1)`, p < 0.001). Species identity explains `r round(r2 * 100, 1)`% of the total variance in petal length (η² = `r round(r2, 3)`), indicating strong morphological separation. A post-hoc Tukey test confirms that all three pairwise comparisons are significant: ```{r tukey} tukey <- TukeyHSD(fit) knitr::kable( as.data.frame(tukey$Species), digits = 4, caption = "Tukey HSD pairwise comparisons for petal length." ) ``` --- ## Principal component analysis A PCA across all four traits summarises the overall morphological differentiation in two dimensions. ```{r pca, include=FALSE} pca <- prcomp(iris[, 1:4], scale. = TRUE) pca_df <- data.frame(pca$x, Species = iris$Species) var_exp <- round(summary(pca)$importance[2, 1:2] * 100, 1) ``` ```{r pca-plot, fig.cap="PCA of all four floral traits. PC1 and PC2 together explain a large fraction of total variance. *I. setosa* is completely separated from the other two species; *versicolor* and *virginica* overlap partially along PC2."} ggplot(pca_df, aes(x = PC1, y = PC2, fill = Species)) + geom_point(size = 2.8, shape = 21, stroke = 0.3, colour = "white", alpha = 0.9) + stat_ellipse(aes(colour = Species), level = 0.90, linewidth = 0.7, linetype = "dashed") + scale_fill_manual(values = sp_cols) + scale_colour_manual(values = sp_cols) + labs( x = paste0("PC1 (", var_exp[1], "% variance explained)"), y = paste0("PC2 (", var_exp[2], "% variance explained)"), fill = "Species", colour = "Species" ) + theme_classic(base_size = 13) ``` PC1 and PC2 together explain `r var_exp[1] + var_exp[2]`% of total variance. The near-complete separation of *I. setosa* suggests it is morphologically more distinct from the other two species, consistent with its classification in a separate subsection (*Psiris*) within the genus. --- ## RMarkdown features illustrated This section is here for teaching purposes only. It demonstrates several chunk options that do not affect the scientific content above. ### Echo off (`echo=FALSE`) The chunk below runs and produces output, but the source code is hidden. ```{r echo-demo, echo=FALSE} knitr::kable( head(iris, 4), caption = "First four rows (code hidden with echo=FALSE)." ) ``` ### Include off (`include=FALSE`) The chunk below runs and assigns a value, but nothing appears in the report. The object is available to subsequent chunks and inline expressions. ```{r include-demo, include=FALSE} n_setosa <- sum(iris$Species == "setosa") ``` There are `r n_setosa` *setosa* observations. That number was computed in the hidden chunk above and inserted here using inline R code. ### Caching a slow step (`cache=TRUE`) The correlation matrix below is cached. On subsequent knits it is loaded from disk rather than recomputed. Delete the `_cache` folder to force re-execution. ```{r slow-chunk, cache=TRUE} Sys.sleep(2) knitr::kable( round(cor(iris[, 1:4]), 2), caption = "Correlation matrix across all four traits (cached)." ) ``` --- ## Session information ```{r session-info} sessionInfo() ```