library(knitr)
## Warning: package 'knitr' was built under R version 4.0.5
getwd()
## [1] "C:/Users/Acer/Desktop"
setwd( "C:/Users/Acer/Desktop")
###Creating dataframe##Dataframe name spp##


facto<-read.csv( "C:/Users/Acer/Desktop/eggplant.csv")
###Verification of imported data###


names(facto)
##  [1] "Factor.A"                "Factor.B"               
##  [3] "Samples"                 "Week"                   
##  [5] "Plant.ht..2wk"           "Plant.ht..3wk"          
##  [7] "NoLvs..2wk"              "NLvs..3wk"              
##  [9] "Canopy.Cover..1MAT"      "Chlorophyll.Top...3wk"  
## [11] "Chloropyll.Bottom..3.wk"
head(facto)
##   Factor.A Factor.B Samples Week Plant.ht..2wk Plant.ht..3wk NoLvs..2wk
## 1       A1       B1       1    2           9.2           9.6          5
## 2       A1       B1       2    2           8.8           7.5          6
## 3       A1       B1       3    2           8.2           9.6          5
## 4       A1       B1       4    2           8.2          10.6          6
## 5       A1       B1       5    2           7.1           9.8          5
## 6       A1       B1       6    2           9.6          11.4          7
##   NLvs..3wk Canopy.Cover..1MAT Chlorophyll.Top...3wk Chloropyll.Bottom..3.wk
## 1         6               6.28                    NA                      NA
## 2        NA              11.82                    NA                      NA
## 3        NA               8.34                    NA                      NA
## 4        NA               5.80                    NA                      NA
## 5        NA               9.17                    NA                      NA
## 6        NA              12.71                    NA                      NA
str(facto)
## 'data.frame':    24 obs. of  11 variables:
##  $ Factor.A               : chr  "A1" "A1" "A1" "A1" ...
##  $ Factor.B               : chr  "B1" "B1" "B1" "B1" ...
##  $ Samples                : int  1 2 3 4 5 6 1 2 3 4 ...
##  $ Week                   : int  2 2 2 2 2 2 2 2 2 2 ...
##  $ Plant.ht..2wk          : num  9.2 8.8 8.2 8.2 7.1 9.6 5.5 5.5 8.5 7.5 ...
##  $ Plant.ht..3wk          : num  9.6 7.5 9.6 10.6 9.8 11.4 10.4 11.3 11.6 10 ...
##  $ NoLvs..2wk             : int  5 6 5 6 5 7 4 4 5 6 ...
##  $ NLvs..3wk              : int  6 NA NA NA NA NA 9 6 3 7 ...
##  $ Canopy.Cover..1MAT     : num  6.28 11.82 8.34 5.8 9.17 ...
##  $ Chlorophyll.Top...3wk  : logi  NA NA NA NA NA NA ...
##  $ Chloropyll.Bottom..3.wk: logi  NA NA NA NA NA NA ...
###installing libraries###


library (agricolae)
## Warning: package 'agricolae' was built under R version 4.0.5
library(ggplot2)
###Anova Factorial##

factoanov<-aov(Canopy.Cover..1MAT~Factor.A*Factor.B, data = facto)


summary(factoanov)
##                   Df Sum Sq Mean Sq F value   Pr(>F)    
## Factor.A           1    2.7     2.7   0.081   0.7791    
## Factor.B           1 2347.1  2347.1  69.737 5.97e-08 ***
## Factor.A:Factor.B  1  155.2   155.2   4.613   0.0442 *  
## Residuals         20  673.1    33.7                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
###interaction plots###

with(facto, interaction.plot(x.factor = Factor.B, 
                           trace.factor = Factor.A, 
                           response = Canopy.Cover..1MAT))

###interaction plots with error bars###

library (ggpubr)
## Warning: package 'ggpubr' was built under R version 4.0.5
ggline(facto, x = "Factor.B", y = "Canopy.Cover..1MAT", color = "Factor.A",
       add = c("mean_se", "dotplot"),
       palette = c("#0384fc", "gold"))
## Bin width defaults to 1/30 of the range of the data. Pick better value with `binwidth`.

####Factorial Post Hoc, Tukey HSD## 

tukey<-TukeyHSD(factoanov)

library(multcompView)

tukey.cld <- multcompLetters4(factoanov, tukey)
print(tukey.cld)
## $Factor.A
## $Factor.A$Letters
##  A1  A2 
## "a" "a" 
## 
## $Factor.A$LetterMatrix
##       a
## A1 TRUE
## A2 TRUE
## 
## 
## $Factor.B
##  B2  B1 
## "a" "b" 
## 
## $`Factor.A:Factor.B`
## A2:B2 A1:B2 A1:B1 A2:B1 
##   "a"   "a"   "b"   "b"