#Script para arboles 

library(mapview)
## Warning: package 'mapview' was built under R version 3.6.1
library(raster)
## Warning: package 'raster' was built under R version 3.6.1
## Loading required package: sp
## Warning: package 'sp' was built under R version 3.6.1
library(sf)
## Warning: package 'sf' was built under R version 3.6.3
## Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.6.1
library(rgdal)
## Warning: package 'rgdal' was built under R version 3.6.1
## rgdal: version: 1.4-4, (SVN revision 833)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.2.3, released 2017/11/20
##  Path to GDAL shared files: C:/Users/Samsung/Documents/R/win-library/3.6/rgdal/gdal
##  GDAL binary built with GEOS: TRUE 
##  Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
##  Path to PROJ.4 shared files: C:/Users/Samsung/Documents/R/win-library/3.6/rgdal/proj
##  Linking to sp version: 1.3-1
pal = mapviewPalette("mapviewTopoColors")

Nogales <- raster("NDVI1.tif", package = "mapview")
NogalNDVI <- stack(Nogales)

mapview(NogalNDVI[[1]], col.regions = pal(100), at = seq(-0.2, 1, 0.2), legend = TRUE)
## Warning in rasterCheckSize(x, maxpixels = maxpixels): maximum number of pixels for Raster* viewing is 5e+05 ; 
## the supplied Raster* has 2971392 
##  ... decreasing Raster* resolution to 5e+05 pixels
##  to view full resolution set 'maxpixels =  2971392 '
# the distribution of values in the raster
hist(NogalNDVI, main="Distribucion de los valores de NDVI", 
     col= "purple", 
     maxpixels=22000000)

#Visualizar los datos 

plot(NogalNDVI, 
     main="NDVI de arboles de nogal") 

summary(NogalNDVI)
## Warning in .local(object, ...): summary is an estimate based on a sample of 1e+05 cells (3.37% of all cells)
##                NDVI1
## Min.    0.000000e+00
## 1st Qu. 2.439024e-01
## Median  4.571429e-01
## 3rd Qu. 7.465754e-01
## Max.    9.767442e-01
## NA's    2.498640e+05
#para filtrar los valores 
image(NogalNDVI, zlim=c(0.5,0.9))

#Grafico de caja y bigote:
boxplot(NogalNDVI)

arboles <- st_read("areaarboles.shp")
## Reading layer `areaarboles' from data source `C:\Users\Samsung\Documents\APLICADA\EQUIPO6MJ\areaarboles.shp' using driver `ESRI Shapefile'
## Simple feature collection with 16072 features and 3 fields
## geometry type:  POLYGON
## dimension:      XY
## bbox:           xmin: 221797.9 ymin: 3389726 xmax: 222235.7 ymax: 3389776
## epsg (SRID):    32613
## proj4string:    +proj=utm +zone=13 +datum=WGS84 +units=m +no_defs
ggplot(arboles) +
  geom_sf()

mapview::mapview(arboles,labels=F)