Gráficos con ggplot2: Primeras ideas

Módulo práctico de ITA
(Grupo M)

Pedro J. Pérez

sintaxis de ggplot2 [ 🌟 ]

ggplot(data = iris, aes(x = Sepal.Length, 
                        y = Petal.Length, 
                        color = Species)) + 
    geom_point()

Gráfico en slow-motion!

iris %>% 
iris %>% 
ggplot() + 
iris %>% 
ggplot() + 
   aes(x = Sepal.Length) +
iris %>% 
ggplot() + 
   aes(x = Sepal.Length) +
   aes(y = Petal.Length) +
iris %>% 
ggplot() + 
   aes(x = Sepal.Length) +
   aes(y = Petal.Length) +
   aes(color = Species)  + 
iris %>% 
ggplot() + 
   aes(x = Sepal.Length) +
   aes(y = Petal.Length) +
   aes(color = Species)  + 
  geom_point()
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1          5.1         3.5          1.4         0.2  setosa
#> 2          4.9         3.0          1.4         0.2  setosa
#> 3          4.7         3.2          1.3         0.2  setosa
#> 4          4.6         3.1          1.5         0.2  setosa
#> 5          5.0         3.6          1.4         0.2  setosa
#> 6          5.4         3.9          1.7         0.4  setosa

La forma habitual(!) de escribirlo es:

ggplot(data = iris, 
       aes(x = Sepal.Length, 
           y = Petal.Length, 
           color = Species)) + 
    geom_point()

IMAGINA qué hacen las siguientes instrucciones. Después las ejecutas.

#- Fíjate: sólo hay 2 variables en aes() --
ggplot(data = iris, aes(x = Sepal.Length, y = Petal.Length)) + 
  geom_point()

#- Fíjate: ahora usamos geom_line() --
ggplot(data = iris, aes(x = Sepal.Length, y = Petal.Length)) + 
  geom_line()

#- Fíjate: ahora usamos 2 geom_xx() --
ggplot(data = iris, aes(x = Sepal.Length, y = Petal.Length)) + 
  geom_point() + geom_smooth()

#- Fíjate: ahora hay argumentos dentro de geom_point(). Uno de ellos es `color` --
ggplot(data = iris, aes(x = Sepal.Length, y = Petal.Length)) + 
  geom_point(color = "red", size = 2, alpha = 0.2)
ggplot(data = iris, aes(x = Sepal.Length, y = Petal.Length)) + 
  geom_point()

ggplot(data = iris, aes(x = Sepal.Length, y = Petal.Length)) + 
  geom_line()

ggplot(data = iris, aes(x = Sepal.Length, y = Petal.Length)) + 
  geom_point() + geom_smooth()

ggplot(data = iris, aes(x = Sepal.Length, y = Petal.Length)) + 
  geom_point(color = "red", size = 2, alpha = 0.2)

vamos a profundizar un poco más en ggplot2

Es importante que veas que estas 3 expresiones hacen el mismo gráfico:

ggplot(data = iris, aes(x = Sepal.Length, y = Petal.Length)) + 
  geom_point()

ggplot(data = iris) + 
  geom_point(aes(x = Sepal.Length, y = Petal.Length))

ggplot() + 
  geom_point(data = iris, aes(x = Sepal.Length, y = Petal.Length))


  • Bueno, sí ¿Y qué más da? Pues importa y muuuuccho.

  • Esto nos va a dar mucha flexibilidad/control a nuestros gráficos. Lo veremos

¿qué hacen estas 3 expresiones?

ggplot(data = iris, aes(x = Sepal.Length, y = Petal.Length, color = Species)) + 
  geom_point() + geom_smooth()

ggplot(data = iris, aes(x = Sepal.Length, y = Petal.Length)) + 
  geom_point(aes(color = Species)) + geom_smooth()

ggplot(data = iris, aes(x = Sepal.Length, y = Petal.Length)) + 
  geom_point() + geom_smooth(aes(color = Species))

¿por qué no funciona esta expresión?


ggplot() +
  geom_point(data = iris, 
             aes(x = Sepal.Length, y = Petal.Length)) + 
   geom_line(aes(x = Sepal.Length, y = Petal.Length))
#> Error in `geom_line()`:
#> ! Problem while computing aesthetics.
#> ℹ Error occurred in the 2nd layer.
#> Caused by error:
#> ! objeto 'Sepal.Length' no encontrado
  • geom_line() no sabe qué datos usar: iris solo se ha indicado dentro de geom_point(), no en ggplot()

Mapear vs fijar: ¿dentro o fuera de aes()? [ 🌟 ]

Mapear (dentro de aes()): el color depende de una variable → ggplot2 crea una leyenda

ggplot(data = iris, 
       aes(x = Sepal.Length, y = Petal.Length)) + 
  geom_point(aes(color = Species))

Fijar (fuera de aes()): el color es un valor fijo para todos los puntos → no hay leyenda

ggplot(data = iris, 
       aes(x = Sepal.Length, y = Petal.Length)) + 
  geom_point(color = "purple")

otra estética: size dentro y fuera de aes()

Mapear (dentro de aes()): el tamaño depende de una variable (aquí, la especie) → ggplot2 crea una leyenda

ggplot(data = iris, 
       aes(x = Sepal.Length, y = Petal.Length)) + 
  geom_point(aes(size = Species, color = Species))

Fijar (fuera de aes()): el tamaño es un valor fijo para todos los puntos → no sale en la leyenda (solo el color)

ggplot(data = iris, 
       aes(x = Sepal.Length, y = Petal.Length)) + 
  geom_point(aes(color = Species), size = 3)

¿veremos alguna vez otros gráficos?

  • SÍ!!!! en las siguientes slides haremos gráficos de líneas, de puntos (X-Y) y de barras. Y ya no con iris, sino con los datos de gapminder

  • Si no puedes esperarte ve a la R Graph Gallery o a la galería de extensiones de ggplot2


para hacer otros gráficos tendremos que usar otros geom_xx()

Por ejemplo:

  • geom_line()
  • geom_bar()
  • geom_col()
  • geom_density()
  • geom_path()
  • geom_map()
  • geom_raster()
  • geom_rug()
  • … y muchos más