--- title: "analyses" output: html_document date: "2025-03-03" --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) library(dplyr) library(kableExtra) library(ggplot2) library(officer) library(flextable) library(openxlsx) ``` ## Binom test ```{r tabelka, include=FALSE} proportions_binom <- data.frame( species = character(), stage = character(), M = numeric(), F = numeric(), M_F = numeric(), prop = numeric(), p_val = numeric(), ci_min = numeric(), ci_max = numeric(), stringsAsFactors = FALSE) ``` ```{r funkcje, echo=FALSE} describe_binom <- function(df, species, stage, Mn, Fn) { all <- (Mn+Fn) #tab <- matrix(c(Mn, all/2, Fn, all/2), nrow = 2) pt <- binom.test(x = Mn, n = all, p = 0.5) df <- rbind(df, data.frame( species = species, stage = stage, M = Mn, F = Fn, M_F = round(Mn/Fn, 2), prop = round(pt$estimate, 2), p_val = round(pt$p.value, 4), ci_min = round(pt$conf.int[1], 2), ci_max = round(pt$conf.int[2], 2) ) ) return(df) } ``` ```{r ra-seeds, echo=FALSE} Fn <- 62 Mn <- 38 seeds <- c(Fn, Mn) all_n <- sum(seeds) proportions_binom <- describe_binom(proportions_binom, "R. acetosa", "seeds", Mn, Fn) ``` ```{r ra-seedlings, echo=FALSE} Fs <- 18 Ms <- 17 seedlings <- c(Fs, Ms) all_s <- sum(seedlings) proportions_binom <- describe_binom(proportions_binom, "R. acetosa", "plants", Ms, Fs) ``` ```{r rt-seeds, echo=FALSE} Fn <- 40+38+38+41 Mn <- 18+21+22+19 seeds <- c(Fn, Mn) all_n <- sum(seeds) proportions_binom <- describe_binom(proportions_binom, "R. thyrsiflorus", "seeds", Mn, Fn) ``` ```{r rt-seedlings, echo=FALSE} Fs <- 17 Ms <- 9 seedlings <- c(Fs, Ms) all_s <- sum(seedlings) proportions_binom <- describe_binom(proportions_binom, "R. thyrsiflorus", "plants", Ms, Fs) ``` ```{r plot-proportions, echo=FALSE} proportions_binom$group <- paste(proportions_binom$species, proportions_binom$stage, sep = " - ") knitr::kable(proportions_binom) %>% kable_styling(full_width = FALSE, position = "center") proportions_binom$group <- factor(proportions_binom$group, levels = c("R. acetosa - seeds", "R. acetosa - plants", "R. thyrsiflorus - seeds", "R. thyrsiflorus - plants")) write.table(proportions_binom, file = "proportions_binom.tsv", sep = "\t", row.names = FALSE, quote = FALSE, na = "NA") write.xlsx(proportions_binom, file = "proportions_binom.xlsx", rowNames = FALSE) doc <- read_docx() doc <- doc %>% body_add_flextable(flextable(proportions_binom)) print(doc, target = "proportions_binom.docx") p <- ggplot(proportions_binom, aes(x = group, y = prop, fill = stage)) + geom_bar(stat = "identity", width = 0.7, color = "black") + geom_errorbar(aes(ymin = ci_min, ymax = ci_max), width = 0.2, size = 0.8) + scale_fill_manual(values = c("seeds" = "gray80", "plants" = "gray90")) + ylim(0, 1) + labs(x = "Species and Stage", y = "Frequency of males", title = "Frequency of males in seeds and plants") + theme_minimal() + theme(axis.text.x = element_text(angle = 45, hjust = 1)) p ggsave(filename = "box_binom.svg", plot = p, width = 8, height = 6, dpi = 300, bg = "transparent") ggsave(filename = "box_binom.pdf", plot = p, width = 8, height = 6, bg = "transparent") ggsave(filename = "box_binom.png", plot = p, width = 8, height = 6, dpi = 300, bg = "transparent") ```