Análisis Exploratorio de Datos

Row

Row

Row

Row

Row

Row

Row

Row

Row

Row

Row

Row

Row

Row

Row

Row

Row

---
title: "Análisis Exploratorio de Datos (EDA)"
author: "Ma. Guadalupe Cervantes Castillo"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
  flexdashboard::flex_dashboard:
    orientation: rows
    social: menu
    source_code: embed

#  pdf_document: default
 # word_document: default
  #html_notebook: default
---

## Análisis Exploratorio de Datos

```{r setup, include=FALSE}
#{r, echo=FALSE, warning=FALSE}
library(tidyverse)
library(highcharter)
library(ggplot2)
library(plotly)
library(plyr)
library(dplyr)
library(viridisLite)
library(forecast)
library(treemap)
library(arules)
library(flexdashboard)

thm <- 
  hc_theme(
    colors = c("#1a6ecc", "#434348", "#90ed7d"),
    chart = list(
      backgroundColor = "transparent",
      style = list(fontFamily = "Source Sans Pro")
    ),
    xAxis = list(
      gridLineWidth = 1
    )
  )
```


```{r discripcion-variables, eval=FALSE, echo=FALSE}
 str(mpg)
``` 
Row
-----------------------------------------------------------------------
```{r}
 p <- ggplot(mpg, aes(x = displ, y = hwy, color = trans)) +
  geom_point(shape=11, size=2 ) +
  labs(
    title = "Millas por galón vs. Cilindrada, clasificado por transmisión"
  )
ggplotly(p)
```


```{r}
# Left 
p <- ggplot(mpg, aes(x = displ, y = hwy, color = class)) +
  geom_point(shape=2) +
labs(
    title = "Millas por galón vs. Cilíndros, clasificado por tipo de auto"
  )
ggplotly(p)

```

Row
-----------------------------------------------------------------------

```{r}
# Right ok
p <- ggplot(mpg, aes(x = displ, y = hwy, shape = drv)) +
  geom_point(color="firebrick") +
  labs(
    title = "Millas por galón vs. Cilindros, clasificado por tipo de tracción"
  )
ggplotly(p)
```

```{r}

# ok
p <- ggplot(mpg) + 
  geom_point(aes(x = displ, y = hwy), color = "green", shape=1) +
labs(
    title = "Millas de carretera por galón vs. Cilindrada de motor"
  )
ggplotly(p)
```

Row
-----------------------------------------------------------------------

```{r} 
# ok
p <- ggplot(mpg, aes(x = displ, y = hwy)) + 
  geom_point(color = "blue", shape=25) +
  labs(
    title = "Millas de carretera por galón vs. Cilindrada de motor"
  )
ggplotly(p)
```




```{r}
# Left ok
p <- ggplot(mpg, aes(x = displ, y = hwy, size = class, color=class)) +
  geom_point(shape = 21, stroke = 1) +
labs(
    title = "Millas por galón vs. Cilíndros, clasificado por tipo de coche"
  )
ggplotly(p)
```

Row
-----------------------------------------------------------------------

```{r}
# ok
p <- ggplot(mpg, aes(x = displ, y = hwy, size = displ)) +
  geom_point(color="orange", shape=21) +
  labs(
    title = "Millas de carretera por galón vs. Cilindrada de motor"
  )
ggplotly(p)
```


```{r}
# ok
p<- ggplot(mpg, aes(x = displ, y = hwy, size = displ, color=displ)) +
  geom_point() +
  labs(
    title = "Millas/galón vs. Cilindrada de motor, clasificado por cilindrada"
  )
ggplotly(p)
```

Row
-----------------------------------------------------------------------

```{r}
# ok
p <- mpg %>% 
  ggplot(aes(x=displ,y=hwy, size=displ, color=cyl)) +
  geom_point() +
  labs(
    title = "Millas galón vs. cilindrada, clasificado por núm. de cilíndros"
  )
ggplotly(p)
```

```{r}
# Right  ok
p <- ggplot(mpg, aes(x = displ, y = hwy, alpha = displ)) +
  geom_point(color="blue") +
  labs(
    title = "Millas de carretera por galón vs. Cilindrada de motor"
  )
ggplotly(p)
```

Row
-----------------------------------------------------------------------

```{r}
# Right ok
p <- ggplot(mpg, aes(x = displ, y = hwy, alpha = displ, color=displ)) +
  geom_point() +
  labs(
    title = "Millas galón vs. Cilindrada motor, clasificada por cilindrada"
  )
ggplotly(p)
```



```{r}
# ok
p <- mpg |>
  ggplot(aes(x = displ, y = hwy)) + 
  geom_point(colour="blue") +
  labs(
    title = "Millas de carretera por galón vs. Cilindrada de motor"
  )
ggplotly(p)
```

Row
-----------------------------------------------------------------------

```{r}
# ok
p <- ggplot(mpg, aes(x = displ, y = hwy, color = drv)) + 
  geom_point() +
  geom_smooth(aes(linetype = drv)) +
  labs(
    title = "Millas galón vs. Cilindrada, clasificada por tipo tren motriz"
  )
ggplotly(p)
```


```{r}
# ok
p <- ggplot(mpg, aes(x = displ, y = hwy)) + 
  geom_point(aes(color = class)) + 
  geom_smooth() +
labs(
    title = "Millas por galón vs. Cilíndros, clasificado por tipo de coche"
  )
ggplotly(p)
```




```{r filtro-uno, eval=FALSE, echo=FALSE}
mpg |> 
  filter(class == "2seater") |> 
  filter( trans == "r")
  
  
```

Row
-----------------------------------------------------------------------

```{r}
# ok
p <- ggplot(mpg, aes(x = displ, y = hwy)) + 
  geom_point() + 
  geom_point(
    data = mpg |> filter(class == "2seater"), 
    color = "orange"
  ) +
  geom_point(
    data = mpg |> filter(class == "compact"), 
    shape = 1, size = 3, color = "red"
  ) +
labs(
    title = "Millas/galón vs. Cilíndros, 2seater (orange) y compact (red)"
  )
ggplotly(p)
```



```{r}
# ok
p <- ggplot(mpg, aes(class, hwy)) +
  geom_boxplot(aes(colour = class, fill = class)) +
labs(
    title = "Millas de carretera por galón vs. tipo de coche"
  )
ggplotly(p)
```

Row
-----------------------------------------------------------------------

```{r}
# ok
p<- ggplot(mpg, aes(class, hwy)) +
  geom_boxplot(aes(colour = class, fill = after_scale(alpha(colour, 0.4)))) +
labs(
    title = "Millas de carretera por galón vs. tipo de coche"
  )
ggplotly(p)
```



```{r}
# Making a proportional stacked density plot - ok
p <- ggplot(mpg, aes(cty)) +
  geom_density(
    aes(
      colour = factor(cyl),
      fill = after_scale(alpha(colour, 0.3)),
      y = after_stat(count / sum(n[!duplicated(group)]))
    ),
    position = "stack", bw = 1
  ) +
  geom_density(bw = 1) +
labs(
    title = "Millas ciudad/galón vs. densidad, clasif. número de cilindros"
  )
 ggplotly(p)
```

Row
-----------------------------------------------------------------------

```{r}
# Imitating a ridgeline plot - duda por que muestra el rotulo group - ndesity
p <- ggplot(mpg, aes(cty, colour = factor(cyl))) +
  geom_ribbon(
    stat = "density", outline.type = "upper",
    aes(
      fill = after_scale(alpha(colour, 0.3)),
      ymin = after_stat(group),
      ymax = after_stat(group + ndensity)
    )
  ) +
labs(
    title = "Densidad vs. millas ciudad/galón, agrupado por núm. cilíndros"
  )
ggplotly(p)
```



```{r}
# no encontre el equivalente para ggplotly
# https://exts.ggplot2.tidyverse.org/gallery/
library(ggridges)

ggplot(mpg, aes(x = hwy, y = drv, fill = drv, color = drv)) +
  geom_density_ridges(alpha = 0.5, show.legend = FALSE) +
labs(
    title = "Tipo de tren motriz vs. millas de carretera por galón"
  )
#> Picking joint bandwidth of 1.28


```

Row
-----------------------------------------------------------------------

```{r}
# ok
p <- ggplot(mpg, aes(x = displ, y = hwy, color=displ)) + 
  geom_point() + 
  facet_wrap(~cyl) +
labs(
    title = "Millas carretera/galón vs. cilindrada motor, por cilindrada"
  )
ggplotly(p)
```



```{r}
# ok
p <- ggplot(mpg, aes(x = displ, y = hwy)) + 
  geom_point(color="blue") + 
  facet_grid(drv ~ cyl) +
labs(
    title = "Millas carretera/galón vs. cilindrada, cilíndros y tracción "
  )
ggplotly(p)
```

Row
-----------------------------------------------------------------------

```{r}
# ok
p <- ggplot(mpg, aes(x = displ, y = hwy)) + 
  geom_point() + 
  facet_grid(drv ~ cyl, scales = "free_y") +
labs(
    title = "Millas carretera/galón vs. cilindrada, cilíndros y tracción "
  )
ggplotly(p)
```



```{r}
# ok
p <- ggplot(mpg) + 
  geom_point(aes(x = displ, y = hwy)) +
  facet_grid(drv ~ .) +
labs(
    title = "Millas carretera/galón vs. cilindrada y tracción "
  )
ggplotly(p)
```

Row
-----------------------------------------------------------------------

```{r}
# ok
p <- ggplot(mpg) + 
  geom_point(aes(x = displ, y = hwy, color=displ)) +
  facet_grid(. ~ cyl) +
labs(
    title = "Millas carretera/galón vs. cilindrada, núm. cilíndros"
  )
ggplotly(p)
```



```{r}
# ok
p <- ggplot(mpg) + 
  geom_point(aes(x = displ, y = hwy)) + 
  facet_wrap(~ class, nrow = 2) +
labs(
    title = "Millas carretera/galón vs. cilindrada, por tipo de coche"
  )
ggplotly(p)
```

Row
-----------------------------------------------------------------------

```{r}
# Left - ok
p <- ggplot(mpg, aes(x = drv, color = drv)) + 
  geom_bar() +
labs(
    title = "Total por tipo de tracción"
  )
ggplotly(p)

```



```{r}
# Right - ok
p <- ggplot(mpg, aes(x = drv, fill = drv)) + 
  geom_bar() +
labs(
    title = "Total por tipo de tracción"
  )
ggplotly(p)
```

Row
-----------------------------------------------------------------------

```{r}
# ok
p <- ggplot(mpg, aes(x = drv, fill = class)) + 
  geom_bar() +
labs(
    title = "Total por tipo de tracción, clasificado por tipo de automóvil"
  )
ggplotly(p)
```



```{r}
# ok
p <- ggplot(mpg, aes(x = drv, fill = class)) + 
  geom_bar(alpha = 1/2, position = "identity") +
labs(
    title = "Total por tipo de tracción, clasificado por tipo de automóvil"
  )
ggplotly(p)
```

Row
-----------------------------------------------------------------------

```{r}
# Right - ok
p <- ggplot(mpg, aes(x = drv, color = class)) + 
  geom_bar(fill = NA, position = "identity") +
labs(
    title = "Total por tipo de tracción, clasificado por tipo de automóvil"
  )
ggplotly(p)
```



```{r}
# Left - ok
p <- ggplot(mpg, aes(x = drv, fill = class)) + 
  geom_bar(position = "fill") +
labs(
    title = "Porcentaje por tipo de tracción, clasif. por tipo de coche"
  )
ggplotly(p)

```

Row
-----------------------------------------------------------------------

```{r}
# Right - ok
p <- ggplot(mpg, aes(x = drv, fill = class)) + 
  geom_bar(position = "dodge") +
labs(
    title = "Total por tipo de tracción y clasificado por tipo de automóvil"
  )
ggplotly(p)
```

```{r}
g <- ggplot(mpg, aes(x = cty, y = cyl))+
labs(
    title = "Número de cilíndros vs. millas de ciudad por galón"
  )

ggplotly(g + geom_point(color = "firebrick", shape = "diamond", size = 2)) 
```