# Instalamos y/o cargamos paquetes
library("tidyverse")
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ 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("readxl")

Función read_csv()

ALUMNOS_2024 <- read_csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vTwVt48G81FHfD_K9jcVmhjN3otTkR0-5Y8mC23oaEOTmhiG4So0kjw6I0b2tkU5ilpehvlhqBjId9i/pub?output=csv")
## Rows: 24 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Marca temporal, Genero, Nivel.dificultad.R
## dbl (2): Edad, Asig.rend.aprob.2024
## 
## ℹ 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.
names(ALUMNOS_2024)
## [1] "Marca temporal"       "Genero"               "Edad"                
## [4] "Nivel.dificultad.R"   "Asig.rend.aprob.2024"

Función read_excel()

MANDARINAS <- read_excel("MANDARINAS_2024.xlsx")
glimpse(MANDARINAS)
## Rows: 419
## Columns: 8
## $ N             <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1…
## $ GRUPO         <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
## $ VARIEDAD      <chr> "Clementina", "Clementina", "Clementina", "Clementina", …
## $ N_DE_FRUTO    <dbl> 19, 9, 21, 8, 4, 30, 22, 23, 17, 27, 29, 14, 16, 13, 25,…
## $ PESO          <dbl> 101, 122, 127, 126, 37, 139, 140, 130, 138, 142, 121, 15…
## $ DIAM_ECUAT    <dbl> 64.2, 64.2, 64.7, 64.9, 65.9, 66.4, 67.1, 67.5, 68.2, 68…
## $ NIVEL_DE_DAÑO <dbl> 1, 0, 3, 3, 2, 2, 3, 1, 2, 2, 2, 1, 1, 2, 1, 1, 0, 1, 0,…
## $ COLOR         <dbl> 4, 5, 4, 1, 5, 4, 4, 3, 3, 4, 4, 1, 1, 3, 4, 1, 4, 1, 5,…

VISUALIZACIÓN DE DATOS

Cargamos la base de datos “iris”

Ordenamos la base de datos

IRIS <- iris %>% 
  rename(Longitud.Sepalo = Sepal.Length,
         Ancho.Sepalo = Sepal.Width,
         Longitud.Petalo = Petal.Length,
         Ancho.Petalo = Petal.Width,
         ESPECIE = Species)
## Rows: 150
## Columns: 5
## $ Longitud.Sepalo <dbl> 5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9, 5.4,…
## $ Ancho.Sepalo    <dbl> 3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4, 2.9, 3.1, 3.7,…
## $ Longitud.Petalo <dbl> 1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5, 1.4, 1.5, 1.5,…
## $ Ancho.Petalo    <dbl> 0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 0.2, 0.2, 0.1, 0.2,…
## $ ESPECIE         <fct> setosa, setosa, setosa, setosa, setosa, setosa, setosa…

Diagramas de dispersión

ggplot(IRIS, aes(Longitud.Petalo, Ancho.Petalo)) +
  geom_point()

ggplot(IRIS, aes(Longitud.Petalo, Ancho.Petalo)) +
  geom_point(shape = 25, fill = "yellow") # Para las formas 21 a 25, `fill =` controla el color de relleno de la forma.

También es posible modificar el tamaño de la forma seleccionada con “size =

ggplot(IRIS, aes(Longitud.Petalo, Ancho.Petalo)) +
  geom_point(size = 4)

ggplot(IRIS, aes(Longitud.Petalo, Ancho.Petalo)) +
  geom_point(color = "red")

Combinamos atributos

ggplot(IRIS, aes(Longitud.Petalo, Ancho.Petalo, shape = ESPECIE, color = ESPECIE)) +
  geom_point(size= 3)

Gráfico de líneas

PP_77_22 <- read_excel("PP_77_22.xlsx")
glimpse(PP_77_22)
## Rows: 46
## Columns: 2
## $ ANIO <dbl> 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987,…
## $ PP   <dbl> 666.4, 553.2, 675.7, 532.4, 528.7, 410.9, 569.2, 484.0, 610.2, 37…
ggplot(PP_77_22, aes(ANIO, PP)) +
  geom_line()

Renombramos variables

PP_77_22 <- PP_77_22 %>% 
  rename(AÑO = ANIO,
         PRECIPITACION = PP)

Visualizamos

PP_77_22
## # A tibble: 46 × 2
##      AÑO PRECIPITACION
##    <dbl>         <dbl>
##  1  1977          666.
##  2  1978          553.
##  3  1979          676.
##  4  1980          532.
##  5  1981          529.
##  6  1982          411.
##  7  1983          569.
##  8  1984          484 
##  9  1985          610.
## 10  1986          379.
## # ℹ 36 more rows

Combinamos ambos argumentos…

ggplot(PP_77_22, aes(AÑO, PRECIPITACION)) +
  geom_line(color = "blue", linetype = 3, lwd = 1 )

Histogramas

ggplot(PP_77_22, aes(PRECIPITACION)) +
  geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Cambiamos color de los contenedores.

ggplot(PP_77_22, aes(PRECIPITACION)) +
  geom_histogram(color = "yellow")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Modificamos color y relleno de los contenedores.

ggplot(PP_77_22, aes(PRECIPITACION)) +
  geom_histogram(color = "yellow", fill = "darkgreen")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

A continuación pueden ver los nombres de algunos colores con la función colors()

Ajuste de los contenedores

  1. Ajustando el número de bins a través del argumento bins. Usando este método especificamos el número de contenedores.
ggplot(PP_77_22, aes(PRECIPITACION)) +
  geom_histogram(bins = 8, color = "gray", fill = "lightblue") +
  theme_classic()

  1. Ajustando el ancho de los contenedores a través del argumento binwidth. Con el segundo método, en lugar de especificar el número de bins, especificamos el ancho de los bins. Por ejemplo, establezcamos el ancho de cada contenedor en cien milímetros.
ggplot(PP_77_22, aes(PRECIPITACION)) +
  geom_histogram(binwidth = 100, color = "red", fill = "blue") +
  theme_classic()

Polígono de frecuencias

ggplot(PP_77_22, aes(PRECIPITACION)) + 
  geom_freqpoly(binwidth = 100)

Combinamos un histograma con un polígono de frecuencias

ggplot(PP_77_22, aes(PRECIPITACION)) +
  geom_histogram(binwidth = 100, color = "yellow") +
  geom_freqpoly(binwidth = 100, color = "red", linewidth = 1.5)


Gráfico de cajas

MANDARINAS <- read_excel("MANDARINAS_2024.xlsx")
str(MANDARINAS)
## tibble [419 × 8] (S3: tbl_df/tbl/data.frame)
##  $ N            : num [1:419] 1 2 3 4 5 6 7 8 9 10 ...
##  $ GRUPO        : num [1:419] 1 1 1 1 1 1 1 1 1 1 ...
##  $ VARIEDAD     : chr [1:419] "Clementina" "Clementina" "Clementina" "Clementina" ...
##  $ N_DE_FRUTO   : num [1:419] 19 9 21 8 4 30 22 23 17 27 ...
##  $ PESO         : num [1:419] 101 122 127 126 37 139 140 130 138 142 ...
##  $ DIAM_ECUAT   : num [1:419] 64.2 64.2 64.7 64.9 65.9 66.4 67.1 67.5 68.2 68.2 ...
##  $ NIVEL_DE_DAÑO: num [1:419] 1 0 3 3 2 2 3 1 2 2 ...
##  $ COLOR        : num [1:419] 4 5 4 1 5 4 4 3 3 4 ...
ggplot(MANDARINAS, aes(DIAM_ECUAT, VARIEDAD)) +
  geom_boxplot()

ggplot(MANDARINAS, aes(DIAM_ECUAT, VARIEDAD, color = VARIEDAD)) + 
  geom_boxplot()

¿Y si agregamos la media muestral?

ggplot(MANDARINAS, aes(DIAM_ECUAT, VARIEDAD, color = VARIEDAD)) + 
  geom_boxplot() +
  stat_summary(fun = mean, color = "black") # función para agregar la media muestral y asignarle color
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_segment()`).