knitr::opts_chunk$set(echo = TRUE)

#1. Area entre curvas de dos Funciones

y1 <- function(x) {x^2/2}
y2 <- function(x) {2*x}
curve(y1,from = 0,to = 4, ylim = c(0,12),type = 'l')
curve(y2,from = 0,to =4, add = T, col ='blue',type = 'l')

x <- seq(0,4, l=100)
polygon(x,y1(x),x,y2(x), col = 'darkcyan',density = 30)
text(2,3,'A')
text(1,4,'f(x)')
text(2,3,'A')

x0 <- 2.5
segments(x0,y1(x0),x0,y2(x0), col = 'blue')
x0 <- 2.6
segments(x0,y1(x0),x0,y2(x0), col = 'blue')
text(2.55,y2(2.55),'dx', pos=3)

\[ dA=(y_2 - y_1)*dx \] \[A=\int dA=\int(y_2-y_1)dx=\int(2x-\frac{x^2}{2})dx \]

\[ A = \int_{0}^{4}((2x)-(\frac{x^2}{2}))\ dx = (x^2-\frac{x^3}{6})|_{0}^{4}\]

y1 <- function(x) {x^2/2}
y2 <- function(x) {2*x}
curve(y1,from = 0,to = 4, ylim = c(0,10),col = 'red',type = 'l',lwd=2)
curve(y2,from = 0,to =4, add = T, col ='red3',type = 'l',lwd=2)

abline(h=c(0,8),v=c(0,4))

n <- 10000
xn <- runif(n,0,4)
yn <- runif(n,0,8)

points(xn,yn,pch = 16, cex = 0.5, col = gray(0.5))

men_y2 <-sum(yn<=y2(seq(0,4,l=n)))
men_y1 <-sum(yn<=y1(seq(0,4,l=n)))
men_y1;men_y2
## [1] 3316
## [1] 4948
puntos_A <- men_y2 - men_y1;puntos_A
## [1] 1632
(puntos_A*32)/n
## [1] 5.2224
library(agricolae)
dates<-c(14,21,28) 

evaluation<-c(40,80,90)
audps(evaluation,dates)
## evaluation 
##       1470
audps(evaluation,dates,"relative")
## evaluation 
##        0.7
mi_audps <- function(t,y){
  audps(y,t)
}
tiempos <- seq(7,70,7)
daño <- sort(runif(n=length(tiempos),
                   min=5,20))
mi_audps(tiempos,daño)
## evaluation 
##   718.2753
mi_trat_audps <- function(yt){
  salida = c()
  t <- unlist(yt[,1])
  for(i in seq(2,ncol(yt))){
    salida[i] <- mi_audps(t=t,y=unlist(yt[,i]))
  }
  salida <- salida[-1]
  return(salida)
}
library(readxl)
df_da <- read_excel("d:/Users/Janus/Documents/Computacion estadistica/datos_audps.xlsx")
df_da
audps_trat <- mi_trat_audps(df_da)

dfg <- data.frame(t = rep(df_da$Tiempo, 3),
                 y = c(df_da$D_t1,df_da$D_t2,df_da$D_t3),
                 trat = rep(1:3, each=nrow(df_da)));dfg
f_texto <- data.frame(label = round(audps_trat,2),
                      trat = 1:3)

#2. Entendiendo la funcion Unlist()

df_da$Tiempo
##  [1]  7 14 21 28 35 42 49 56 63 70
df_da[,1]
unlist(df_da[,1])
##  Tiempo1  Tiempo2  Tiempo3  Tiempo4  Tiempo5  Tiempo6  Tiempo7  Tiempo8 
##        7       14       21       28       35       42       49       56 
##  Tiempo9 Tiempo10 
##       63       70
class(df_da$Tiempo)
## [1] "numeric"
class(df_da[,1])
## [1] "tbl_df"     "tbl"        "data.frame"
class(unlist(df_da[,1]))
## [1] "numeric"

#3. Grafica libreria GGplot

library(ggplot2)
ggplot(data = dfg, aes(x = t, y = y))+
  geom_line()+
  geom_text(data = f_texto,
            mapping = aes(x = 20, y = 60, label = label))+
  facet_wrap(~trat)

#4. Grafica libreria Lattice

library(lattice)
xyplot(y~t|trat, data = dfg, t='l')

#5. Grafica libreria GGpubr

library(ggpubr)
dfg$trat <- as.factor(dfg$trat)
dfg$t <- rownames(dfg)
ggviolin(dfg, x = "trat", y = "y", fill = "trat",
         palette = c("cyan","green","firebrick1"),
         add = "boxplot",
         add.params = list(fill = "white"),
         ylab = 'Daño',
         xlab = 'Tratamiento',
         width = 0.8,
         ggtheme = theme_dark())

#6. Grafica lbreria plotly

library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
fig <- plot_ly(dfg, x = df_da$Tiempo, y = df_da$D_t1, type = 'bar', name = 'Trat1',
        marker = list(color = 'chocolate3'))
fig <- fig %>% add_trace(y = df_da$D_t2, name = 'Trat2', marker = list(color = 'cyan3'))
fig <- fig %>% add_trace(y = df_da$D_t3, name = 'Trat3', marker = list(color = 'firebrick2'))
fig <- fig %>% layout(title = 'Daño en plantas según tratamiento',
         xaxis = list(
           title = "Tiempo en Días",
           tickfont = list(
             size = 14,
             color = 'chocolate3')),
         yaxis = list(
           title = 'Daño',
           titlefont = list(
             size = 16,
             color = 'cyan3'),
           tickfont = list(
             size = 14,
             color = 'firebrick2')),
         legend = list(x = 0, y = 1, bgcolor = 'white', bordercolor = 'black'),
         barmode = 'group', bargap = 0.15, bargroupgap = 0.1)

fig
## Warning: `arrange_()` is deprecated as of dplyr 0.7.0.
## Please use `arrange()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
## Warning: 'layout' objects don't have these attributes: 'bargroupgap'
## Valid attributes include:
## 'font', 'title', 'uniformtext', 'autosize', 'width', 'height', 'margin', 'paper_bgcolor', 'plot_bgcolor', 'separators', 'hidesources', 'showlegend', 'colorway', 'datarevision', 'uirevision', 'editrevision', 'selectionrevision', 'template', 'modebar', 'meta', 'transition', '_deprecated', 'clickmode', 'dragmode', 'hovermode', 'hoverdistance', 'spikedistance', 'hoverlabel', 'selectdirection', 'grid', 'calendar', 'xaxis', 'yaxis', 'ternary', 'scene', 'geo', 'mapbox', 'polar', 'radialaxis', 'angularaxis', 'direction', 'orientation', 'editType', 'legend', 'annotations', 'shapes', 'images', 'updatemenus', 'sliders', 'colorscale', 'coloraxis', 'metasrc', 'barmode', 'bargap', 'mapType'