library(readxl)
library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)

  # Leer archivo Excel
datos_individuales <- read_excel("Datos micro agricola.xlsx", 
    col_types = c("numeric", "text", "text", 
        "numeric", "numeric", "numeric"))

# Convertir a factores
datos_individuales$MICROORGANISMO <- as.factor(datos_individuales$MICROORGANISMO)
datos_individuales$ESTRÉS <- as.factor(datos_individuales$ESTRÉS)
datos_individuales$TRATAMIENTO <- as.factor(datos_individuales$TRATAMIENTO)
datos_individuales$MUESTREO <- as.factor(datos_individuales$MUESTREO)

# SPAD ~ Microorganismo * Estrés
modelo_spad <- aov(SPAD ~ MICROORGANISMO * ESTRÉS, data = datos_individuales)
summary(modelo_spad)
##                       Df Sum Sq Mean Sq F value  Pr(>F)   
## MICROORGANISMO         1   52.6   52.62   1.243 0.27156   
## ESTRÉS                 2  483.7  241.83   5.712 0.00658 **
## MICROORGANISMO:ESTRÉS  1    2.4    2.40   0.057 0.81293   
## Residuals             40 1693.6   42.34                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
TukeyHSD(modelo_spad, "ESTRÉS")
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = SPAD ~ MICROORGANISMO * ESTRÉS, data = datos_individuales)
## 
## $ESTRÉS
##           diff       lwr       upr     p adj
## 50-0  4.590741 -1.874749 11.056230 0.2073495
## 80-0  8.307407  1.841918 14.772897 0.0090137
## 80-50 3.716667 -1.562383  8.995717 0.2126696
# Temperatura ~ Microorganismo * Estrés
modelo_temp <- aov(TEMPERATURA ~ MICROORGANISMO * ESTRÉS, data = datos_individuales)
summary(modelo_temp)
##                       Df Sum Sq Mean Sq F value  Pr(>F)   
## MICROORGANISMO         1    0.4    0.39   0.020 0.88787   
## ESTRÉS                 2  282.2  141.12   7.374 0.00188 **
## MICROORGANISMO:ESTRÉS  1    3.4    3.36   0.176 0.67740   
## Residuals             40  765.5   19.14                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Agrupar y calcular media y error estándar
resumen_spad <- datos_individuales %>%
  group_by(MICROORGANISMO, ESTRÉS) %>%
  summarise(
    media = mean(SPAD, na.rm = TRUE),
    se = sd(SPAD, na.rm = TRUE) / sqrt(n())
  )
## `summarise()` has grouped output by 'MICROORGANISMO'. You can override using
## the `.groups` argument.
# Gráfico de barras
ggplot(resumen_spad, aes(x = ESTRÉS, y = media, fill = MICROORGANISMO)) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.8)) +
  geom_errorbar(aes(ymin = media - se, ymax = media + se),
                position = position_dodge(width = 0.8), width = 0.2) +
  labs(title = "SPAD por estrés y microorganismo",
       x = "Nivel de estrés (%)", y = "Valor medio SPAD") +
  theme_minimal()

resumen_temperatura <- datos_individuales %>%
  group_by(MICROORGANISMO, ESTRÉS) %>%
  summarise(
    media = mean(TEMPERATURA, na.rm = TRUE),
    se = sd(TEMPERATURA, na.rm = TRUE) / sqrt(n())
  )
## `summarise()` has grouped output by 'MICROORGANISMO'. You can override using
## the `.groups` argument.
ggplot(resumen_temperatura, aes(x = ESTRÉS, y = media, fill = MICROORGANISMO)) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.8)) +
  geom_errorbar(aes(ymin = media - se, ymax = media + se),
                position = position_dodge(width = 0.8), width = 0.2) +
  labs(title = "TEMPERATURA por estrés y microorganismo",
       x = "Nivel de estrés (%)", y = "Valor medio TEMPERATURA") +
  theme_minimal()

## datos para análisis descriptivo

datos_promedios <- read_excel("Promedios microbiologĂ­a agrĂ­cola.xlsx", 
    col_types = c("numeric", "text", "text", 
        "numeric", "numeric", "numeric", 
        "numeric", "numeric"))

# Asegurar que variables sean categĂłricas
datos_promedios$MICROORGANISMO <- as.factor(datos_promedios$MICROORGANISMO)
datos_promedios$ESTRÉS <- as.factor(datos_promedios$ESTRÉS)
datos_promedios$TRATAMIENTO <- as.factor(datos_promedios$TRATAMIENTO)
datos_promedios$MUESTREO <- as.factor(datos_promedios$MUESTREO)

library(ggplot2)

ggplot(datos_promedios, aes(x = TRATAMIENTO, y = ALTURA, fill = TRATAMIENTO)) +
  geom_bar(stat = "identity", color = "black") +
  labs(title = "Altura promedio por tratamiento",
       y = "Altura (cm)", x = "Tratamiento") +
  theme_minimal()

ggplot(datos_promedios, aes(x = TRATAMIENTO, y = `PESO FRESCO`, fill = TRATAMIENTO)) +
  geom_bar(stat = "identity", color = "black") +
  labs(title = "Peso promedio por tratamiento",
       y = "Peso (g)", x = "Tratamiento") +
  theme_minimal()

ggplot(datos_promedios, aes(x = TRATAMIENTO, y = DIAMETRO, fill = TRATAMIENTO)) +
  geom_bar(stat = "identity", color = "black") +
  labs(title = "Diámetro promedio por tratamiento",
       y = "diámetro (cm)", x = "Tratamiento") +
  theme_minimal()

ggplot(datos_promedios, aes(x = TRATAMIENTO, y = HOJAS, fill = TRATAMIENTO)) +
  geom_bar(stat = "identity", color = "black") +
  labs(title = "Moda nĂşmero de hojas por tratamiento",
       y = "N° de hojas", x = "Tratamiento") +
  theme_minimal()

#######################################

# Supongamos que tu tabla se llama 'datos_promedios'
# y contiene: Tratamiento, Muestreo, Altura, Peso, Diametro, Hojas

# Reorganizar en formato largo
datos_largos <- datos_promedios %>%
  pivot_longer(cols = c(ALTURA, `PESO FRESCO`, DIAMETRO, HOJAS),
               names_to = "Variable",
               values_to = "Valor")
datos_largos$MICROORGANISMO <- as.factor(datos_largos$MICROORGANISMO)
datos_largos$ESTRÉS <- as.factor(datos_largos$ESTRÉS)
datos_largos$Variable <- as.factor(datos_largos$Variable)

ggplot(datos_largos, aes(x = ESTRÉS, y = Valor, fill = MICROORGANISMO)) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.8), color = "black") +
  facet_wrap(~ Variable, scales = "free_y") +
  labs(title = "Comparación de microorganismo por nivel de estrés",
       x = "Nivel de estrés hídrico (%)",
       y = "Valor promedio",
       fill = "Microorganismo") +
  theme_minimal() +
  scale_fill_brewer(palette = "Set2")

# Gráfico con barras agrupadas por tratamiento y coloreadas por muestreo
ggplot(datos_largos, aes(x = TRATAMIENTO, y = Valor, fill = MUESTREO)) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.8), color = "black") +
  facet_wrap(~ Variable, scales = "free_y") +
  labs(title = "Promedios por tratamiento y muestreo",
       x = "Tratamiento",
       y = "Valor promedio",
       fill = "Muestreo") +
  theme_minimal() +
  scale_fill_brewer(palette = "Set2")