--- title: "dlugosc_kwitnienia" output: html_document: default always_allow_html: true date: "2025-03-05" --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) library(dplyr) library(kableExtra) library(ggplot2) ``` Data: `flowering.tsv` ```{r read_data, echo=FALSE} data_file <- "flowering.tsv" data <- read.table(file=paste(data_file), sep = '\t', header = TRUE, na.strings = "NA") #print(data) ``` ## Flowering time ```{r flowering-sel_danych, echo=FALSE} # R. acetosa, męskie am <- data[data$species == 'R. acetosa' & data$sex == 'M', ] # R. acetosa, żeńskie af <- data[data$species == 'R. acetosa' & data$sex == 'F', ] # Analogicznie, R. thyrsiflorus tm <- data[data$species == 'R. thyrsiflorus' & data$sex == 'M', ] tf <- data[data$species == 'R. thyrsiflorus' & data$sex == 'F', ] ``` ```{r functions, echo=FALSE} describe_flowering <- function(df, species, sex, dat_all, kol) { dat = dat_all[, kol] tt = t.test(dat) df <- rbind(df, data.frame( species = species, sex = sex, min = min(dat), max = max(dat), mean = round(mean(dat), 2), sd = round(sd(dat), 2), ci_min = round(tt$conf.int[1], 2), ci_max = round(tt$conf.int[2], 2) )) return(df) } compare_flowering <- function(df, species1, species2, sex1, sex2, dat1_all, dat2_all, kol) { dat1 = dat1_all[, kol] dat2 = dat2_all[, kol] tt = t.test(dat1, dat2) df <- rbind(df, data.frame( species1 = species1, species2 = species2, sex1 = sex1, sex2 = sex2, set1_mean = round(mean(dat1), 2), set2_mean = round(mean(dat2), 2), means_difference = round((mean(dat1) - mean(dat2)), 2), p_val = round(tt$p.value, 6), ci_min = round(tt$conf.int[1], 2), ci_max = round(tt$conf.int[2], 2))) return(df) } ``` ## First flowering - description ```{r flowering-opis, echo=FALSE} flowering1_desc <- data.frame( species = character(), sex = character(), min = numeric(), max = numeric(), mean = numeric(), sd = numeric(), ci_min = numeric(), ci_max = numeric(), stringsAsFactors = FALSE) flowering1_desc <- describe_flowering(flowering1_desc, "R. acetosa", "M", am, "sum_1") flowering1_desc <- describe_flowering(flowering1_desc, "R. acetosa", "F", af, "sum_1") flowering1_desc <- describe_flowering(flowering1_desc, "R. thyrsiflorus", "M", tm, "sum_1") flowering1_desc <- describe_flowering(flowering1_desc, "R. thyrsiflorus", "F", tf, "sum_1") knitr::kable(flowering1_desc) %>% kable_styling(full_width = FALSE, position = "center") write.table(flowering1_desc, file = "flowering1_desc.tsv", sep = "\t", row.names = FALSE, quote = FALSE, na = "NA") ``` ## First flowering - comparisons ```{r flowering-comp, echo=FALSE} flowering1_comp <- data.frame( species1 = character(), species2 = character(), sex1 = character(), sex2 = character(), set1_mean = numeric(), set2_mean = numeric(), means_difference = numeric(), p_val = numeric(), ci_min = numeric(), ci_max = numeric(), stringsAsFactors = FALSE) flowering1_comp <- compare_flowering(flowering1_comp, "R. acetosa", "R. acetosa", "M", "F", am, af, "sum_1") flowering1_comp <- compare_flowering(flowering1_comp, "R. thyrsiflorus", "R. thyrsiflorus", "M", "F", tm, tf, "sum_1") flowering1_comp <- compare_flowering(flowering1_comp, "R. acetosa", "R. thyrsiflorus", "M", "M", am, tm, "sum_1") flowering1_comp <- compare_flowering(flowering1_comp, "R. acetosa", "R. thyrsiflorus", "F", "F", af, tf, "sum_1") knitr::kable(flowering1_comp) %>% kable_styling(full_width = FALSE, position = "center") write.table(flowering1_comp, file = "flowering1_comp.tsv", sep = "\t", row.names = FALSE, quote = FALSE, na = "NA") ``` ```{r rozklady, echo=FALSE} data$name <- paste(data$species, data$sex, sep='') flowering1_desc$name <- paste(flowering1_desc$species, flowering1_desc$sex, sep='') p <- ggplot(data = data) + geom_jitter(mapping = aes(x = name, y = sum_1, fill = sex), shape=21, stroke=1, position = position_jitter(width = 0.3, height = 0), alpha = 0.5, size = 2) + geom_point(data = flowering1_desc, aes(x = name, y = mean), position = position_dodge(width = 0.5), size = 4) + geom_errorbar(data = flowering1_desc, aes(x = name, ymin = ci_min, ymax = ci_max), width = 0.2, position = position_dodge(width = 0.5)) + scale_fill_manual(values = c("M" = "black", "F" = "white")) + theme_minimal() ggsave(filename = "jitter.svg", plot = p, width = 8, height = 6, dpi = 300, bg = "transparent") ggsave(filename = "jitter.pdf", plot = p, width = 8, height = 6, bg = "transparent") ggsave(filename = "jitter.png", plot = p, width = 8, height = 6, dpi = 300, bg = "transparent") p ```