ImportaciĂ³n del datset

library(palmerpenguins)
library(ggthemes)
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.0     ✔ 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

Ejercicio 1

numRows <- nrow(penguins)
numCols <- ncol(penguins)

cat("NĂºmero de filas:", numRows, ", ", "NĂºmero de filas:", numCols)
## NĂºmero de filas: 344 ,  NĂºmero de filas: 8

Ejercicio 2

Consultado la ayuda del data frame con ?penguins podemos ver:
bill_depth_mm
    a number denoting bill depth (millimeters)

Ejercicio 3

ggplot(
    data = penguins,
    mapping = aes(x = bill_depth_mm, y = bill_length_mm, color = species)
) + geom_point() +
  geom_smooth(method = "lm")
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 2 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).

Ejercicio 4

ggplot(
    data = penguins,
    mapping = aes(x = species, y = bill_depth_mm, color = species)
) + geom_point()
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).

La mejor opcion es un diagrama de cajas

ggplot(
    data = penguins,
    mapping = aes(x = species, y = bill_depth_mm, color = species)
) + geom_boxplot()
## Warning: Removed 2 rows containing non-finite outside the scale range
## (`stat_boxplot()`).

Ejercicio 5

ggplot(data = penguins) + 
geom_point()

El error se debe a que no se ha proporciondo las variblaes que se utilizarĂ¡n en el eje x y en el y

Ejercicio 6

Ese argumento controla como son tratados los valores NA. Cuando na.rm = TRUE indica que cualquier fila del conjunto de datos que contenga valores NA sera eliminada antes de representar el grafico.

ggplot(
    data = penguins,
) + geom_point(mapping = aes(x = body_mass_g, y = flipper_length_mm), na.rm = TRUE)

Ejercicio 7

ggplot(
    data = penguins,
) + geom_point(mapping = aes(x = body_mass_g, y = flipper_length_mm), na.rm = TRUE) +
    labs(caption = "Data come from the palmerpenguins package.")

Ejercicio 8

ggplot(data = penguins, aes(x = flipper_length_mm, y = body_mass_g, color = bill_depth_mm)) + 
  geom_point(na.rm = TRUE) +
  geom_smooth(method = 'loess')
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 2 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: The following aesthetics were dropped during statistical transformation:
## colour.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
##   the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
##   variable into a factor?

Ejercicio 9

ggplot(
  data = penguins,
  mapping = aes(x = flipper_length_mm, y = body_mass_g, color = island)
) +
  geom_point() +
  geom_smooth(se = FALSE)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 2 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).

Ejercicio 10

Apesar de las diferencias en la sintaxis, el resultado es el mismo ya que los datos y la estetica que se especifica es la misma en ambos graficos.

ggplot(
  data = penguins,
  mapping = aes(x = flipper_length_mm, y = body_mass_g)
) +
  geom_point() +
  geom_smooth()
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 2 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).

ggplot() +
  geom_point(
    data = penguins,
    mapping = aes(x = flipper_length_mm, y = body_mass_g)
  ) +
  geom_smooth(
    data = penguins,
    mapping = aes(x = flipper_length_mm, y = body_mass_g)
  )
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 2 rows containing non-finite outside the scale range (`stat_smooth()`).
## Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).