class: center, middle .linea-superior[] .linea-inferior[] <img src="imagenes/logo_portada2.png" width="200" /> ## INE Educa: Clases abiertas de R ## Visualización con ggplot II ## Proyecto Ciencia de Datos --- background-image: url("imagenes/fondo2.PNG") background-size: contain; background-position: 100% 0% # Segundo dataset Trabajaremos con un dataset llamado gapminder Contiene datos de PIB y esperanza de vida por país y quinquenio ```r install.packages("gapminder") ``` ```r library(gapminder) life <- gapminder head(life, 10) ``` ``` ## # A tibble: 10 × 6 ## country continent year lifeExp pop gdpPercap ## <fct> <fct> <int> <dbl> <int> <dbl> ## 1 Afghanistan Asia 1952 28.8 8425333 779. ## 2 Afghanistan Asia 1957 30.3 9240934 821. ## 3 Afghanistan Asia 1962 32.0 10267083 853. ## 4 Afghanistan Asia 1967 34.0 11537966 836. ## 5 Afghanistan Asia 1972 36.1 13079460 740. ## 6 Afghanistan Asia 1977 38.4 14880372 786. ## 7 Afghanistan Asia 1982 39.9 12881816 978. ## 8 Afghanistan Asia 1987 40.8 13867957 852. ## 9 Afghanistan Asia 1992 41.7 16317921 649. ## 10 Afghanistan Asia 1997 41.8 22227415 635. ``` --- background-image: url("imagenes/fondo2.PNG") background-size: contain; background-position: 100% 0% # Segundo dataset ### Relación entre ingreso y esperanza de vida .panelset[ .panel[.panel-name[logaritmo] ```r library(tidyverse) life_gdp <- gapminder %>% filter(year == 2007) ggplot(life_gdp, aes(x = lifeExp, y = log(gdpPercap)) ) + geom_point() ``` <img src="visualizacion-2_files/figure-html/unnamed-chunk-3-1.png" style="display: block; margin: auto;" /> ] .panel[.panel-name[sin logaritmo] ```r library(tidyverse) life_gdp <- gapminder %>% filter(year == 2007) ggplot(life_gdp, aes(x = lifeExp, y = gdpPercap) ) + geom_point() ``` <img src="visualizacion-2_files/figure-html/unnamed-chunk-4-1.png" style="display: block; margin: auto;" /> ] ] --- background-image: url("imagenes/fondo2.PNG") background-size: contain; background-position: 100% 0% # Parámetros extra dentro de aes .panelset[ .panel[.panel-name[color] ```r *ggplot(life_gdp, aes(x = lifeExp, y = log(gdpPercap), color = continent) ) + geom_point() ``` <img src="visualizacion-2_files/figure-html/color-1.png" style="display: block; margin: auto;" /> ] .panel[.panel-name[size] ```r *ggplot(life_gdp, aes(x = lifeExp, y = log(gdpPercap), color = continent, size = pop) ) + geom_point() + * scale_size(range = c(1, 7)) ``` <img src="visualizacion-2_files/figure-html/size-1.png" style="display: block; margin: auto;" /> ] ] --- background-image: url("imagenes/fondo2.PNG") background-size: contain; background-position: 100% 0% # Problema: los puntos se superponen * Tenemos, al menos, dos soluciones: + Transparencia + Jitter .panelset[ .panel[.panel-name[transparencia] ```r ggplot(life_gdp, aes(x = lifeExp, y = log(gdpPercap), color = continent, size = pop) ) + geom_point(alpha = 0.6) + scale_size(range = c(1, 5)) ``` <img src="visualizacion-2_files/figure-html/unnamed-chunk-5-1.png" style="display: block; margin: auto;" /> ] .panel[.panel-name[jitter] ```r ggplot(life_gdp, aes(x = lifeExp, y = log(gdpPercap), color = continent, size = pop) ) + geom_point() + geom_jitter(width = 0.5) + scale_size() ``` <img src="visualizacion-2_files/figure-html/unnamed-chunk-6-1.png" style="display: block; margin: auto;" /> ] ] --- background-image: url("imagenes/fondo2.PNG") background-size: contain; background-position: 100% 0% # Paneles separados Con `facet_wrap` generamos un panel para cada categoría de la variable ```r ggplot(life_gdp, aes(x = lifeExp, y = log(gdpPercap), color = continent, size = pop) ) + geom_point() + scale_size(range = c(1, 5)) + * facet_wrap(~continent) ``` <img src="visualizacion-2_files/figure-html/unnamed-chunk-7-1.png" style="display: block; margin: auto;" /> --- background-image: url("imagenes/fondo2.PNG") background-size: contain; background-position: 100% 0% # Resumen * Figuras revisadas en la clase + `geom_bar` + `geom_point` + `geom_line` + `geom_vline` + `geom_hline` * Más figuras + `geom_text` + `geom_segment` + `geom_boxplot` + `geom_polygon` + `geom_density` -- **Memorizar las funciones es una mala estrategia para mejorar en `ggplot`** -- * Debemos preocuparnos de 2 cosas: + Entender la sintaxis de ggplot + Aprender a buscar rápidamente en internet lo que necesitamos --- class: center, middle .linea-superior[] .linea-inferior[] <img src="imagenes/logo_portada2.png" width="200" /> ## INE Educa: Clases abiertas de R ## Proyecto Ciencia de Datos ## Visualización con ggplot II