66
[0010] DEFAD: Representación y tabulación de datos Librería ggplot2 Elvira Ferre Jaén, [email protected] Antonio Maurandi, [email protected] Universidad de Murcia Marzo 2018 000R Team (UMU) [0010] DEFAD 1 / 66

[0010] DEFAD: Representación y tabulación de datos ...gauss.inf.um.es/00Rteam/tabR/materiales/tabulaR_sesion3-1-ggplot2.pdf · Cómodefinirungráficoenggplot2 Conjuntodedatosconelquetrabajaremos

Embed Size (px)

Citation preview

[0010] DEFAD: Representación y tabulación de datosLibrería ggplot2

Elvira Ferre Jaén, [email protected] Maurandi, [email protected]

Universidad de Murcia

Marzo 2018

000R Team (UMU) [0010] DEFAD 1 / 66

Introducción al paquete ggplot2

Introducción al paquete ggplot2

000R Team (UMU) [0010] DEFAD 2 / 66

Introducción al paquete ggplot2

Instalación y carga

Instalación

install.packages( "ggplot2" )# install.packages( "tidyverse" )

Carga del paquete

library( ggplot2 )# library( tidyverse )

000R Team (UMU) [0010] DEFAD 3 / 66

Introducción al paquete ggplot2 The Grammar of Graphics

The Grammar of Graphics

000R Team (UMU) [0010] DEFAD 4 / 66

Introducción al paquete ggplot2 The Grammar of Graphics

Partes de ggplot:

datos y vinculación de variables con atributos estéticos (aestheticmapping)objeto geométrico, lo que uno ve: puntos, líneas, barras, etc.transformaciones estadísticasescalassistema de coordenadasdivisión en subconjuntos (facetting)

000R Team (UMU) [0010] DEFAD 5 / 66

Cómo definir un gráfico en ggplot2

Cómo definir un gráfico en ggplot2

000R Team (UMU) [0010] DEFAD 6 / 66

Cómo definir un gráfico en ggplot2

Conjunto de datos con el que trabajaremos

Previamente pasamos a factor las variables: cyl, vs, am, gear y carb.

head( mtcars, 12 )

## mpg cyl disp hp drat wt qsec vs am gear carb## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3

000R Team (UMU) [0010] DEFAD 7 / 66

Cómo definir un gráfico en ggplot2 Data and aesthetic mapping

Data and aesthetic mapping

000R Team (UMU) [0010] DEFAD 8 / 66

Cómo definir un gráfico en ggplot2 Data and aesthetic mapping

gg <- ggplot( data = mtcars, mapping = aes(x = wt, y = disp))gg

100

200

300

400

2 3 4 5

wt

disp

000R Team (UMU) [0010] DEFAD 9 / 66

Cómo definir un gráfico en ggplot2 Geoms

Geoms

000R Team (UMU) [0010] DEFAD 10 / 66

Cómo definir un gráfico en ggplot2 Geoms

gg + geom_point()

100

200

300

400

2 3 4 5

wt

disp

000R Team (UMU) [0010] DEFAD 11 / 66

Cómo definir un gráfico en ggplot2 Geoms

gg + geom_line()

100

200

300

400

2 3 4 5

wt

disp

000R Team (UMU) [0010] DEFAD 12 / 66

Cómo definir un gráfico en ggplot2 Geoms

gg + geom_line() +geom_point()

100

200

300

400

2 3 4 5

wt

disp

000R Team (UMU) [0010] DEFAD 13 / 66

Cómo definir un gráfico en ggplot2 Geoms

gg + geom_line( colour = "darkblue" )+geom_point()

100

200

300

400

2 3 4 5

wt

disp

000R Team (UMU) [0010] DEFAD 14 / 66

Cómo definir un gráfico en ggplot2 Geoms

gg + geom_line( ) +geom_point( colour = "red" )

100

200

300

400

2 3 4 5

wt

disp

000R Team (UMU) [0010] DEFAD 15 / 66

Cómo definir un gráfico en ggplot2 Geoms

gg + geom_line( colour = "darkblue" ) +geom_point( colour = "red" )

100

200

300

400

2 3 4 5

wt

disp

000R Team (UMU) [0010] DEFAD 16 / 66

Cómo definir un gráfico en ggplot2 Geoms

gg + geom_line( colour = "darkblue" ) +geom_point( mapping = aes( colour = vs ),

size = 3 )

100

200

300

400

2 3 4 5

wt

disp

vs

0

1

000R Team (UMU) [0010] DEFAD 17 / 66

Cómo definir un gráfico en ggplot2 Geoms

gg + geom_line( mapping = aes( colour = vs ,linetype = vs ) ) +

geom_point()

100

200

300

400

2 3 4 5

wt

disp

vs

0

1

000R Team (UMU) [0010] DEFAD 18 / 66

Cómo definir un gráfico en ggplot2 Geoms

ggplot(mtcars, aes(x = wt, y = disp, colour = vs )) +geom_point( ) +geom_line()

100

200

300

400

2 3 4 5

wt

disp

vs

0

1

000R Team (UMU) [0010] DEFAD 19 / 66

Cómo definir un gráfico en ggplot2 Stats

Stats

000R Team (UMU) [0010] DEFAD 20 / 66

Cómo definir un gráfico en ggplot2 Stats

ggplot( mtcars, aes( x = wt, y = disp ) ) +geom_point() +stat_smooth( method = "lm" )

0

200

400

2 3 4 5

wt

disp

000R Team (UMU) [0010] DEFAD 21 / 66

Cómo definir un gráfico en ggplot2 Stats

ggplot( mtcars, aes( x = wt, y = disp ) ) +geom_point() +geom_smooth( method = "lm", se = FALSE )

100

200

300

400

500

2 3 4 5

wt

disp

000R Team (UMU) [0010] DEFAD 22 / 66

Cómo definir un gráfico en ggplot2 Stats

ggplot( mtcars, aes( x = wt, y = disp ) ) +geom_point( aes( colour = gear ) ) +stat_smooth( method = "lm", se = FALSE )

100

200

300

400

500

2 3 4 5

wt

disp

gear

3

4

5

000R Team (UMU) [0010] DEFAD 23 / 66

Cómo definir un gráfico en ggplot2 Stats

ggplot( mtcars, aes( x = wt, y = disp, colour = gear ) ) +geom_point() +stat_smooth( method = "lm", se = F )

100

200

300

400

2 3 4 5

wt

disp

gear

3

4

5

000R Team (UMU) [0010] DEFAD 24 / 66

Cómo definir un gráfico en ggplot2 Stats

ggplot( mtcars, aes( x = wt, y = disp, colour = gear ) ) +geom_point() +stat_smooth( aes( colour = NULL ), method = "lm", se = F )

100

200

300

400

500

2 3 4 5

wt

disp

gear

3

4

5

000R Team (UMU) [0010] DEFAD 25 / 66

Cómo definir un gráfico en ggplot2 Gráficos de barras

Gráficos de barras

000R Team (UMU) [0010] DEFAD 26 / 66

Cómo definir un gráfico en ggplot2 Gráficos de barras

ggplot( data = mtcars, aes( x = gear) ) +geom_bar()

0

5

10

15

3 4 5

gear

coun

t

000R Team (UMU) [0010] DEFAD 27 / 66

Cómo definir un gráfico en ggplot2 Gráficos de barras

ggplot( data = mtcars ) +geom_bar( mapping = aes( x = gear,y = ..prop.., group = 1 ) )

0.0

0.1

0.2

0.3

0.4

3 4 5

gear

prop

000R Team (UMU) [0010] DEFAD 28 / 66

Cómo definir un gráfico en ggplot2 Gráficos de barras

ggplot( data = mtcars ) +geom_bar( aes( x = gear, fill = gear) )

0

5

10

15

3 4 5

gear

coun

t

gear

3

4

5

000R Team (UMU) [0010] DEFAD 29 / 66

Cómo definir un gráfico en ggplot2 Gráficos de barras

ggplot( data = mtcars, aes( x = vs, fill = gear ) ) +geom_bar()

0

5

10

15

0 1

vs

coun

t

gear

3

4

5

000R Team (UMU) [0010] DEFAD 30 / 66

Cómo definir un gráfico en ggplot2 Gráficos de barras

ggplot( data = mtcars, aes( x = vs, fill = gear ) ) +geom_bar( position = position_dodge() )

0.0

2.5

5.0

7.5

10.0

12.5

0 1

vs

coun

t

gear

3

4

5

000R Team (UMU) [0010] DEFAD 31 / 66

Cómo definir un gráfico en ggplot2 Boxplot

Boxplot

000R Team (UMU) [0010] DEFAD 32 / 66

Cómo definir un gráfico en ggplot2 Boxplot

ggplot( mtcars, aes( x = gear , y = wt ) ) +geom_boxplot()

2

3

4

5

3 4 5

gear

wt

000R Team (UMU) [0010] DEFAD 33 / 66

Cómo definir un gráfico en ggplot2 Boxplot

ggplot( mtcars, aes( x = gear , y = wt ) ) +geom_boxplot( outlier.colour = "red" )

2

3

4

5

3 4 5

gear

wt

000R Team (UMU) [0010] DEFAD 34 / 66

Cómo definir un gráfico en ggplot2 Boxplot

ggplot( mtcars, aes( x = gear , y = wt, fill = vs ) ) +geom_boxplot()

2

3

4

5

3 4 5

gear

wt

vs

0

1

000R Team (UMU) [0010] DEFAD 35 / 66

Cómo definir un gráfico en ggplot2 Boxplot

ggplot( mtcars, aes( x = gear, y = wt, fill = vs ) ) +geom_boxplot() +coord_flip()

3

4

5

2 3 4 5

wt

gear

vs

0

1

000R Team (UMU) [0010] DEFAD 36 / 66

Cómo definir un gráfico en ggplot2 Facetting

Facetting

000R Team (UMU) [0010] DEFAD 37 / 66

Cómo definir un gráfico en ggplot2 Facetting

Dividir por factores

ggplot( mtcars, aes( x = wt, y = disp ) ) +geom_point( aes( colour = gear ) ) +facet_grid( vs ~ . )

01

2 3 4 5

100

200

300

400

100

200

300

400

wt

disp

gear

3

4

5

000R Team (UMU) [0010] DEFAD 38 / 66

Cómo definir un gráfico en ggplot2 Facetting

facet_grid(v ~ h)

ggplot( mtcars, aes( x = wt, y = disp ) ) +geom_point( aes( colour = gear ) ) +facet_grid( vs ~ cyl )

4 6 8

01

2 3 4 5 2 3 4 5 2 3 4 5

100

200

300

400

100

200

300

400

wt

disp

gear

3

4

5

000R Team (UMU) [0010] DEFAD 39 / 66

Cómo definir un gráfico en ggplot2 Facetting

Dividir por filas o columnas

ggplot( mtcars, aes( x = wt, y = disp ) ) +geom_point( aes( colour = gear ) ) +facet_wrap( ~ carb, nrow = 2 )

4 6 8

1 2 3

2 3 4 5 2 3 4 5 2 3 4 5

100

200

300

400

100

200

300

400

wt

disp

gear

3

4

5

000R Team (UMU) [0010] DEFAD 40 / 66

Cómo definir un gráfico en ggplot2 Colors

Colors

000R Team (UMU) [0010] DEFAD 41 / 66

Cómo definir un gráfico en ggplot2 Colors

Existen dos argumentos principales para añadir color a los gráficos, coloury fill .

Puntos y líneas: colour = “red”Objetos con relleno: fill = “red”

000R Team (UMU) [0010] DEFAD 42 / 66

Cómo definir un gráfico en ggplot2 Colors

ggplot( mtcars, aes( x = gear ) ) +geom_bar( stat="count", fill="#2CA25F", colour="black")

0

5

10

15

3 4 5

gear

coun

t

000R Team (UMU) [0010] DEFAD 43 / 66

Cómo definir un gráfico en ggplot2 Colors

ggplot( data = mtcars, aes( x = vs, fill = gear ) ) +geom_bar( position = position_dodge() ) +scale_fill_manual(values = c("#CC79A7", "#009E73", "#56B4E9"))

0.0

2.5

5.0

7.5

10.0

12.5

0 1

vs

coun

t

gear

3

4

5

000R Team (UMU) [0010] DEFAD 44 / 66

Cómo definir un gráfico en ggplot2 Colors

ggplot( mtcars, aes( x = wt, y = disp ) ) +geom_point( aes( colour = gear ) ) +scale_color_manual(values = c("#999999", "#E69F00", "#56B4E9"))

100

200

300

400

2 3 4 5

wt

disp

gear

3

4

5

000R Team (UMU) [0010] DEFAD 45 / 66

Cómo definir un gráfico en ggplot2 Colors

ggplot( mtcars, aes( x = wt, y = disp ) ) +geom_point( aes( colour = gear ) ) +scale_color_brewer( palette = "Dark2" )

100

200

300

400

2 3 4 5

wt

disp

gear

3

4

5

000R Team (UMU) [0010] DEFAD 46 / 66

Cómo definir un gráfico en ggplot2 Colors

ggplot( mtcars, aes( x = wt, y = disp ) ) +geom_point( aes( colour = gear ) ) +scale_color_brewer( palette = "Set1" ) +theme_bw()

100

200

300

400

2 3 4 5

wt

disp

gear

3

4

5

000R Team (UMU) [0010] DEFAD 47 / 66

Cómo definir un gráfico en ggplot2 Títulos y etiquetas

Títulos y etiquetas

000R Team (UMU) [0010] DEFAD 48 / 66

Cómo definir un gráfico en ggplot2 Títulos y etiquetas

ggplot( mtcars, aes( x = gear , y = wt ) ) +geom_boxplot() +labs( title = "Número de engranajes según el peso")

2

3

4

5

3 4 5

gear

wt

Número de engranajes según el peso

000R Team (UMU) [0010] DEFAD 49 / 66

Cómo definir un gráfico en ggplot2 Títulos y etiquetas

ggplot( mtcars, aes( x = gear , y = wt ) ) +geom_boxplot() +labs( title = "Número de engranajes según el peso",

x = "Número de marchas", y = "Peso (Kg)" )

000R Team (UMU) [0010] DEFAD 50 / 66

Cómo definir un gráfico en ggplot2 Títulos y etiquetas

2

3

4

5

3 4 5

Número de marchas

Pes

o (K

g)

Número de engranajes según el peso

000R Team (UMU) [0010] DEFAD 51 / 66

Cómo definir un gráfico en ggplot2 Títulos y etiquetas

bp <- ggplot( mtcars, aes( x = gear , y = wt, fill = gear ) ) +geom_boxplot() +labs( x = "Número de marchas", y = "Peso (Kg)" )

000R Team (UMU) [0010] DEFAD 52 / 66

Cómo definir un gráfico en ggplot2 Ejes

Ejes

000R Team (UMU) [0010] DEFAD 53 / 66

Cómo definir un gráfico en ggplot2 Ejes

bp <- ggplot( mtcars, aes( x = gear , y = wt, fill = gear ) ) +geom_boxplot() +labs( x = "Número de marchas", y = "Peso (Kg)" )

2

3

4

5

3 4 5

Número de marchas

Pes

o (K

g)

gear

3

4

5

000R Team (UMU) [0010] DEFAD 54 / 66

Cómo definir un gráfico en ggplot2 Ejes

Ejes discretos

bp + scale_x_discrete( breaks = c( "3", "4", "5" ),labels = c( "mod1", "mod2", "mod3"))

2

3

4

5

mod1 mod2 mod3

Número de marchas

Pes

o (K

g)

gear

3

4

5

000R Team (UMU) [0010] DEFAD 55 / 66

Cómo definir un gráfico en ggplot2 Ejes

Ejes continuos

bp + scale_y_continuous( limits = c( 0, 8 ) )

0

2

4

6

8

mod1 mod2 mod3

Número de marchas

Pes

o (K

g)

gear

3

4

5

Más información000R Team (UMU) [0010] DEFAD 56 / 66

Cómo definir un gráfico en ggplot2 Leyendas

Leyendas

000R Team (UMU) [0010] DEFAD 57 / 66

Cómo definir un gráfico en ggplot2 Leyendas

bp + scale_fill_discrete( name = "Marchas" )

2

3

4

5

3 4 5

Número de marchas

Pes

o (K

g)

Marchas

3

4

5

000R Team (UMU) [0010] DEFAD 58 / 66

Cómo definir un gráfico en ggplot2 Leyendas

bp +scale_fill_discrete( name = "Marchas",

breaks = c( "3", "4", "5" ),labels = c( "mod1", "mod2", "mod3" ) )

2

3

4

5

3 4 5

Número de marchas

Pes

o (K

g)

Marchas

mod1

mod2

mod3

000R Team (UMU) [0010] DEFAD 59 / 66

Cómo definir un gráfico en ggplot2 Leyendas

bp + scale_x_discrete( breaks = c( "3", "4", "5" ),labels = c( "mod1", "mod2", "mod3")) +

scale_fill_discrete( name = "Marchas",breaks = c( "3", "4", "5" ),labels = c( "model1", "model2", "model3"))

000R Team (UMU) [0010] DEFAD 60 / 66

Cómo definir un gráfico en ggplot2 Leyendas

2

3

4

5

mod1 mod2 mod3

Número de marchas

Pes

o (K

g)

Marchas

mod1

mod2

mod3

000R Team (UMU) [0010] DEFAD 61 / 66

Cómo definir un gráfico en ggplot2 Más de un gráfico por device

Más de un gráfico por device

000R Team (UMU) [0010] DEFAD 62 / 66

Cómo definir un gráfico en ggplot2 Más de un gráfico por device

# library(gridExtra)p1 <- ggplot( data = iris,

aes(x = Sepal.Length, y = Sepal.Width,col = Species ) ) +

geom_point(size = 0.7)

p2 <- ggplot( data = mtcars,aes( x = mpg, y = qsec,

col = factor( gear ) ) ) +geom_point( size = 0.7 )

grid.arrange( p1, p2, nrow = 1 )

000R Team (UMU) [0010] DEFAD 63 / 66

Cómo definir un gráfico en ggplot2 Más de un gráfico por device

2.0

2.5

3.0

3.5

4.0

4.5

5 6 7 8

Sepal.Length

Sep

al.W

idth Species

setosa

versicolor

virginica

16

18

20

22

10 15 20 25 30 35

mpg

qsec

factor(gear)

3

4

5

000R Team (UMU) [0010] DEFAD 64 / 66

Enlaces y referencias

Enlaces y referencias

000R Team (UMU) [0010] DEFAD 65 / 66

Enlaces y referencias

Enlaces y referencias

Enlace a la página de CRANPágina oficialManualA Layered Grammar of Graphics, Hadley WickhamCookbook-RA Simple Introduction to the Graphing Philosophy of ggplot2Color Brewer: Color advice for cartography

000R Team (UMU) [0010] DEFAD 66 / 66