#Librerias
library(readr)
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(ggplot2)
library(lattice)
library(tigerstats)
## Loading required package: abd
## Loading required package: nlme
## 
## Attaching package: 'nlme'
## The following object is masked from 'package:dplyr':
## 
##     collapse
## Loading required package: grid
## Loading required package: mosaic
## Registered S3 method overwritten by 'mosaic':
##   method                           from   
##   fortify.SpatialPolygonsDataFrame ggplot2
## 
## The 'mosaic' package masks several functions from core packages in order to add 
## additional features.  The original behavior of these functions should not be affected by this.
## 
## Attaching package: 'mosaic'
## The following object is masked from 'package:Matrix':
## 
##     mean
## The following object is masked from 'package:ggplot2':
## 
##     stat
## The following objects are masked from 'package:dplyr':
## 
##     count, do, tally
## The following objects are masked from 'package:stats':
## 
##     binom.test, cor, cor.test, cov, fivenum, IQR, median, prop.test,
##     quantile, sd, t.test, var
## The following objects are masked from 'package:base':
## 
##     max, mean, min, prod, range, sample, sum
## Welcome to tigerstats!
## To learn more about this package, consult its website:
##  http://homerhanumat.github.io/tigerstats
library(multcompView)
library(readxl)
Severidad <- read_excel("C:\\Users\\USER\\Documents\\UNIVERSIDAD F\\2022-1\\Fitopatologia\\Fotos fito 2.xlsx",sheet = "Hoja1")
df1=data.frame(Severidad)

#Analisis de Varianza de dos factores

#Organizacion de datos

head(df1)
str(df1)
## 'data.frame':    840 obs. of  5 variables:
##  $ Muestreos  : num  1 1 1 1 1 2 2 2 2 2 ...
##  $ Variedad   : chr  "Pastusa" "Pastusa" "Pastusa" "Pastusa" ...
##  $ Repeticion : num  1 2 3 4 5 1 2 3 4 5 ...
##  $ Tratamiento: chr  "T1" "T1" "T1" "T1" ...
##  $ Severidad  : num  0 0 0 0 0 0 0 0 0 0 ...

Los Factores Muestreo y tratamientos que estan como caracteres , se transforman en Factores para el analisis de varianzas

# creating a variable as factor for the ANOVA
df1$Muestreos <- as.factor(df1$Muestreos)
df1$Tratamiento <- as.factor(df1$Tratamiento)
df1$Variedad <- as.factor(df1$Variedad)

str(df1)
## 'data.frame':    840 obs. of  5 variables:
##  $ Muestreos  : Factor w/ 14 levels "1","2","3","4",..: 1 1 1 1 1 2 2 2 2 2 ...
##  $ Variedad   : Factor w/ 4 levels "Criolla","Pastusa",..: 2 2 2 2 2 2 2 2 2 2 ...
##  $ Repeticion : num  1 2 3 4 5 1 2 3 4 5 ...
##  $ Tratamiento: Factor w/ 3 levels "T1","T2","T3": 1 1 1 1 1 1 1 1 1 1 ...
##  $ Severidad  : num  0 0 0 0 0 0 0 0 0 0 ...
# analysis of variance
anova <- aov(df1$Severidad ~ df1$Tratamiento*df1$Variedad, data = df1)
summary(anova)
##                               Df Sum Sq Mean Sq F value Pr(>F)    
## df1$Tratamiento                2  33787   16894   42.45 <2e-16 ***
## df1$Variedad                   3  73703   24568   61.74 <2e-16 ***
## df1$Tratamiento:df1$Variedad   6  70955   11826   29.72 <2e-16 ***
## Residuals                    828 329494     398                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# table with factors, means and standard deviation
data_summary <- group_by(df1, Variedad, Tratamiento) %>%
  summarise(mean=mean(Severidad), sd=sd(Severidad)) %>%
  arrange(desc(mean))
## `summarise()` has grouped output by 'Variedad'. You can override using the
## `.groups` argument.
print(data_summary)
## # A tibble: 12 × 4
## # Groups:   Variedad [4]
##    Variedad  Tratamiento  mean    sd
##    <fct>     <fct>       <dbl> <dbl>
##  1 Criolla   T2          41.6   40.9
##  2 Pastusa   T3          33.4   38.2
##  3 Criolla   T3          24.4   35.9
##  4 Pastusa   T2           6.99  19.0
##  5 Criolla   T1           0      0  
##  6 Pastusa   T1           0      0  
##  7 Remolacha T1           0      0  
##  8 Remolacha T2           0      0  
##  9 Remolacha T3           0      0  
## 10 Zanahoria T1           0      0  
## 11 Zanahoria T2           0      0  
## 12 Zanahoria T3           0      0
# Tukey's test
tukey <- TukeyHSD(anova)
print(tukey)
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: aov(formula = df1$Severidad ~ df1$Tratamiento * df1$Variedad, data = df1)
## 
## $`df1$Tratamiento`
##            diff       lwr       upr     p adj
## T2-T1 12.146429  8.187937 16.104920 0.0000000
## T3-T1 14.460714 10.502223 18.419206 0.0000000
## T3-T2  2.314286 -1.644206  6.272777 0.3557498
## 
## $`df1$Variedad`
##                              diff        lwr        upr    p adj
## Pastusa-Criolla     -8.523810e+00 -13.535284  -3.512335 7.94e-05
## Remolacha-Criolla   -2.200000e+01 -27.011475 -16.988525 0.00e+00
## Zanahoria-Criolla   -2.200000e+01 -27.011475 -16.988525 0.00e+00
## Remolacha-Pastusa   -1.347619e+01 -18.487665  -8.464716 0.00e+00
## Zanahoria-Pastusa   -1.347619e+01 -18.487665  -8.464716 0.00e+00
## Zanahoria-Remolacha  1.551352e-13  -5.011475   5.011475 1.00e+00
## 
## $`df1$Tratamiento:df1$Variedad`
##                                    diff        lwr        upr     p adj
## T2:Criolla-T1:Criolla      4.160000e+01  30.548772  52.651228 0.0000000
## T3:Criolla-T1:Criolla      2.440000e+01  13.348772  35.451228 0.0000000
## T1:Pastusa-T1:Criolla     -1.158692e-13 -11.051228  11.051228 1.0000000
## T2:Pastusa-T1:Criolla      6.985714e+00  -4.065513  18.036942 0.6432300
## T3:Pastusa-T1:Criolla      3.344286e+01  22.391629  44.494085 0.0000000
## T1:Remolacha-T1:Criolla   -2.202682e-13 -11.051228  11.051228 1.0000000
## T2:Remolacha-T1:Criolla   -1.487699e-13 -11.051228  11.051228 1.0000000
## T3:Remolacha-T1:Criolla   -2.993161e-13 -11.051228  11.051228 1.0000000
## T1:Zanahoria-T1:Criolla   -9.769963e-14 -11.051228  11.051228 1.0000000
## T2:Zanahoria-T1:Criolla    1.079137e-13 -11.051228  11.051228 1.0000000
## T3:Zanahoria-T1:Criolla   -2.620126e-13 -11.051228  11.051228 1.0000000
## T3:Criolla-T2:Criolla     -1.720000e+01 -28.251228  -6.148772 0.0000269
## T1:Pastusa-T2:Criolla     -4.160000e+01 -52.651228 -30.548772 0.0000000
## T2:Pastusa-T2:Criolla     -3.461429e+01 -45.665513 -23.563058 0.0000000
## T3:Pastusa-T2:Criolla     -8.157143e+00 -19.208371   2.894085 0.3941168
## T1:Remolacha-T2:Criolla   -4.160000e+01 -52.651228 -30.548772 0.0000000
## T2:Remolacha-T2:Criolla   -4.160000e+01 -52.651228 -30.548772 0.0000000
## T3:Remolacha-T2:Criolla   -4.160000e+01 -52.651228 -30.548772 0.0000000
## T1:Zanahoria-T2:Criolla   -4.160000e+01 -52.651228 -30.548772 0.0000000
## T2:Zanahoria-T2:Criolla   -4.160000e+01 -52.651228 -30.548772 0.0000000
## T3:Zanahoria-T2:Criolla   -4.160000e+01 -52.651228 -30.548772 0.0000000
## T1:Pastusa-T3:Criolla     -2.440000e+01 -35.451228 -13.348772 0.0000000
## T2:Pastusa-T3:Criolla     -1.741429e+01 -28.465513  -6.363058 0.0000195
## T3:Pastusa-T3:Criolla      9.042857e+00  -2.008371  20.094085 0.2371296
## T1:Remolacha-T3:Criolla   -2.440000e+01 -35.451228 -13.348772 0.0000000
## T2:Remolacha-T3:Criolla   -2.440000e+01 -35.451228 -13.348772 0.0000000
## T3:Remolacha-T3:Criolla   -2.440000e+01 -35.451228 -13.348772 0.0000000
## T1:Zanahoria-T3:Criolla   -2.440000e+01 -35.451228 -13.348772 0.0000000
## T2:Zanahoria-T3:Criolla   -2.440000e+01 -35.451228 -13.348772 0.0000000
## T3:Zanahoria-T3:Criolla   -2.440000e+01 -35.451228 -13.348772 0.0000000
## T2:Pastusa-T1:Pastusa      6.985714e+00  -4.065513  18.036942 0.6432300
## T3:Pastusa-T1:Pastusa      3.344286e+01  22.391629  44.494085 0.0000000
## T1:Remolacha-T1:Pastusa   -1.043990e-13 -11.051228  11.051228 1.0000000
## T2:Remolacha-T1:Pastusa   -3.290067e-14 -11.051228  11.051228 1.0000000
## T3:Remolacha-T1:Pastusa   -1.834469e-13 -11.051228  11.051228 1.0000000
## T1:Zanahoria-T1:Pastusa    1.816959e-14 -11.051228  11.051228 1.0000000
## T2:Zanahoria-T1:Pastusa    2.237829e-13 -11.051228  11.051228 1.0000000
## T3:Zanahoria-T1:Pastusa   -1.461434e-13 -11.051228  11.051228 1.0000000
## T3:Pastusa-T2:Pastusa      2.645714e+01  15.405915  37.508371 0.0000000
## T1:Remolacha-T2:Pastusa   -6.985714e+00 -18.036942   4.065513 0.6432300
## T2:Remolacha-T2:Pastusa   -6.985714e+00 -18.036942   4.065513 0.6432300
## T3:Remolacha-T2:Pastusa   -6.985714e+00 -18.036942   4.065513 0.6432300
## T1:Zanahoria-T2:Pastusa   -6.985714e+00 -18.036942   4.065513 0.6432300
## T2:Zanahoria-T2:Pastusa   -6.985714e+00 -18.036942   4.065513 0.6432300
## T3:Zanahoria-T2:Pastusa   -6.985714e+00 -18.036942   4.065513 0.6432300
## T1:Remolacha-T3:Pastusa   -3.344286e+01 -44.494085 -22.391629 0.0000000
## T2:Remolacha-T3:Pastusa   -3.344286e+01 -44.494085 -22.391629 0.0000000
## T3:Remolacha-T3:Pastusa   -3.344286e+01 -44.494085 -22.391629 0.0000000
## T1:Zanahoria-T3:Pastusa   -3.344286e+01 -44.494085 -22.391629 0.0000000
## T2:Zanahoria-T3:Pastusa   -3.344286e+01 -44.494085 -22.391629 0.0000000
## T3:Zanahoria-T3:Pastusa   -3.344286e+01 -44.494085 -22.391629 0.0000000
## T2:Remolacha-T1:Remolacha  7.149836e-14 -11.051228  11.051228 1.0000000
## T3:Remolacha-T1:Remolacha -7.904788e-14 -11.051228  11.051228 1.0000000
## T1:Zanahoria-T1:Remolacha  1.225686e-13 -11.051228  11.051228 1.0000000
## T2:Zanahoria-T1:Remolacha  3.281819e-13 -11.051228  11.051228 1.0000000
## T3:Zanahoria-T1:Remolacha -4.174439e-14 -11.051228  11.051228 1.0000000
## T3:Remolacha-T2:Remolacha -1.505462e-13 -11.051228  11.051228 1.0000000
## T1:Zanahoria-T2:Remolacha  5.107026e-14 -11.051228  11.051228 1.0000000
## T2:Zanahoria-T2:Remolacha  2.566836e-13 -11.051228  11.051228 1.0000000
## T3:Zanahoria-T2:Remolacha -1.132427e-13 -11.051228  11.051228 1.0000000
## T1:Zanahoria-T3:Remolacha  2.016165e-13 -11.051228  11.051228 1.0000000
## T2:Zanahoria-T3:Remolacha  4.072298e-13 -11.051228  11.051228 1.0000000
## T3:Zanahoria-T3:Remolacha  3.730349e-14 -11.051228  11.051228 1.0000000
## T2:Zanahoria-T1:Zanahoria  2.056133e-13 -11.051228  11.051228 1.0000000
## T3:Zanahoria-T1:Zanahoria -1.643130e-13 -11.051228  11.051228 1.0000000
## T3:Zanahoria-T2:Zanahoria -3.699263e-13 -11.051228  11.051228 1.0000000
# creating the compact letter display
tukey.cld <- multcompLetters4(anova, tukey)
print(tukey.cld)
## $`df1$Tratamiento`
##  T3  T2  T1 
## "a" "a" "b" 
## 
## $`df1$Variedad`
##   Criolla   Pastusa Remolacha Zanahoria 
##       "a"       "b"       "c"       "c" 
## 
## $`df1$Tratamiento:df1$Variedad`
##   T2:Criolla   T3:Pastusa   T3:Criolla   T2:Pastusa   T1:Criolla   T1:Pastusa 
##          "a"         "ab"          "b"          "c"          "c"          "c" 
## T1:Remolacha T2:Remolacha T3:Remolacha T1:Zanahoria T2:Zanahoria T3:Zanahoria 
##          "c"          "c"          "c"          "c"          "c"          "c"
# adding the compact letter display to the table with means and sd
cld <- as.data.frame.list(tukey.cld$`df1$Tratamiento:df1$Variedad`)
data_summary$Tukey <- cld$Letters
print(data_summary)
## # A tibble: 12 × 5
## # Groups:   Variedad [4]
##    Variedad  Tratamiento  mean    sd Tukey
##    <fct>     <fct>       <dbl> <dbl> <chr>
##  1 Criolla   T2          41.6   40.9 a    
##  2 Pastusa   T3          33.4   38.2 ab   
##  3 Criolla   T3          24.4   35.9 b    
##  4 Pastusa   T2           6.99  19.0 c    
##  5 Criolla   T1           0      0   c    
##  6 Pastusa   T1           0      0   c    
##  7 Remolacha T1           0      0   c    
##  8 Remolacha T2           0      0   c    
##  9 Remolacha T3           0      0   c    
## 10 Zanahoria T1           0      0   c    
## 11 Zanahoria T2           0      0   c    
## 12 Zanahoria T3           0      0   c
# coloured barplot
ggplot(data_summary, aes(x = factor(Tratamiento), y = mean)) + 
  geom_bar(stat = "identity")

# coloured barplot
ggplot(data_summary, aes(x = Variedad, y = mean, fill = Tratamiento, colour = Tratamiento)) + 
  geom_bar(stat = "identity", position = "dodge", alpha = 0.5)  +
  geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), position = position_dodge(0.9), width = 0.25,
                show.legend = FALSE) +
  labs(x="Variedades", y="Severidad (%)") +
  theme_bw() + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  theme(legend.position = c(0.1, 0.75)) +
  geom_text(aes(label=Tukey), position = position_dodge(0.90), size = 3, 
            vjust=-0.8, hjust=-0.5, colour = "gray25") +
  ylim(0, 200) +
  geom_text(aes(label=Tratamiento, y = 100), position = position_dodge(0.90), show.legend = FALSE) +
  scale_fill_brewer(palette = "Dark2") +
  scale_color_brewer(palette = "Dark2")

Severidad1 <- read_excel("C:\\Users\\USER\\Documents\\UNIVERSIDAD F\\2022-1\\Fitopatologia\\Fotos fito 2.xlsx",sheet = "Hoja3")
df2=data.frame(Severidad1)

##Grafica Sveridad

ggplot(df2)+aes(Tratamiento, Severidad, color=Tratamiento)+geom_point(size = 2)+geom_line(aes(group=Tratamiento))+facet_wrap(.~Muestreos, ncol = 5)

##Incidencia

##Muestreo 2

Severidad2 <- read_excel("C:\\Users\\USER\\Documents\\UNIVERSIDAD F\\2022-1\\Fitopatologia\\Fotos fito 2.xlsx",sheet = "T2")
df3=data.frame(Severidad2)
ggplot(df3)+aes(Variedad, Incidencia, color=Variedad)+geom_point(size = 2)+geom_line(aes(group=Variedad))+facet_wrap(.~Muestreos, ncol = 5)
## Warning: Removed 220 rows containing missing values (geom_point).
## Warning: Removed 164 row(s) containing missing values (geom_path).

##Muestreo 3

Severidad3 <- read_excel("C:\\Users\\USER\\Documents\\UNIVERSIDAD F\\2022-1\\Fitopatologia\\Fotos fito 2.xlsx",sheet = "T3")
df4=data.frame(Severidad3)
ggplot(df4)+aes(Variedad, Incidencia, color=Variedad)+geom_point(size = 2)+geom_line(aes(group=Variedad))+facet_wrap(.~Muestreos, ncol = 5)
## Warning: Removed 194 rows containing missing values (geom_point).
## Warning: Removed 155 row(s) containing missing values (geom_path).