library(ggplot2)

library(ggmap)
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(readxl)

library(tidyverse)
## -- Attaching packages -------------------------------------------------------------------- tidyverse 1.3.0 --
## v tibble  2.1.3     v dplyr   0.8.3
## v tidyr   1.0.2     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.4.0
## v purrr   0.3.3
## -- Conflicts ----------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(sp)

library(maptools)
## Checking rgeos availability: FALSE
##      Note: when rgeos is not available, polygon geometry     computations in maptools depend on gpclib,
##      which has a restricted licence. It is disabled by default;
##      to enable gpclib, type gpclibPermit()
X2007sep <- read_excel("2007sep.xlsx")
View(X2007sep)
Huatabampo  <- (X2007sep)

##Datos disponibles de pozos 
library(DT)

datatable(X2007sep)
str(Huatabampo)
## Classes 'tbl_df', 'tbl' and 'data.frame':    293 obs. of  10 variables:
##  $ MODULO: num  1 1 1 1 1 1 1 1 1 1 ...
##  $ POZO  : chr  "1" "2" "3" "4" ...
##  $ X     : num  620903 620915 620943 620879 620888 ...
##  $ Y     : num  2962392 2963671 2964903 2965667 2966604 ...
##  $ SNM   : num  3.91 4.53 2.8 3.64 3.49 ...
##  $ NF    : num  2.68 2.61 1.3 2.14 2.01 2 1.63 2.82 3 3 ...
##  $ CE    : num  2.83 8.35 8.66 8.34 9.18 7.9 9.64 7.17 1.88 1.93 ...
##  $ PPM   : num  1811 5344 5542 5338 5875 ...
##  $ PH    : num  6.8 6.9 6.8 7.1 6.6 6.8 6.5 6.9 7 7 ...
##  $ TEMP  : num  28.5 29.2 28.9 29.4 28.3 28.4 28 27.5 28.7 28.6 ...
PPM <- Huatabampo$PPM
PH <- Huatabampo$PH
NF <- Huatabampo$NF
SNM <- Huatabampo$SNM


datos <- data.frame(PPM, PH, NF, SNM)

#Primer gr攼㸱fico de correlaci昼㸳n

pairs(datos)

cor(NF, SNM, method = c("pearson", "kendall", "spearman"))
## [1] 0.67311
cor.test(NF, SNM, method=c("pearson", "kendall", "spearman"))
## 
##  Pearson's product-moment correlation
## 
## data:  NF and SNM
## t = 15.526, df = 291, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.6052017 0.7312929
## sample estimates:
##     cor 
## 0.67311
ggplot(Huatabampo, aes(x = SNM, y = NF)) + 
  geom_point()

cor(SNM, NF, method = c("pearson", "kendall", "spearman"))
## [1] 0.67311
cor.test(SNM, NF, method=c("pearson", "kendall", "spearman"))
## 
##  Pearson's product-moment correlation
## 
## data:  SNM and NF
## t = 15.526, df = 291, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.6052017 0.7312929
## sample estimates:
##     cor 
## 0.67311
# Boxplot of SNM VS NF
# Boxplot of weight vs. weeks
ggplot(data = Huatabampo, 
       aes(x = cut(SNM, breaks = 4), y = NF)) + 
  geom_boxplot()

ggplot(data = Huatabampo, 
       aes(x = cut(SNM, breaks = 4), y = PPM)) + 
  geom_boxplot()

library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggmap':
## 
##     wind
## 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
Huat <- ggplot(Huatabampo, aes(x = NF, y = PPM, color = factor(MODULO))) + 
  geom_point()

ggplotly(Huat)
library(dplyr)
library(tidyr)
library(ggplot2)
library(broom)

library(dplyr)
Huatabampo %>%
  filter(NF <=2.9 ) %>%
  ggplot(aes(x = NF, y = SNM)) +
  geom_point()

# Compute correlation
Huatabampo %>%
  summarize(N = n(), r = cor(SNM, NF))
## # A tibble: 1 x 2
##       N     r
##   <int> <dbl>
## 1   293 0.673
#Para hacer el mapa 
library(sf)
## Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
library(rgdal)
## rgdal: version: 1.4-8, (SVN revision 845)
##  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/braya/OneDrive/Documentos/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/braya/OneDrive/Documentos/R/win-library/3.6/rgdal/proj
##  Linking to sp version: 1.4-0
library(gstat)

library(RSAGA)
## Loading required package: shapefiles
## Loading required package: foreign
## 
## Attaching package: 'shapefiles'
## The following objects are masked from 'package:foreign':
## 
##     read.dbf, write.dbf
## Loading required package: plyr
## ------------------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## ------------------------------------------------------------------------------
## 
## Attaching package: 'plyr'
## The following objects are masked from 'package:plotly':
## 
##     arrange, mutate, rename, summarise
## The following objects are masked from 'package:dplyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
## The following object is masked from 'package:purrr':
## 
##     compact
library(raster)
## 
## Attaching package: 'raster'
## The following object is masked from 'package:plotly':
## 
##     select
## The following object is masked from 'package:dplyr':
## 
##     select
## The following object is masked from 'package:tidyr':
## 
##     extract
########
library(dplyr)
library(tidyr)
library(ggplot2)
library(broom)

#Graficas de linea que nos dicen como se comportan los datos en funcion de otro
ggplot(data = datos , aes(x = PH)) + 
  geom_bar(fill="red")

ggplot(X2007sep, aes(x=TEMP, y=PH, group=5)) + geom_line() + geom_point()