class: center, middle, inverse, title-slide .title[ # Visualización de datos con ggplot2 ] .subtitle[ ## Visualización de datos con R ] .author[ ### Christian Chiroque Ruiz ] --- layout: true class: animated, fadeIn --- class: inverse # ¿Dónde estamos en el curso? - Introducción a la Estadística - Tidyverse: Manipulación de datos con {dplyr} - **Tidyverse: Visualización de datos con {ggplot2}** --- # ggplot2 Es un sistema organizado de visualización de datos. Forma parte del conjunto de librerías llamado *tidyverse*. ``` install.packages("tidyverse") # Sólo si lo necesitas library(tidyverse) ``` La primera versión del paquete fue liberada por Hadley Wickham el 10 de junio de 2007, desde entonces el paquete se ha enriquecido con diferentes elementos. Ggplot2 se ha convertido en el paquete de creación de visualizaciones más popular en el universo R por permitir de manera sencilla obtener gráficos de alta calidad. Incluso [otros programas de Data Science carecen de una herramienta como ggplot2](https://towardsdatascience.com/how-to-use-ggplot2-in-python-74ab8adec129). --- # Gramática de ggplot2 .pull-left[ - La gramática del ggplot2 se basa en el libro The Grammar of Graphics - A diferencia de los gráficos con el paquete base donde creamos un gráfico a base de pasos sucesivos, ggplot2 se basa en una gramática de gráficos, añadiendo elementos a un graphical device , donde distintos componentes independientes se pueden combinar de muchas maneras diferentes. ] .pull-right[  ] --- class: center # Son 7 capas principales  --- # 1ra Capa: Data Es la materia prima sobre la cual se van a posicionar el resto de capas y los datos que se desean representar. El ggplot2 sólo acepta un tipo de datos: data.frames/ tibbles. No permite *vectores*. Vamos a utilizar la data del paquete {gapminder}. --- # 1ra Capa: Data ```r #install.packages("gapminder") library(gapminder) data<-gapminder::gapminder head(data, 3) ``` ``` # A tibble: 3 × 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. ``` --- # 2da Capa: Aesthetics ("Estéticas") ``` aes() ``` - Indican las variables que se van a graficar, tanto en el **eje horizontal** (x) como en el **eje vertical** (y). - Ggplo2 no está pensado para gráficos tridimensionales, pero ciertamente podemos incluir una tercera variable, por ejemplo, indicando el **color** si deseamos identificar grupos, o indicando el tamaño (de los puntos en un scatterplot) para agregar una nueva variable cuantitativa. --- # 3ra Capa: Geometric Objects (Objetos geométricos) - Funciones: geom_line(), geom_boxplot(), etc. - Indica qué tipo de gráfico (geometría) se va a construir: gráfico de barras, columnas, puntos, histográmas, boxplots, líneas, densidad, entre otros. - En el paquete {ggplot2} existen 30 geometrías disponibles. Puedes ver el detalle de estos en la [documentación del paquete](https://cran.r-project.org/web/packages/ggplot2/ggplot2.pdf). - Cada geometría tiene su propia función y, como ya hemos visto, cada una puede tener distintos argumentos. --- class: inverse, middle, center # Estas 3 capas son el mínimo necesario para hacer un gráfico: Data + Aesthetics + Geometries --- count: false Recuerda los pasos básicos .panel1-chunk_1-auto[ ```r data #<< ``` ] .panel2-chunk_1-auto[ ``` # A tibble: 1,704 × 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. # … with 1,694 more rows ``` ] --- count: false Recuerda los pasos básicos .panel1-chunk_1-auto[ ```r data %>% ggplot() #<< ``` ] .panel2-chunk_1-auto[ <!-- --> ] --- count: false Recuerda los pasos básicos .panel1-chunk_1-auto[ ```r data %>% ggplot() + aes(x = lifeExp) #<< ``` ] .panel2-chunk_1-auto[ <!-- --> ] --- count: false Recuerda los pasos básicos .panel1-chunk_1-auto[ ```r data %>% ggplot() + aes(x = lifeExp) + geom_histogram() #<< ``` ] .panel2-chunk_1-auto[ <!-- --> ] <style> .panel1-chunk_1-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-chunk_1-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-chunk_1-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false Probemos con argumentos y otras geometrías univariadas .panel1-primer_chunk-rotate[ ```r data %>% ggplot() + aes(x = lifeExp) + geom_histogram(binwidth=5,color="white") #<< ``` ] .panel2-primer_chunk-rotate[ <!-- --> ] --- count: false Probemos con argumentos y otras geometrías univariadas .panel1-primer_chunk-rotate[ ```r data %>% ggplot() + aes(x = lifeExp) + geom_boxplot() #<< ``` ] .panel2-primer_chunk-rotate[ <!-- --> ] --- count: false Probemos con argumentos y otras geometrías univariadas .panel1-primer_chunk-rotate[ ```r data %>% ggplot() + aes(x = lifeExp) + geom_boxplot(notch=TRUE,color="blue") #<< ``` ] .panel2-primer_chunk-rotate[ <!-- --> ] <style> .panel1-primer_chunk-rotate { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-primer_chunk-rotate { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-primer_chunk-rotate { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false Ahora probemos con gráficos bivariados .panel1-chunk_3-auto[ ```r data #<< ``` ] .panel2-chunk_3-auto[ ``` # A tibble: 1,704 × 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. # … with 1,694 more rows ``` ] --- count: false Ahora probemos con gráficos bivariados .panel1-chunk_3-auto[ ```r data %>% ggplot() #<< ``` ] .panel2-chunk_3-auto[ <!-- --> ] --- count: false Ahora probemos con gráficos bivariados .panel1-chunk_3-auto[ ```r data %>% ggplot() + aes(x = lifeExp) #<< ``` ] .panel2-chunk_3-auto[ <!-- --> ] --- count: false Ahora probemos con gráficos bivariados .panel1-chunk_3-auto[ ```r data %>% ggplot() + aes(x = lifeExp) + aes(y = gdpPercap) #<< ``` ] .panel2-chunk_3-auto[ <!-- --> ] --- count: false Ahora probemos con gráficos bivariados .panel1-chunk_3-auto[ ```r data %>% ggplot() + aes(x = lifeExp) + aes(y = gdpPercap) + geom_point() #<< ``` ] .panel2-chunk_3-auto[ <!-- --> ] --- count: false Ahora probemos con gráficos bivariados .panel1-chunk_3-auto[ ```r data %>% ggplot() + aes(x = lifeExp) + aes(y = gdpPercap) + geom_point() + aes(color=continent) #<< ``` ] .panel2-chunk_3-auto[ <!-- --> ] --- count: false Ahora probemos con gráficos bivariados .panel1-chunk_3-auto[ ```r data %>% ggplot() + aes(x = lifeExp) + aes(y = gdpPercap) + geom_point() + aes(color=continent) + geom_smooth() #<< ``` ] .panel2-chunk_3-auto[ <!-- --> ] <style> .panel1-chunk_3-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-chunk_3-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-chunk_3-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false Mezclemos otras funciones del tidyverse y más de un gráfico .panel1-chunk_4-auto[ ```r data #<< ``` ] .panel2-chunk_4-auto[ ``` # A tibble: 1,704 × 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. # … with 1,694 more rows ``` ] --- count: false Mezclemos otras funciones del tidyverse y más de un gráfico .panel1-chunk_4-auto[ ```r data %>% filter(country=="Peru", year>1960) #<< ``` ] .panel2-chunk_4-auto[ ``` # A tibble: 10 × 6 country continent year lifeExp pop gdpPercap <fct> <fct> <int> <dbl> <int> <dbl> 1 Peru Americas 1962 49.1 10516500 4957. 2 Peru Americas 1967 51.4 12132200 5788. 3 Peru Americas 1972 55.4 13954700 5938. 4 Peru Americas 1977 58.4 15990099 6281. 5 Peru Americas 1982 61.4 18125129 6435. 6 Peru Americas 1987 64.1 20195924 6361. 7 Peru Americas 1992 66.5 22430449 4446. 8 Peru Americas 1997 68.4 24748122 5838. 9 Peru Americas 2002 69.9 26769436 5909. 10 Peru Americas 2007 71.4 28674757 7409. ``` ] --- count: false Mezclemos otras funciones del tidyverse y más de un gráfico .panel1-chunk_4-auto[ ```r data %>% filter(country=="Peru", year>1960) %>% ggplot() #<< ``` ] .panel2-chunk_4-auto[ <!-- --> ] --- count: false Mezclemos otras funciones del tidyverse y más de un gráfico .panel1-chunk_4-auto[ ```r data %>% filter(country=="Peru", year>1960) %>% ggplot() + aes(x = year) #<< ``` ] .panel2-chunk_4-auto[ <!-- --> ] --- count: false Mezclemos otras funciones del tidyverse y más de un gráfico .panel1-chunk_4-auto[ ```r data %>% filter(country=="Peru", year>1960) %>% ggplot() + aes(x = year) + aes(y = gdpPercap) #<< ``` ] .panel2-chunk_4-auto[ <!-- --> ] --- count: false Mezclemos otras funciones del tidyverse y más de un gráfico .panel1-chunk_4-auto[ ```r data %>% filter(country=="Peru", year>1960) %>% ggplot() + aes(x = year) + aes(y = gdpPercap) + geom_line() #<< ``` ] .panel2-chunk_4-auto[ <!-- --> ] --- count: false Mezclemos otras funciones del tidyverse y más de un gráfico .panel1-chunk_4-auto[ ```r data %>% filter(country=="Peru", year>1960) %>% ggplot() + aes(x = year) + aes(y = gdpPercap) + geom_line()+ geom_point() #<< ``` ] .panel2-chunk_4-auto[ <!-- --> ] <style> .panel1-chunk_4-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-chunk_4-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-chunk_4-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false Y el gráfico de barras? .panel1-chunk_7-auto[ ```r data #<< ``` ] .panel2-chunk_7-auto[ ``` # A tibble: 1,704 × 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. # … with 1,694 more rows ``` ] --- count: false Y el gráfico de barras? .panel1-chunk_7-auto[ ```r data %>% select(country,continent) #<< ``` ] .panel2-chunk_7-auto[ ``` # A tibble: 1,704 × 2 country continent <fct> <fct> 1 Afghanistan Asia 2 Afghanistan Asia 3 Afghanistan Asia 4 Afghanistan Asia 5 Afghanistan Asia 6 Afghanistan Asia 7 Afghanistan Asia 8 Afghanistan Asia 9 Afghanistan Asia 10 Afghanistan Asia # … with 1,694 more rows ``` ] --- count: false Y el gráfico de barras? .panel1-chunk_7-auto[ ```r data %>% select(country,continent) %>% group_by(continent) #<< ``` ] .panel2-chunk_7-auto[ ``` # A tibble: 1,704 × 2 # Groups: continent [5] country continent <fct> <fct> 1 Afghanistan Asia 2 Afghanistan Asia 3 Afghanistan Asia 4 Afghanistan Asia 5 Afghanistan Asia 6 Afghanistan Asia 7 Afghanistan Asia 8 Afghanistan Asia 9 Afghanistan Asia 10 Afghanistan Asia # … with 1,694 more rows ``` ] --- count: false Y el gráfico de barras? .panel1-chunk_7-auto[ ```r data %>% select(country,continent) %>% group_by(continent) %>% filter(!duplicated(country)) #<< ``` ] .panel2-chunk_7-auto[ ``` # A tibble: 142 × 2 # Groups: continent [5] country continent <fct> <fct> 1 Afghanistan Asia 2 Albania Europe 3 Algeria Africa 4 Angola Africa 5 Argentina Americas 6 Australia Oceania 7 Austria Europe 8 Bahrain Asia 9 Bangladesh Asia 10 Belgium Europe # … with 132 more rows ``` ] --- count: false Y el gráfico de barras? .panel1-chunk_7-auto[ ```r data %>% select(country,continent) %>% group_by(continent) %>% filter(!duplicated(country)) %>% ungroup() #<< ``` ] .panel2-chunk_7-auto[ ``` # A tibble: 142 × 2 country continent <fct> <fct> 1 Afghanistan Asia 2 Albania Europe 3 Algeria Africa 4 Angola Africa 5 Argentina Americas 6 Australia Oceania 7 Austria Europe 8 Bahrain Asia 9 Bangladesh Asia 10 Belgium Europe # … with 132 more rows ``` ] --- count: false Y el gráfico de barras? .panel1-chunk_7-auto[ ```r data %>% select(country,continent) %>% group_by(continent) %>% filter(!duplicated(country)) %>% ungroup() %>% ggplot() #<< ``` ] .panel2-chunk_7-auto[ <!-- --> ] --- count: false Y el gráfico de barras? .panel1-chunk_7-auto[ ```r data %>% select(country,continent) %>% group_by(continent) %>% filter(!duplicated(country)) %>% ungroup() %>% ggplot()+ aes(x=continent) #<< ``` ] .panel2-chunk_7-auto[ <!-- --> ] --- count: false Y el gráfico de barras? .panel1-chunk_7-auto[ ```r data %>% select(country,continent) %>% group_by(continent) %>% filter(!duplicated(country)) %>% ungroup() %>% ggplot()+ aes(x=continent) + geom_bar() #<< ``` ] .panel2-chunk_7-auto[ <!-- --> ] <style> .panel1-chunk_7-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-chunk_7-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-chunk_7-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false Y el gráfico de barras apiladas? .panel1-chunk_8-auto[ ```r data #<< ``` ] .panel2-chunk_8-auto[ ``` # A tibble: 1,704 × 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. # … with 1,694 more rows ``` ] --- count: false Y el gráfico de barras apiladas? .panel1-chunk_8-auto[ ```r data %>% select(-4, -5) #<< ``` ] .panel2-chunk_8-auto[ ``` # A tibble: 1,704 × 4 country continent year gdpPercap <fct> <fct> <int> <dbl> 1 Afghanistan Asia 1952 779. 2 Afghanistan Asia 1957 821. 3 Afghanistan Asia 1962 853. 4 Afghanistan Asia 1967 836. 5 Afghanistan Asia 1972 740. 6 Afghanistan Asia 1977 786. 7 Afghanistan Asia 1982 978. 8 Afghanistan Asia 1987 852. 9 Afghanistan Asia 1992 649. 10 Afghanistan Asia 1997 635. # … with 1,694 more rows ``` ] --- count: false Y el gráfico de barras apiladas? .panel1-chunk_8-auto[ ```r data %>% select(-4, -5) %>% mutate(gdp_cat=case_when( #<< gdpPercap<4000~ "Bajo GDPpc", #<< TRUE~ "Alto GDPpc")) #<< ``` ] .panel2-chunk_8-auto[ ``` # A tibble: 1,704 × 5 country continent year gdpPercap gdp_cat <fct> <fct> <int> <dbl> <chr> 1 Afghanistan Asia 1952 779. Bajo GDPpc 2 Afghanistan Asia 1957 821. Bajo GDPpc 3 Afghanistan Asia 1962 853. Bajo GDPpc 4 Afghanistan Asia 1967 836. Bajo GDPpc 5 Afghanistan Asia 1972 740. Bajo GDPpc 6 Afghanistan Asia 1977 786. Bajo GDPpc 7 Afghanistan Asia 1982 978. Bajo GDPpc 8 Afghanistan Asia 1987 852. Bajo GDPpc 9 Afghanistan Asia 1992 649. Bajo GDPpc 10 Afghanistan Asia 1997 635. Bajo GDPpc # … with 1,694 more rows ``` ] --- count: false Y el gráfico de barras apiladas? .panel1-chunk_8-auto[ ```r data %>% select(-4, -5) %>% mutate(gdp_cat=case_when( gdpPercap<4000~ "Bajo GDPpc", TRUE~ "Alto GDPpc")) %>% filter(year==2007) #<< ``` ] .panel2-chunk_8-auto[ ``` # A tibble: 142 × 5 country continent year gdpPercap gdp_cat <fct> <fct> <int> <dbl> <chr> 1 Afghanistan Asia 2007 975. Bajo GDPpc 2 Albania Europe 2007 5937. Alto GDPpc 3 Algeria Africa 2007 6223. Alto GDPpc 4 Angola Africa 2007 4797. Alto GDPpc 5 Argentina Americas 2007 12779. Alto GDPpc 6 Australia Oceania 2007 34435. Alto GDPpc 7 Austria Europe 2007 36126. Alto GDPpc 8 Bahrain Asia 2007 29796. Alto GDPpc 9 Bangladesh Asia 2007 1391. Bajo GDPpc 10 Belgium Europe 2007 33693. Alto GDPpc # … with 132 more rows ``` ] --- count: false Y el gráfico de barras apiladas? .panel1-chunk_8-auto[ ```r data %>% select(-4, -5) %>% mutate(gdp_cat=case_when( gdpPercap<4000~ "Bajo GDPpc", TRUE~ "Alto GDPpc")) %>% filter(year==2007) %>% ggplot() #<< ``` ] .panel2-chunk_8-auto[ <!-- --> ] --- count: false Y el gráfico de barras apiladas? .panel1-chunk_8-auto[ ```r data %>% select(-4, -5) %>% mutate(gdp_cat=case_when( gdpPercap<4000~ "Bajo GDPpc", TRUE~ "Alto GDPpc")) %>% filter(year==2007) %>% ggplot()+ aes(x=continent, fill=gdp_cat) #<< ``` ] .panel2-chunk_8-auto[ <!-- --> ] --- count: false Y el gráfico de barras apiladas? .panel1-chunk_8-auto[ ```r data %>% select(-4, -5) %>% mutate(gdp_cat=case_when( gdpPercap<4000~ "Bajo GDPpc", TRUE~ "Alto GDPpc")) %>% filter(year==2007) %>% ggplot()+ aes(x=continent, fill=gdp_cat) + geom_bar() #<< ``` ] .panel2-chunk_8-auto[ <!-- --> ] <style> .panel1-chunk_8-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-chunk_8-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-chunk_8-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false Puedes colocar grupos en uno de los ejes para gráficos univariados .panel1-chunk_9-user[ ```r data %>% #<< select(-4, -5) %>% #<< mutate(gdp_cat=case_when( #<< gdpPercap<4000~ "Bajo GDPpc", #<< TRUE~ "Alto GDPpc")) %>% #<< filter(year==2007) #<< ``` ] .panel2-chunk_9-user[ ``` # A tibble: 142 × 5 country continent year gdpPercap gdp_cat <fct> <fct> <int> <dbl> <chr> 1 Afghanistan Asia 2007 975. Bajo GDPpc 2 Albania Europe 2007 5937. Alto GDPpc 3 Algeria Africa 2007 6223. Alto GDPpc 4 Angola Africa 2007 4797. Alto GDPpc 5 Argentina Americas 2007 12779. Alto GDPpc 6 Australia Oceania 2007 34435. Alto GDPpc 7 Austria Europe 2007 36126. Alto GDPpc 8 Bahrain Asia 2007 29796. Alto GDPpc 9 Bangladesh Asia 2007 1391. Bajo GDPpc 10 Belgium Europe 2007 33693. Alto GDPpc # … with 132 more rows ``` ] --- count: false Puedes colocar grupos en uno de los ejes para gráficos univariados .panel1-chunk_9-user[ ```r data %>% select(-4, -5) %>% mutate(gdp_cat=case_when( gdpPercap<4000~ "Bajo GDPpc", TRUE~ "Alto GDPpc")) %>% filter(year==2007) %>% ggplot() #<< ``` ] .panel2-chunk_9-user[ <!-- --> ] --- count: false Puedes colocar grupos en uno de los ejes para gráficos univariados .panel1-chunk_9-user[ ```r data %>% select(-4, -5) %>% mutate(gdp_cat=case_when( gdpPercap<4000~ "Bajo GDPpc", TRUE~ "Alto GDPpc")) %>% filter(year==2007) %>% ggplot()+ aes(x=continent)+ #<< aes(y=gdpPercap) + #<< aes(color=continent) #<< ``` ] .panel2-chunk_9-user[ <!-- --> ] --- count: false Puedes colocar grupos en uno de los ejes para gráficos univariados .panel1-chunk_9-user[ ```r data %>% select(-4, -5) %>% mutate(gdp_cat=case_when( gdpPercap<4000~ "Bajo GDPpc", TRUE~ "Alto GDPpc")) %>% filter(year==2007) %>% ggplot()+ aes(x=continent)+ aes(y=gdpPercap) + aes(color=continent) + geom_boxplot() #<< ``` ] .panel2-chunk_9-user[ <!-- --> ] <style> .panel1-chunk_9-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-chunk_9-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-chunk_9-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false Separando grupos por color .panel1-chunk_12-user[ ```r data %>% #<< filter(continent=="Oceania") %>% #<< ggplot()+ #<< aes(x=year) + #<< aes(y=lifeExp) #<< ``` ] .panel2-chunk_12-user[ <!-- --> ] --- count: false Separando grupos por color .panel1-chunk_12-user[ ```r data %>% filter(continent=="Oceania") %>% ggplot()+ aes(x=year) + aes(y=lifeExp) + aes(color=country) #<< ``` ] .panel2-chunk_12-user[ <!-- --> ] --- count: false Separando grupos por color .panel1-chunk_12-user[ ```r data %>% filter(continent=="Oceania") %>% ggplot()+ aes(x=year) + aes(y=lifeExp) + aes(color=country) + geom_line() + #<< geom_point() #<< ``` ] .panel2-chunk_12-user[ <!-- --> ] <style> .panel1-chunk_12-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-chunk_12-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-chunk_12-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false Separando grupos sin color .panel1-chunk_13-user[ ```r data %>% #<< filter(continent=="Oceania") %>% #<< ggplot()+ #<< aes(x=year) + #<< aes(y=lifeExp) #<< ``` ] .panel2-chunk_13-user[ <!-- --> ] --- count: false Separando grupos sin color .panel1-chunk_13-user[ ```r data %>% filter(continent=="Oceania") %>% ggplot()+ aes(x=year) + aes(y=lifeExp) + aes(group=country) #<< ``` ] .panel2-chunk_13-user[ <!-- --> ] --- count: false Separando grupos sin color .panel1-chunk_13-user[ ```r data %>% filter(continent=="Oceania") %>% ggplot()+ aes(x=year) + aes(y=lifeExp) + aes(group=country) + geom_line() + #<< geom_point() #<< ``` ] .panel2-chunk_13-user[ <!-- --> ] <style> .panel1-chunk_13-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-chunk_13-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-chunk_13-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: inverse, center, middle # Ahora vayamos con las demás capas! --- # 4ta Capa: Facets (Facetas) .pull-left[ - Permite descomponer un gráfico en subgráficos, también llamadas cuadrículas o facetas, según una variable **cualitativa**. - Sirve para comparar grupos, separándolos y así facilitando la identificación de diferencias significativas entre estos. ] .pull-right[  ] --- count: false Utilizamos facet_wrap para separar la figura por las categorías de UNA variable cualitativa .panel1-chunk_5-user[ ```r data %>% #<< ggplot() + #<< aes(x = lifeExp) + #<< aes(y = gdpPercap) + #<< geom_point() + #<< aes(color=continent) #<< ``` ] .panel2-chunk_5-user[ <!-- --> ] --- count: false Utilizamos facet_wrap para separar la figura por las categorías de UNA variable cualitativa .panel1-chunk_5-user[ ```r data %>% ggplot() + aes(x = lifeExp) + aes(y = gdpPercap) + geom_point() + aes(color=continent) + facet_wrap(~continent) #<< ``` ] .panel2-chunk_5-user[ <!-- --> ] <style> .panel1-chunk_5-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-chunk_5-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-chunk_5-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false Usamos facet_grid() para cruzar las categorías de dos variables cualitativas .panel1-chunk_6-user[ ```r data %>% #<< filter(continent %in% c("Africa","Americas", "Europe")) %>% #<< filter(year==2007) %>% #<< mutate(gdp_cat=case_when(gdpPercap<4000~ "Bajo GDPpc", #<< TRUE~ "Alto GDPpc")) %>% #<< ggplot() + #<< aes(x = lifeExp) + #<< aes(y = gdpPercap) + #<< geom_point() + #<< aes(color=continent) #<< ``` ] .panel2-chunk_6-user[ <!-- --> ] --- count: false Usamos facet_grid() para cruzar las categorías de dos variables cualitativas .panel1-chunk_6-user[ ```r data %>% filter(continent %in% c("Africa","Americas", "Europe")) %>% filter(year==2007) %>% mutate(gdp_cat=case_when(gdpPercap<4000~ "Bajo GDPpc", TRUE~ "Alto GDPpc")) %>% ggplot() + aes(x = lifeExp) + aes(y = gdpPercap) + geom_point() + aes(color=continent) + facet_grid(cols = vars(continent), rows = vars(gdp_cat)) #<< ``` ] .panel2-chunk_6-user[ <!-- --> ] <style> .panel1-chunk_6-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-chunk_6-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-chunk_6-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # 5ta Capa: Statistical Transformations (Tranformaciones Estadísticas) - Permite adicionar indicadores o estadísticos específicos calculados a partir de los datos de insumo. - Por ejemplo, se puede colocar la media de una variable numérica. --- count: false Podemos graficar haciendo uso de stat_summary .panel1-chunk_11-user[ ```r data %>% #<< mutate(gdp_cat=case_when( #<< gdpPercap<4000~ "Bajo GDPpc", #<< TRUE~ "Alto GDPpc")) %>% #<< filter(year==2007) %>% #<< ggplot() #<< ``` ] .panel2-chunk_11-user[ <!-- --> ] --- count: false Podemos graficar haciendo uso de stat_summary .panel1-chunk_11-user[ ```r data %>% mutate(gdp_cat=case_when( gdpPercap<4000~ "Bajo GDPpc", TRUE~ "Alto GDPpc")) %>% filter(year==2007) %>% ggplot()+ aes(x=continent, group=gdp_cat, color=gdp_cat) #<< ``` ] .panel2-chunk_11-user[ <!-- --> ] --- count: false Podemos graficar haciendo uso de stat_summary .panel1-chunk_11-user[ ```r data %>% mutate(gdp_cat=case_when( gdpPercap<4000~ "Bajo GDPpc", TRUE~ "Alto GDPpc")) %>% filter(year==2007) %>% ggplot()+ aes(x=continent, group=gdp_cat, color=gdp_cat)+ stat_summary(aes(y=gdpPercap), #<< fun ="mean", #<< geom="point") #<< ``` ] .panel2-chunk_11-user[ <!-- --> ] --- count: false Podemos graficar haciendo uso de stat_summary .panel1-chunk_11-user[ ```r data %>% mutate(gdp_cat=case_when( gdpPercap<4000~ "Bajo GDPpc", TRUE~ "Alto GDPpc")) %>% filter(year==2007) %>% ggplot()+ aes(x=continent, group=gdp_cat, color=gdp_cat)+ stat_summary(aes(y=gdpPercap), fun ="mean", geom="point") + stat_summary(aes(y=gdpPercap), #<< fun ="mean", #<< geom="line") #<< ``` ] .panel2-chunk_11-user[ <!-- --> ] <style> .panel1-chunk_11-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-chunk_11-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-chunk_11-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false Podemos agregarlo encima de otro gráfico .panel1-chunk_10-user[ ```r data %>% #<< select(-4, -5) %>% #<< mutate(gdp_cat=case_when( #<< gdpPercap<4000~ "Bajo GDPpc", #<< TRUE~ "Alto GDPpc")) %>% #<< filter(year==2007) %>% #<< ggplot()+ #<< aes(x=continent)+ aes(y=gdpPercap) + #<< aes(color=continent) + #<< geom_boxplot() #<< ``` ] .panel2-chunk_10-user[ <!-- --> ] --- count: false Podemos agregarlo encima de otro gráfico .panel1-chunk_10-user[ ```r data %>% select(-4, -5) %>% mutate(gdp_cat=case_when( gdpPercap<4000~ "Bajo GDPpc", TRUE~ "Alto GDPpc")) %>% filter(year==2007) %>% ggplot()+ aes(x=continent)+ aes(y=gdpPercap) + aes(color=continent) + geom_boxplot()+ stat_summary(fun ="mean", #<< colour="red", #<< size = 5, #<< geom="point") #<< ``` ] .panel2-chunk_10-user[ <!-- --> ] --- count: false Podemos agregarlo encima de otro gráfico .panel1-chunk_10-user[ ```r data %>% select(-4, -5) %>% mutate(gdp_cat=case_when( gdpPercap<4000~ "Bajo GDPpc", TRUE~ "Alto GDPpc")) %>% filter(year==2007) %>% ggplot()+ aes(x=continent)+ aes(y=gdpPercap) + aes(color=continent) + geom_boxplot()+ stat_summary(fun ="mean", colour="red", size = 5, geom="point") + stat_summary(fun ="median", #<< colour="blue", #<< size = 5, #<< geom="point") #<< ``` ] .panel2-chunk_10-user[ <!-- --> ] <style> .panel1-chunk_10-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-chunk_10-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-chunk_10-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # 6ta Capa: Coordinates (Coordinadas) - Sirve para especificar cómo será presentada la información de las variables en los ejes horizontal y vertical. --- count: false Cambiamos la escala de uno de los ejes .panel1-chunk_19-user[ ```r data %>% #<< filter(continent=="Asia", year==2007) %>% #<< ggplot()+ #<< aes(x = gdpPercap, y = lifeExp, #<< size = pop, color = country) %>% #<< geom_point(show.legend = F, alpha = 0.7) #<< ``` ] .panel2-chunk_19-user[ <!-- --> ] --- count: false Cambiamos la escala de uno de los ejes .panel1-chunk_19-user[ ```r data %>% filter(continent=="Asia", year==2007) %>% ggplot()+ aes(x = gdpPercap, y = lifeExp, size = pop, color = country) %>% geom_point(show.legend = F, alpha = 0.7) + scale_x_log10() #<< ``` ] .panel2-chunk_19-user[ <!-- --> ] --- count: false Cambiamos la escala de uno de los ejes .panel1-chunk_19-user[ ```r data %>% filter(continent=="Asia", year==2007) %>% ggplot()+ aes(x = gdpPercap, y = lifeExp, size = pop, color = country) %>% geom_point(show.legend = F, alpha = 0.7) + scale_x_log10() + labs(x = 'GDP Per Capita', #<< y = 'Life Expectancy') #<< ``` ] .panel2-chunk_19-user[ <!-- --> ] <style> .panel1-chunk_19-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-chunk_19-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-chunk_19-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # 7ma Capa: Themes - Funciones: theme_gray(), theme_bw(), theme_classic() - Es la capa que le da la apariencia final que tendrá el gráfico. - Se utiliza para personalizar el estilo del gráfico, pues [modifica elementos del gráfico](https://ggplot2.tidyverse.org/reference/theme.html) no ligados a los datos. - Se puede crear un tema para que se adapte a la imagen institucional de una organización o al tipo de diseño de un documento específico. - Se modifican temas tales como el color del fondo, los ejes, tamaño del gráfico, grilla, posición de los nombres, entre otros. --- count: false Cambiamos los temas de un gráfico de acuerdo a nuestro gusto .panel1-chunk_17-rotate[ ```r data %>% filter(continent=="Asia", year==2007) %>% ggplot()+ aes(x = gdpPercap, y = lifeExp, size = pop, color = country) %>% geom_point(show.legend = F, alpha = 0.7) + scale_x_log10() + labs(x = 'GDP Per Capita', y = 'Life Expectancy') + scale_size(range = c(2, 15))+ theme_gray() #<< ``` ] .panel2-chunk_17-rotate[ <!-- --> ] --- count: false Cambiamos los temas de un gráfico de acuerdo a nuestro gusto .panel1-chunk_17-rotate[ ```r data %>% filter(continent=="Asia", year==2007) %>% ggplot()+ aes(x = gdpPercap, y = lifeExp, size = pop, color = country) %>% geom_point(show.legend = F, alpha = 0.7) + scale_x_log10() + labs(x = 'GDP Per Capita', y = 'Life Expectancy') + scale_size(range = c(2, 15))+ theme_bw() #<< ``` ] .panel2-chunk_17-rotate[ <!-- --> ] --- count: false Cambiamos los temas de un gráfico de acuerdo a nuestro gusto .panel1-chunk_17-rotate[ ```r data %>% filter(continent=="Asia", year==2007) %>% ggplot()+ aes(x = gdpPercap, y = lifeExp, size = pop, color = country) %>% geom_point(show.legend = F, alpha = 0.7) + scale_x_log10() + labs(x = 'GDP Per Capita', y = 'Life Expectancy') + scale_size(range = c(2, 15))+ theme_classic() #<< ``` ] .panel2-chunk_17-rotate[ <!-- --> ] <style> .panel1-chunk_17-rotate { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-chunk_17-rotate { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-chunk_17-rotate { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> ```r data %>% filter(continent=="Asia", year==2007) %>% ggplot()+ aes(x = gdpPercap, y = lifeExp, size = pop, color = country) %>% geom_point(show.legend = F, alpha = 0.7) + scale_x_log10() + labs(x = 'GDP Per Capita', y = 'Life Expectancy') + scale_size(range = c(2, 15))+ theme_gray()+#ROTATE theme_bw() +#ROTATE theme_classic() #ROTATE ``` <!-- --> --- class: inverse, center, middle # Extensiones de ggplot2 Hay mucho más! 120 extensiones disponibles en: https://exts.ggplot2.tidyverse.org/gallery/ --- # gganimate - Amplía la gramática de gráficos implementada por ggplot2 para incluir la descripción de la animación. - Proporciona una gama de nuevas clases de gramática que se pueden agregar al objeto de la trama para personalizar cómo debe cambiar con el tiempo. ```r # install.packages("gganimate") library(gganimate) #Consider installing: #- the `gifski` package for gif output #- the `av` package for video output ``` --- count: false Primero generamos un gráfico .panel1-chunk_14-user[ ```r data %>% #<< ggplot()+ #<< aes(x = gdpPercap, y = lifeExp, #<< size = pop, color = country) + #<< geom_point(show.legend = F, alpha = 0.7) + #<< scale_x_log10() + #<< labs(x = 'GDP Per Capita', #<< y = 'Life Expectancy') + #<< scale_size(range = c(2, 15)) #<< ``` ] .panel2-chunk_14-user[ <!-- --> ] --- count: false Primero generamos un gráfico .panel1-chunk_14-user[ ```r data %>% ggplot()+ aes(x = gdpPercap, y = lifeExp, size = pop, color = country) + geom_point(show.legend = F, alpha = 0.7) + scale_x_log10() + labs(x = 'GDP Per Capita', y = 'Life Expectancy') + scale_size(range = c(2, 15)) ``` ] .panel2-chunk_14-user[ <!-- --> ] <style> .panel1-chunk_14-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-chunk_14-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-chunk_14-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> ```r data %>% ggplot()+ aes(x = gdpPercap, y = lifeExp, size = pop, color = country) + geom_point(show.legend = F, alpha = 0.7) + scale_x_log10() + labs(x = 'GDP Per Capita', y = 'Life Expectancy') + scale_size(range = c(2, 15))#BREAK ``` <!-- --> --- count: false Luego le añadimos la transición animada y lo personalizamos .panel1-chunk_15-user[ ```r data %>% #<< ggplot()+ #<< aes(x = gdpPercap, y = lifeExp, #<< size = pop, color = country) + #<< geom_point(show.legend = F, alpha = 0.7) + #<< scale_x_log10() + #<< labs(x = 'GDP Per Capita', #<< y = 'Life Expectancy') + #<< scale_size(range = c(2, 15)) #<< ``` ] .panel2-chunk_15-user[ <!-- --> ] --- count: false Luego le añadimos la transición animada y lo personalizamos .panel1-chunk_15-user[ ```r data %>% ggplot()+ aes(x = gdpPercap, y = lifeExp, size = pop, color = country) + geom_point(show.legend = F, alpha = 0.7) + scale_x_log10() + labs(x = 'GDP Per Capita', y = 'Life Expectancy') + scale_size(range = c(2, 15)) + transition_time(year) #<< ``` ] .panel2-chunk_15-user[ <!-- --> ] --- count: false Luego le añadimos la transición animada y lo personalizamos .panel1-chunk_15-user[ ```r data %>% ggplot()+ aes(x = gdpPercap, y = lifeExp, size = pop, color = country) + geom_point(show.legend = F, alpha = 0.7) + scale_x_log10() + labs(x = 'GDP Per Capita', y = 'Life Expectancy') + scale_size(range = c(2, 15)) + transition_time(year)+ labs(title = "Producto Bruto Interno vs Esperanza de Vida", #<< subtitle = "Year:{frame_time}") #<< ``` ] .panel2-chunk_15-user[ <!-- --> ] --- count: false Luego le añadimos la transición animada y lo personalizamos .panel1-chunk_15-user[ ```r data %>% ggplot()+ aes(x = gdpPercap, y = lifeExp, size = pop, color = country) + geom_point(show.legend = F, alpha = 0.7) + scale_x_log10() + labs(x = 'GDP Per Capita', y = 'Life Expectancy') + scale_size(range = c(2, 15)) + transition_time(year)+ labs(title = "Producto Bruto Interno vs Esperanza de Vida", subtitle = "Year:{frame_time}") + shadow_wake(0.5) #<< ``` ] .panel2-chunk_15-user[ <!-- --> ] <style> .panel1-chunk_15-user { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-chunk_15-user { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-chunk_15-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> ```r data %>% ggplot()+ aes(x = gdpPercap, y = lifeExp, size = pop, color = country) + geom_point(show.legend = F, alpha = 0.7) + scale_x_log10() + labs(x = 'GDP Per Capita', y = 'Life Expectancy') + scale_size(range = c(2, 15)) + #BREAK transition_time(year)+ #BREAK labs(title = "Producto Bruto Interno vs Esperanza de Vida", subtitle = "Year:{frame_time}") + #BREAK shadow_wake(0.5) ``` <!-- --> --- # ggrepel - Proporciona geoms de texto y etiquetas para 'ggplot2' que ayudan a evitar la superposición de etiquetas de texto. - Las etiquetas se repelen unas de otras y se alejan de los puntos de datos. ```r # install.packages("ggrepel") library(ggrepel) ``` --- count: false Vemos la diferencia entre ggplot2::geom_text() y repel::geom_text_repel() .panel1-chunk_16-rotate[ ```r data %>% filter(continent=="Americas", year==2007) %>% ggplot()+ aes(x = gdpPercap, y = lifeExp, label=country) + geom_point(show.legend = F, alpha = 0.7) + scale_x_log10() + labs(x = 'GDP Per Capita', y = 'Life Expectancy') + scale_size(range = c(2, 15)) + geom_text() #<< ``` ] .panel2-chunk_16-rotate[ <!-- --> ] --- count: false Vemos la diferencia entre ggplot2::geom_text() y repel::geom_text_repel() .panel1-chunk_16-rotate[ ```r data %>% filter(continent=="Americas", year==2007) %>% ggplot()+ aes(x = gdpPercap, y = lifeExp, label=country) + geom_point(show.legend = F, alpha = 0.7) + scale_x_log10() + labs(x = 'GDP Per Capita', y = 'Life Expectancy') + scale_size(range = c(2, 15)) + geom_text_repel(max.overlaps=15) #<< ``` ] .panel2-chunk_16-rotate[ <!-- --> ] <style> .panel1-chunk_16-rotate { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-chunk_16-rotate { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-chunk_16-rotate { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> ```r data %>% filter(continent=="Americas", year==2007) %>% ggplot()+ aes(x = gdpPercap, y = lifeExp, label=country) + geom_point(show.legend = F, alpha = 0.7) + scale_x_log10() + labs(x = 'GDP Per Capita', y = 'Life Expectancy') + scale_size(range = c(2, 15)) + geom_text() + #ROTATE geom_text_repel(max.overlaps=15) #ROTATE ``` <!-- --> --- # Notas finales: - Puedes ver todos los detalles de ggplot2 en la guía elaborada por el mismo Posit: https://github.com/rstudio/cheatsheets/blob/main/data-visualization.pdf - Te recomiendo que explores más sobre las extensiones del ggplot2 y también sobre los argumentos de cada una de las funciones vistas en clase. - Qué gráficos has necesitado en tu día a día o te gustaría construir? Seguramente hay un paquete para ello en R. <!-- adjust font size in this css code chunk, currently 80 --> <style type="text/css"> .remark-code{line-height: 1.5; font-size: 90%} @media print { .has-continuation { display: block; } } code.r.hljs.remark-code{ position: relative; overflow-x: hidden; } code.r.hljs.remark-code:hover{ overflow-x:visible; width: 500px; border-style: solid; } </style>