Ejercicio 06

Fichero de datos

Vamos a trabajar con el conjunto de datos mpg integrado en el paquete ggplot2.

head( mpg )
## # A tibble: 6 x 11
##   manufacturer model displ  year   cyl trans drv     cty   hwy fl    class
##   <chr>        <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 audi         a4     1.80  1999     4 auto… f        18    29 p     comp…
## 2 audi         a4     1.80  1999     4 manu… f        21    29 p     comp…
## 3 audi         a4     2.00  2008     4 manu… f        20    31 p     comp…
## 4 audi         a4     2.00  2008     4 auto… f        21    30 p     comp…
## 5 audi         a4     2.80  1999     6 auto… f        16    26 p     comp…
## 6 audi         a4     2.80  1999     6 manu… f        18    26 p     comp…

Las variables numéricas que más nos interesan son:

  • displ: tamaño del motor de un coche, en litros.
  • hwy: eficiencia del combustible

Represetanción gráfica

1. A partir del gráfico siguiente, añade una curva que se ajuste a los datos.

ggplot( data = mpg, aes( x = displ, y = hwy ) ) + 
  geom_point( aes( colour = drv ) ) 

Añado la curva

ggplot( data = mpg, aes( x = displ, y = hwy ) ) + 
  geom_point( aes( colour = drv ) ) + 
  stat_smooth()

2. Modifica el código anterior para que también dibuje una curva distinta para cada nivel de drv. Haz que no aparezca el intervalo de confianza

ggplot( data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + 
    geom_point() + 
    geom_smooth( se = FALSE )

3. Trata de reproducir el siguiente gráfico

Gráfico ejemplo

Gráfico ejemplo

El código es:

ggplot( data = mpg, mapping = aes(x = displ, y = hwy)) + 
    geom_point( aes( color = drv )) + 
    geom_smooth( aes( linetype = drv), se = FALSE )

Observación: para exportar el gráfico utilicé la función ggsave()

Deja trata de la sesión

sessionInfo()
## R version 3.4.4 (2018-03-15)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 16.04.4 LTS
## 
## Matrix products: default
## BLAS: /usr/lib/libblas/libblas.so.3.6.0
## LAPACK: /usr/lib/lapack/liblapack.so.3.6.0
## 
## locale:
##  [1] LC_CTYPE=es_ES.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=es_ES.UTF-8        LC_COLLATE=es_ES.UTF-8    
##  [5] LC_MONETARY=es_ES.UTF-8    LC_MESSAGES=es_ES.UTF-8   
##  [7] LC_PAPER=es_ES.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=es_ES.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] ggplot2_2.2.1
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.15     knitr_1.18       magrittr_1.5     munsell_0.4.3   
##  [5] colorspace_1.3-2 rlang_0.2.0      stringr_1.2.0    plyr_1.8.4      
##  [9] tools_3.4.4      grid_3.4.4       gtable_0.2.0     utf8_1.1.3      
## [13] cli_1.0.0        htmltools_0.3.6  assertthat_0.2.0 yaml_2.1.16     
## [17] lazyeval_0.2.1   rprojroot_1.3-2  digest_0.6.14    tibble_1.4.2    
## [21] crayon_1.3.4     evaluate_0.10.1  rmarkdown_1.8    labeling_0.3    
## [25] stringi_1.1.6    compiler_3.4.4   pillar_1.1.0     scales_0.5.0    
## [29] backports_1.1.2