####Programas y Set up ###Desacargar Base

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
library(ggplot2)
library(readxl)

df_bank <- read_csv("C:\\Users\\sguerra\\Downloads\\bank.csv")
## Rows: 4521 Columns: 17
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (10): job, marital, education, default, housing, loan, contact, month, p...
## dbl  (7): age, balance, day, duration, campaign, pdays, previous
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
View (df_bank)
str (df_bank) #structure, nos dice que tipo de variables tenemos
## spc_tbl_ [4,521 × 17] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ age      : num [1:4521] 30 33 35 30 59 35 36 39 41 43 ...
##  $ job      : chr [1:4521] "unemployed" "services" "management" "management" ...
##  $ marital  : chr [1:4521] "married" "married" "single" "married" ...
##  $ education: chr [1:4521] "primary" "secondary" "tertiary" "tertiary" ...
##  $ default  : chr [1:4521] "no" "no" "no" "no" ...
##  $ balance  : num [1:4521] 1787 4789 1350 1476 0 ...
##  $ housing  : chr [1:4521] "no" "yes" "yes" "yes" ...
##  $ loan     : chr [1:4521] "no" "yes" "no" "yes" ...
##  $ contact  : chr [1:4521] "cellular" "cellular" "cellular" "unknown" ...
##  $ day      : num [1:4521] 19 11 16 3 5 23 14 6 14 17 ...
##  $ month    : chr [1:4521] "oct" "may" "apr" "jun" ...
##  $ duration : num [1:4521] 79 220 185 199 226 141 341 151 57 313 ...
##  $ campaign : num [1:4521] 1 1 1 4 1 2 1 2 2 1 ...
##  $ pdays    : num [1:4521] -1 339 330 -1 -1 176 330 -1 -1 147 ...
##  $ previous : num [1:4521] 0 4 1 0 0 3 2 0 0 2 ...
##  $ poutcome : chr [1:4521] "unknown" "failure" "failure" "unknown" ...
##  $ y        : chr [1:4521] "no" "no" "no" "no" ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   age = col_double(),
##   ..   job = col_character(),
##   ..   marital = col_character(),
##   ..   education = col_character(),
##   ..   default = col_character(),
##   ..   balance = col_double(),
##   ..   housing = col_character(),
##   ..   loan = col_character(),
##   ..   contact = col_character(),
##   ..   day = col_double(),
##   ..   month = col_character(),
##   ..   duration = col_double(),
##   ..   campaign = col_double(),
##   ..   pdays = col_double(),
##   ..   previous = col_double(),
##   ..   poutcome = col_character(),
##   ..   y = col_character()
##   .. )
##  - attr(*, "problems")=<externalptr>
summary(df_bank)
##       age            job              marital           education        
##  Min.   :19.00   Length:4521        Length:4521        Length:4521       
##  1st Qu.:33.00   Class :character   Class :character   Class :character  
##  Median :39.00   Mode  :character   Mode  :character   Mode  :character  
##  Mean   :41.17                                                           
##  3rd Qu.:49.00                                                           
##  Max.   :87.00                                                           
##    default             balance        housing              loan          
##  Length:4521        Min.   :-3313   Length:4521        Length:4521       
##  Class :character   1st Qu.:   69   Class :character   Class :character  
##  Mode  :character   Median :  444   Mode  :character   Mode  :character  
##                     Mean   : 1423                                        
##                     3rd Qu.: 1480                                        
##                     Max.   :71188                                        
##    contact               day           month              duration   
##  Length:4521        Min.   : 1.00   Length:4521        Min.   :   4  
##  Class :character   1st Qu.: 9.00   Class :character   1st Qu.: 104  
##  Mode  :character   Median :16.00   Mode  :character   Median : 185  
##                     Mean   :15.92                      Mean   : 264  
##                     3rd Qu.:21.00                      3rd Qu.: 329  
##                     Max.   :31.00                      Max.   :3025  
##     campaign          pdays           previous         poutcome        
##  Min.   : 1.000   Min.   : -1.00   Min.   : 0.0000   Length:4521       
##  1st Qu.: 1.000   1st Qu.: -1.00   1st Qu.: 0.0000   Class :character  
##  Median : 2.000   Median : -1.00   Median : 0.0000   Mode  :character  
##  Mean   : 2.794   Mean   : 39.77   Mean   : 0.5426                     
##  3rd Qu.: 3.000   3rd Qu.: -1.00   3rd Qu.: 0.0000                     
##  Max.   :50.000   Max.   :871.00   Max.   :25.0000                     
##       y            
##  Length:4521       
##  Class :character  
##  Mode  :character  
##                    
##                    
## 
glimpse(df_bank)
## Rows: 4,521
## Columns: 17
## $ age       <dbl> 30, 33, 35, 30, 59, 35, 36, 39, 41, 43, 39, 43, 36, 20, 31, …
## $ job       <chr> "unemployed", "services", "management", "management", "blue-…
## $ marital   <chr> "married", "married", "single", "married", "married", "singl…
## $ education <chr> "primary", "secondary", "tertiary", "tertiary", "secondary",…
## $ default   <chr> "no", "no", "no", "no", "no", "no", "no", "no", "no", "no", …
## $ balance   <dbl> 1787, 4789, 1350, 1476, 0, 747, 307, 147, 221, -88, 9374, 26…
## $ housing   <chr> "no", "yes", "yes", "yes", "yes", "no", "yes", "yes", "yes",…
## $ loan      <chr> "no", "yes", "no", "yes", "no", "no", "no", "no", "no", "yes…
## $ contact   <chr> "cellular", "cellular", "cellular", "unknown", "unknown", "c…
## $ day       <dbl> 19, 11, 16, 3, 5, 23, 14, 6, 14, 17, 20, 17, 13, 30, 29, 29,…
## $ month     <chr> "oct", "may", "apr", "jun", "may", "feb", "may", "may", "may…
## $ duration  <dbl> 79, 220, 185, 199, 226, 141, 341, 151, 57, 313, 273, 113, 32…
## $ campaign  <dbl> 1, 1, 1, 4, 1, 2, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 5, 1, 1, 1, …
## $ pdays     <dbl> -1, 339, 330, -1, -1, 176, 330, -1, -1, 147, -1, -1, -1, -1,…
## $ previous  <dbl> 0, 4, 1, 0, 0, 3, 2, 0, 0, 2, 0, 0, 0, 0, 1, 0, 0, 2, 0, 1, …
## $ poutcome  <chr> "unknown", "failure", "failure", "unknown", "unknown", "fail…
## $ y         <chr> "no", "no", "no", "no", "no", "no", "no", "no", "no", "no", …
str(df_bank)
## spc_tbl_ [4,521 × 17] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ age      : num [1:4521] 30 33 35 30 59 35 36 39 41 43 ...
##  $ job      : chr [1:4521] "unemployed" "services" "management" "management" ...
##  $ marital  : chr [1:4521] "married" "married" "single" "married" ...
##  $ education: chr [1:4521] "primary" "secondary" "tertiary" "tertiary" ...
##  $ default  : chr [1:4521] "no" "no" "no" "no" ...
##  $ balance  : num [1:4521] 1787 4789 1350 1476 0 ...
##  $ housing  : chr [1:4521] "no" "yes" "yes" "yes" ...
##  $ loan     : chr [1:4521] "no" "yes" "no" "yes" ...
##  $ contact  : chr [1:4521] "cellular" "cellular" "cellular" "unknown" ...
##  $ day      : num [1:4521] 19 11 16 3 5 23 14 6 14 17 ...
##  $ month    : chr [1:4521] "oct" "may" "apr" "jun" ...
##  $ duration : num [1:4521] 79 220 185 199 226 141 341 151 57 313 ...
##  $ campaign : num [1:4521] 1 1 1 4 1 2 1 2 2 1 ...
##  $ pdays    : num [1:4521] -1 339 330 -1 -1 176 330 -1 -1 147 ...
##  $ previous : num [1:4521] 0 4 1 0 0 3 2 0 0 2 ...
##  $ poutcome : chr [1:4521] "unknown" "failure" "failure" "unknown" ...
##  $ y        : chr [1:4521] "no" "no" "no" "no" ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   age = col_double(),
##   ..   job = col_character(),
##   ..   marital = col_character(),
##   ..   education = col_character(),
##   ..   default = col_character(),
##   ..   balance = col_double(),
##   ..   housing = col_character(),
##   ..   loan = col_character(),
##   ..   contact = col_character(),
##   ..   day = col_double(),
##   ..   month = col_character(),
##   ..   duration = col_double(),
##   ..   campaign = col_double(),
##   ..   pdays = col_double(),
##   ..   previous = col_double(),
##   ..   poutcome = col_character(),
##   ..   y = col_character()
##   .. )
##  - attr(*, "problems")=<externalptr>
summary(df_bank)
##       age            job              marital           education        
##  Min.   :19.00   Length:4521        Length:4521        Length:4521       
##  1st Qu.:33.00   Class :character   Class :character   Class :character  
##  Median :39.00   Mode  :character   Mode  :character   Mode  :character  
##  Mean   :41.17                                                           
##  3rd Qu.:49.00                                                           
##  Max.   :87.00                                                           
##    default             balance        housing              loan          
##  Length:4521        Min.   :-3313   Length:4521        Length:4521       
##  Class :character   1st Qu.:   69   Class :character   Class :character  
##  Mode  :character   Median :  444   Mode  :character   Mode  :character  
##                     Mean   : 1423                                        
##                     3rd Qu.: 1480                                        
##                     Max.   :71188                                        
##    contact               day           month              duration   
##  Length:4521        Min.   : 1.00   Length:4521        Min.   :   4  
##  Class :character   1st Qu.: 9.00   Class :character   1st Qu.: 104  
##  Mode  :character   Median :16.00   Mode  :character   Median : 185  
##                     Mean   :15.92                      Mean   : 264  
##                     3rd Qu.:21.00                      3rd Qu.: 329  
##                     Max.   :31.00                      Max.   :3025  
##     campaign          pdays           previous         poutcome        
##  Min.   : 1.000   Min.   : -1.00   Min.   : 0.0000   Length:4521       
##  1st Qu.: 1.000   1st Qu.: -1.00   1st Qu.: 0.0000   Class :character  
##  Median : 2.000   Median : -1.00   Median : 0.0000   Mode  :character  
##  Mean   : 2.794   Mean   : 39.77   Mean   : 0.5426                     
##  3rd Qu.: 3.000   3rd Qu.: -1.00   3rd Qu.: 0.0000                     
##  Max.   :50.000   Max.   :871.00   Max.   :25.0000                     
##       y            
##  Length:4521       
##  Class :character  
##  Mode  :character  
##                    
##                    
## 

###Histograma de Edad

hist(x = df_bank$age)

hist(x = df_bank$age, main = "HISTOGRAMA DE EDAD", col = "cadetblue", xlab = "Edad", ylab = "Frecuencia")

hist(x = df_bank$age, breaks = 3, main = "HISTOGRAMA DE EDAD", col = "cadetblue", xlab = "Edad", ylab = "Frecuencia")

###Grafica de educación

plot(factor(df_bank$education), main = "Gráfica Educación", col = c("blue","green","yellow","grey"), xlab = "Nivel de Educación", ylab = "Frecuencia")

plot(x = df_bank$age, y = df_bank$balance)

plot(x = df_bank$age, y= df_bank$balance, col = factor(df_bank$education %>% c("royalblue", "pink", "purple", "grey")))

plot(x=df_bank$age, y=df_bank$balance, col =factor(df_bank$loan))

legend 
## function (x, y = NULL, legend, fill = NULL, col = par("col"), 
##     border = "black", lty, lwd, pch, angle = 45, density = NULL, 
##     bty = "o", bg = par("bg"), box.lwd = par("lwd"), box.lty = par("lty"), 
##     box.col = par("fg"), pt.bg = NA, cex = 1, pt.cex = cex, pt.lwd = lwd, 
##     xjust = 0, yjust = 1, x.intersp = 1, y.intersp = 1, adj = c(0, 
##         0.5), text.width = NULL, text.col = par("col"), text.font = NULL, 
##     merge = do.lines && has.pch, trace = FALSE, plot = TRUE, 
##     ncol = 1, horiz = FALSE, title = NULL, inset = 0, xpd, title.col = text.col[1], 
##     title.adj = 0.5, title.cex = cex[1], title.font = text.font[1], 
##     seg.len = 2) 
## {
##     if (missing(legend) && !missing(y) && (is.character(y) || 
##         is.expression(y))) {
##         legend <- y
##         y <- NULL
##     }
##     mfill <- !missing(fill) || !missing(density)
##     if (!missing(xpd)) {
##         op <- par("xpd")
##         on.exit(par(xpd = op))
##         par(xpd = xpd)
##     }
##     text.font <- if (is.null(text.font)) 
##         par("font")
##     else text.font
##     title <- as.graphicsAnnot(title)
##     if (length(title) > 1) 
##         stop("invalid 'title'")
##     legend <- as.graphicsAnnot(legend)
##     if (any(sapply(legend, is.language))) 
##         legend <- as.expression(legend)
##     n.leg <- length(legend)
##     if (n.leg == 0) 
##         stop("'legend' is of length 0")
##     auto <- if (is.character(x)) 
##         match.arg(x, c("bottomright", "bottom", "bottomleft", 
##             "left", "topleft", "top", "topright", "right", "center"))
##     else NA
##     if (is.na(auto)) {
##         xy <- xy.coords(x, y, setLab = FALSE)
##         x <- xy$x
##         y <- xy$y
##         nx <- length(x)
##         if (nx < 1 || nx > 2) 
##             stop("invalid coordinate lengths")
##     }
##     else nx <- 0
##     reverse.xaxis <- par("xaxp")[1] > par("xaxp")[2]
##     reverse.yaxis <- par("yaxp")[1] > par("yaxp")[2]
##     xlog <- par("xlog")
##     ylog <- par("ylog")
##     cex <- rep(cex, length.out = n.leg)
##     x.intersp <- rep(x.intersp, length.out = n.leg)
##     seg.len <- rep(seg.len, length.out = n.leg)
##     rect2 <- function(left, top, dx, dy, density = NULL, angle, 
##         ...) {
##         r <- left + dx
##         if (xlog) {
##             left <- 10^left
##             r <- 10^r
##         }
##         b <- top - dy
##         if (ylog) {
##             top <- 10^top
##             b <- 10^b
##         }
##         rect(left, top, r, b, angle = angle, density = density, 
##             ...)
##     }
##     segments2 <- function(x1, y1, dx, dy, ...) {
##         x2 <- x1 + dx
##         if (xlog) {
##             x1 <- 10^x1
##             x2 <- 10^x2
##         }
##         y2 <- y1 + dy
##         if (ylog) {
##             y1 <- 10^y1
##             y2 <- 10^y2
##         }
##         segments(x1, y1, x2, y2, ...)
##     }
##     points2 <- function(x, y, ...) {
##         if (xlog) 
##             x <- 10^x
##         if (ylog) 
##             y <- 10^y
##         points(x, y, ...)
##     }
##     text2 <- function(x, y, ...) {
##         if (xlog) 
##             x <- 10^x
##         if (ylog) 
##             y <- 10^y
##         text(x, y, ...)
##     }
##     colwise <- function(x, n, ncol, n.legpercol, fun, reverse = FALSE) {
##         xmat <- matrix(c(rep(x, length.out = n), rep(0L, n.legpercol * 
##             ncol - n)), ncol = ncol)
##         res <- apply(xmat, 2, fun)
##         res[res == 0L] <- max(res)
##         if (reverse) 
##             -res
##         else res
##     }
##     rowwise <- function(x, n, ncol, n.legpercol, fun, reverse = FALSE) {
##         xmat <- matrix(c(rep(x, length.out = n), rep(0L, n.legpercol * 
##             ncol - n)), ncol = ncol)
##         res <- apply(xmat, 1, fun)
##         if (reverse) 
##             -res
##         else res
##     }
##     if (trace) {
##         catn <- function(...) do.call(cat, c(lapply(list(...), 
##             formatC), "\n"))
##         fv <- function(...) paste(vapply(lapply(list(...), formatC), 
##             paste, collapse = ",", ""), collapse = ", ")
##     }
##     n.legpercol <- if (horiz) {
##         if (ncol != 1) 
##             warning(gettextf("horizontal specification overrides: Number of columns := %d", 
##                 n.leg), domain = NA)
##         ncol <- n.leg
##         1
##     }
##     else ceiling(n.leg/ncol)
##     Cex <- cex * par("cex")
##     if (is.null(text.width)) 
##         text.width <- max(abs(mapply(strwidth, legend, cex = cex, 
##             font = text.font, MoreArgs = list(units = "user"))))
##     else if ((length(text.width) > 1L && any(is.na(text.width))) || 
##         (all(!is.na(text.width)) && (!is.numeric(text.width) || 
##             any(text.width < 0)))) 
##         stop("'text.width' must be numeric, >= 0, or a scalar NA")
##     if (auto.text.width <- all(is.na(text.width))) {
##         text.width <- abs(mapply(strwidth, legend, cex = cex, 
##             font = text.font, MoreArgs = list(units = "user")))
##         ncol <- ceiling(n.leg/n.legpercol)
##     }
##     xyc <- xyinch(par("cin"), warn.log = FALSE)
##     xc <- Cex * xyc[1L]
##     yc <- Cex * xyc[2L]
##     if (any(xc < 0)) 
##         text.width <- -text.width
##     xchar <- xc
##     xextra <- 0
##     y.intersp <- rep(y.intersp, length.out = n.legpercol)
##     yextra <- rowwise(yc, n = n.leg, ncol = ncol, n.legpercol = n.legpercol, 
##         fun = function(x) max(abs(x)), reverse = reverse.yaxis) * 
##         (y.intersp - 1)
##     ymax <- sign(yc[1]) * max(abs(yc)) * max(1, mapply(strheight, 
##         legend, cex = cex, font = text.font, MoreArgs = list(units = "user"))/yc)
##     ychar <- yextra + ymax
##     ymaxtitle <- title.cex * par("cex") * xyc[2L] * max(1, strheight(title, 
##         cex = title.cex, font = title.font, units = "user")/(title.cex * 
##         par("cex") * xyc[2L]))
##     ychartitle <- yextra[1] + ymaxtitle
##     if (trace) 
##         catn("  xchar=", fv(xchar), "; (yextra, ychar)=", fv(yextra, 
##             ychar))
##     if (mfill) {
##         xbox <- xc * 0.8
##         ybox <- yc * 0.5
##         dx.fill <- max(xbox)
##     }
##     do.lines <- (!missing(lty) && (is.character(lty) || any(lty > 
##         0))) || !missing(lwd)
##     has.pch <- !missing(pch) && length(pch) > 0
##     if (do.lines) {
##         x.off <- if (merge) 
##             -0.7
##         else 0
##     }
##     else if (merge) 
##         warning("'merge = TRUE' has no effect when no line segments are drawn")
##     if (has.pch) {
##         if (is.character(pch) && !is.na(pch[1L]) && nchar(pch[1L], 
##             type = "c") > 1) {
##             if (length(pch) > 1) 
##                 warning("not using pch[2..] since pch[1L] has multiple chars")
##             np <- nchar(pch[1L], type = "c")
##             pch <- substr(rep.int(pch[1L], np), 1L:np, 1L:np)
##         }
##         if (!is.character(pch)) 
##             pch <- as.integer(pch)
##     }
##     if (is.na(auto)) {
##         if (xlog) 
##             x <- log10(x)
##         if (ylog) 
##             y <- log10(y)
##     }
##     if (nx == 2) {
##         x <- sort(x)
##         y <- sort(y)
##         left <- x[1L]
##         top <- y[2L]
##         w <- diff(x)
##         h <- diff(y)
##         w0 <- w/ncol
##         x <- mean(x)
##         y <- mean(y)
##         if (missing(xjust)) 
##             xjust <- 0.5
##         if (missing(yjust)) 
##             yjust <- 0.5
##     }
##     else {
##         yc <- rowwise(yc, n.leg, ncol, n.legpercol, fun = function(x) max(abs(x)), 
##             reverse = reverse.yaxis)
##         h <- sum(ychar) + yc[length(yc)] + (!is.null(title)) * 
##             ychartitle
##         xch1 <- colwise(xchar, n.leg, ncol, n.legpercol, fun = function(x) max(abs(x)), 
##             reverse = reverse.xaxis)
##         x.interspCol <- colwise(x.intersp, n.leg, ncol, n.legpercol, 
##             fun = max)
##         seg.lenCol <- colwise(seg.len, n.leg, ncol, n.legpercol, 
##             fun = max)
##         text.width <- colwise(text.width, n = if (auto.text.width) 
##             n.leg
##         else ncol, ncol, n.legpercol = if (auto.text.width) 
##             n.legpercol
##         else 1, fun = function(x) max(abs(x)), reverse = reverse.xaxis)
##         w0 <- text.width + (x.interspCol + 1) * xch1
##         if (mfill) 
##             w0 <- w0 + dx.fill
##         if (do.lines) 
##             w0 <- w0 + (seg.lenCol + x.off) * xch1
##         w <- sum(w0) + 0.5 * xch1[ncol]
##         if (!is.null(title) && (abs(tw <- strwidth(title, units = "user", 
##             cex = title.cex, font = title.font) + 0.5 * title.cex * 
##             par("cex") * xyc[1L])) > abs(w)) {
##             xextra <- (tw - w)/2
##             w <- tw
##         }
##         if (is.na(auto)) {
##             left <- x - xjust * w
##             top <- y + (1 - yjust) * h
##         }
##         else {
##             usr <- par("usr")
##             inset <- rep_len(inset, 2)
##             insetx <- inset[1L] * (usr[2L] - usr[1L])
##             left <- switch(auto, bottomright = , topright = , 
##                 right = usr[2L] - w - insetx, bottomleft = , 
##                 left = , topleft = usr[1L] + insetx, bottom = , 
##                 top = , center = (usr[1L] + usr[2L] - w)/2)
##             insety <- inset[2L] * (usr[4L] - usr[3L])
##             top <- switch(auto, bottomright = , bottom = , bottomleft = usr[3L] + 
##                 h + insety, topleft = , top = , topright = usr[4L] - 
##                 insety, left = , right = , center = (usr[3L] + 
##                 usr[4L] + h)/2)
##         }
##     }
##     if (plot && bty != "n") {
##         if (trace) 
##             catn("  rect2(", left, ",", top, ", w=", w, ", h=", 
##                 h, ", ...)", sep = "")
##         rect2(left, top, dx = w, dy = h, col = bg, density = NULL, 
##             lwd = box.lwd, lty = box.lty, border = box.col)
##     }
##     xt <- left + xc + xextra + rep(c(0, cumsum(w0))[1L:ncol], 
##         each = n.legpercol, length.out = n.leg)
##     topspace <- 0.5 * ymax + (!is.null(title)) * ychartitle
##     yt <- top - topspace - cumsum((c(0, ychar)/2 + c(ychar, 0)/2)[1L:n.legpercol])
##     yt <- rep(yt, length.out = n.leg)
##     if (mfill) {
##         if (plot) {
##             if (!is.null(fill)) 
##                 fill <- rep_len(fill, n.leg)
##             rect2(left = xt, top = yt + ybox/2, dx = xbox, dy = ybox, 
##                 col = fill, density = density, angle = angle, 
##                 border = border)
##         }
##         xt <- xt + dx.fill
##     }
##     if (plot && (has.pch || do.lines)) 
##         col <- rep_len(col, n.leg)
##     if (missing(lwd) || is.null(lwd)) 
##         lwd <- par("lwd")
##     if (do.lines) {
##         if (missing(lty) || is.null(lty)) 
##             lty <- 1
##         lty <- rep_len(lty, n.leg)
##         lwd <- rep_len(lwd, n.leg)
##         ok.l <- !is.na(lty) & (is.character(lty) | lty > 0) & 
##             !is.na(lwd)
##         if (trace) 
##             catn("  segments2(", xt[ok.l] + x.off * xchar[ok.l], 
##                 ",", yt[ok.l], ", dx=", (seg.len * xchar)[ok.l], 
##                 ", dy=0, ...)")
##         if (plot) 
##             segments2(xt[ok.l] + x.off * xchar[ok.l], yt[ok.l], 
##                 dx = (seg.len * xchar)[ok.l], dy = 0, lty = lty[ok.l], 
##                 lwd = lwd[ok.l], col = col[ok.l])
##         xt <- xt + (seg.len + x.off) * xchar
##     }
##     if (has.pch) {
##         pch <- rep_len(pch, n.leg)
##         pt.bg <- rep_len(pt.bg, n.leg)
##         pt.cex <- rep_len(pt.cex, n.leg)
##         pt.lwd <- rep_len(pt.lwd, n.leg)
##         ok <- !is.na(pch)
##         if (!is.character(pch)) {
##             ok <- ok & (pch >= 0 | pch <= -32)
##         }
##         else {
##             ok <- ok & nzchar(pch)
##         }
##         x1 <- (if (merge && do.lines) 
##             xt - (seg.len/2) * xchar
##         else xt)[ok]
##         y1 <- yt[ok]
##         if (trace) 
##             catn("  points2(", x1, ",", y1, ", pch=", pch[ok], 
##                 ", ...)")
##         if (plot) 
##             points2(x1, y1, pch = pch[ok], col = col[ok], cex = pt.cex[ok], 
##                 bg = pt.bg[ok], lwd = pt.lwd[ok])
##     }
##     xt <- xt + x.intersp * xc
##     if (plot) {
##         if (!is.null(title)) 
##             text2(left + w * title.adj, top - ymaxtitle, labels = title, 
##                 adj = c(title.adj, 0), cex = title.cex, col = title.col, 
##                 font = title.font)
##         text2(xt, yt, labels = legend, adj = adj, cex = cex, 
##             col = text.col, font = text.font)
##     }
##     invisible(list(rect = list(w = w, h = h, left = left, top = top), 
##         text = list(x = xt, y = yt)))
## }
## <bytecode: 0x00000130a20058f8>
## <environment: namespace:graphics>

###Diagrama de caja

plot (x = factor(df_bank$education), y = df_bank$age, main ="Edad por nivel educativo")

plot (x = factor(df_bank$education), y = df_bank$age, main ="Edad por nivel educativo", 
      xlab = "Nivel educativo", ylab = "Edad", col = c("blue", "pink","yellow","grey" ))

boxplot(x = df_bank$age, col = "pink")

###Gráfico de mosaico

plot(x = factor (df_bank$marital), y = factor(df_bank$education))

###Función barplot

Tabla de contigencia

table(df_bank$education)
## 
##   primary secondary  tertiary   unknown 
##       678      2306      1350       187
tab_bank <- table(df_bank$loan, df_bank$education)
tab_bank
##      
##       primary secondary tertiary unknown
##   no      584      1890     1176     180
##   yes      94       416      174       7
barplot (tab_bank)

#Maregnes de la tabla

prop.table (tab_bank, margin = 1)
##      
##          primary  secondary   tertiary    unknown
##   no  0.15248042 0.49347258 0.30704961 0.04699739
##   yes 0.13603473 0.60202605 0.25180897 0.01013025
prop.table (tab_bank,margin = 2)
##      
##          primary  secondary   tertiary    unknown
##   no  0.86135693 0.81960104 0.87111111 0.96256684
##   yes 0.13864307 0.18039896 0.12888889 0.03743316
tab <- table(df_bank$housing, df_bank$job)

###Agregar Leyenda y colores

barplot(tab) 
legend(x="topright", legend=c("No", "Yes"), fill = c("Black","Grey"), title = "Housing")

prop.table(tab, margin = 2)
##      
##           admin. blue-collar entrepreneur  housemaid management    retired
##   no  0.36820084  0.26532770   0.44047619 0.65178571 0.48090815 0.78260870
##   yes 0.63179916  0.73467230   0.55952381 0.34821429 0.51909185 0.21739130
##      
##       self-employed   services    student technician unemployed    unknown
##   no     0.51912568 0.31654676 0.76190476 0.44791667 0.54687500 0.97368421
##   yes    0.48087432 0.68345324 0.23809524 0.55208333 0.45312500 0.02631579

#Proporciones

Prestamo personal de acuerdo a la educación

tab2 <- table(df_bank$loan, df_bank$education)
tab2
##      
##       primary secondary tertiary unknown
##   no      584      1890     1176     180
##   yes      94       416      174       7
barplot(tab2) 
legend(x="topright", legend=c("No", "Yes"), fill = c("Black","Grey"), title = "Loan")

prop.table(tab2, margin = 2)
##      
##          primary  secondary   tertiary    unknown
##   no  0.86135693 0.81960104 0.87111111 0.96256684
##   yes 0.13864307 0.18039896 0.12888889 0.03743316