APPOL Análisis de Utilidad y Margen (2019-2021)


Análisis de Utilidad y Margen 2019-2021

Column

M. UTILIDAD

$ 671.11 mill.

M. MARGEN

19.95 %

TOP 15

Row

GRAFICO DE UTILIDAD

Margen por País y Continente

Row

UTILIDAD CONTINENTE

M.utilidad por Categoría Producto

M.utilidad por tipo de Producto

---
title: "Análisis de Utilidad y Margen"
output:
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    code-fold: true
    code-tools: true
    source_code: embed
---

::: {style="display: flex; align-items: center;"}
<img src="/cloud/project/appol%20logo.png" style="width: 100px; margin-right: 10px;"/>

<h1 style="margin: 0; font-weight: bold; font-size: 24px; font-family: &#39;Arial&#39;, sans-serif; text-transform: uppercase;">

APPOL Análisis de Utilidad y Margen (2019-2021)

</h1>
:::

------------------------------------------------------------------------

```{r setup, include=FALSE}
library(flexdashboard)
library(dplyr)
library(plotly)
library(readxl)
library(stringr)
library(leaflet)
library(treemapify)
library(DT)
library(ggplot2)
library(dplyr)
library(sf)
library(rnaturalearth)
library(tidyr)

# Cargar datos
Appol_Datos <- read_excel("Appol Datos.xlsx") %>%
  mutate(
    Trimestre = paste0("T", str_extract(Periodo, "\\d+")),
    Año = str_extract(Periodo, "\\d{4}"),
    Utilidad = Ingresos - Gastos,
    Margen = round((Utilidad / Ingresos) * 100, 2),
    Codigo_Pais = str_sub(Region, 1, 3),
    Continente = str_extract(Region, "(?<=-\\s)[^/]+"),
    Pais = str_extract(Region, "(?<=/).*")
  )

# Resumen global
resumen <- Appol_Datos %>%
  summarise(
    Ingresos_Totales = sum(Ingresos, na.rm = TRUE),
    Utilidad_Total = sum(Utilidad, na.rm = TRUE),
    Margen_Total = round((Utilidad_Total / Ingresos_Totales) * 100, 2)
  )

# Categorías productos
categorias_productos <- data.frame(
  Tipo_Producto = c("Musica", "TV", "Aplicaciones", "PC", "Celulares", "Tablets", "Accesorios"),
  Categoria_Producto = c("Digital", "Digital", "Digital", "Dispositivos", "Dispositivos", "Dispositivos", "Dispositivos")
)

Appol_Datos <- Appol_Datos %>%
  left_join(categorias_productos, by = c("Tipo Producto" = "Tipo_Producto"))

```

# Análisis de Utilidad y Margen 2019-2021

## Column {data-width="250"}

### M. UTILIDAD

```{r}

valueBox(
  value = paste0("$ ", round(resumen$Utilidad_Total / 1e6, 2), " mill."),
  caption = "M.Utilidad",
  icon = "fa-dollar-sign",
  color = "primary"
)

```

------------------------------------------------------------------------

### M. MARGEN

```{r}
valueBox(
  value = paste0(resumen$Margen_Total, " %"),
  caption = "M.Margen",
  icon = "fa-percent",
  color = "info"
)
```

### TOP 15

```{r}
# # Selecciona los top 15 países con mayor utilidad
top_15_paises <- Appol_Datos %>%
  group_by(Pais) %>%
  summarise(
    M_Utilidad = sum(Utilidad, na.rm = TRUE),
    M_Margen = round((sum(Utilidad, na.rm = TRUE) / sum(Ingresos, na.rm = TRUE)) * 100, 2)
  ) %>%
  arrange(desc(M_Utilidad)) %>%
  head(15)

# Calcula la suma de utilidad y margen para todos los países
suma_total <- data.frame(
  Pais = "Total",
  M_Utilidad = sum(Appol_Datos$Utilidad),
  M_Margen = round((sum(Appol_Datos$Utilidad) / sum(Appol_Datos$Ingresos)) * 100, 2)
)

# Combina los resultados en una sola tabla
tabla_paises <- rbind(top_15_paises, suma_total)

# Formatea la utilidad como peso y el margen como porcentaje
tabla_paises$M_Utilidad <- paste0("$", formatC(tabla_paises$M_Utilidad, format = "f", digits = 2, big.mark = ","))
tabla_paises$M_Margen <- paste0(formatC(tabla_paises$M_Margen, format = "f", digits = 2), "%")

# Muestra la tabla
datatable(tabla_paises[, c("Pais", "M_Utilidad", "M_Margen")], 
          options = list(
            pageLength = 16,
            scrollX = TRUE,
            scrollY = "500px",
            searching = FALSE,
            ordering = FALSE,
            info = FALSE,
            paging = FALSE
          ),
          rownames = FALSE,
          class = "display compact",
          filter = "bottom")
```

## Row {data-height="500"}

### GRAFICO DE UTILIDAD

```{r}
tabla_utilidad <- Appol_Datos %>%
  group_by(Año, Trimestre, Continente) %>%
  summarise(Utilidad = sum(Utilidad, na.rm = TRUE))

tabla_utilidad <- tabla_utilidad %>%
  mutate(Trimestre_Ordenado = factor(paste(Año, Trimestre), levels = unique(paste(Año, Trimestre))))

ggplot(tabla_utilidad, aes(x = Trimestre_Ordenado, y = Utilidad, color = Continente, group = Continente)) +
  geom_line(size = 1) +
  geom_point(size = 2) +
  labs(title = "M.utilidad por Año, Trimestre y Continente", x = "", y = "M.Utilidad") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))
```

### Margen por País y Continente

```{r}
world <- ne_countries(scale = "medium", returnclass = "sf")

datos_agrupados <- Appol_Datos %>%
  group_by(Codigo_Pais, Continente, Pais) %>%
  summarise(Margen = mean(Margen, na.rm = TRUE))

world_data <- world %>%
  select(iso_a3, geometry) %>%
  left_join(datos_agrupados, by = c("iso_a3" = "Codigo_Pais"))

world_data <- world_data %>%
  mutate(
    centroid = st_centroid(geometry),
    lon = st_coordinates(centroid)[, 1],
    lat = st_coordinates(centroid)[, 2]
  )

# Crear el gráfico
ggplot(data = world) +
  geom_sf(fill = "gray90", color = "white") +
  geom_point(data = world_data, aes(x = lon, y = lat, color = Continente, size = abs(Margen)), alpha = 0.6) +
  scale_size_continuous(range = c(2, 10), guide = "none") +  # Elimina la leyenda para 'size'
  scale_color_discrete(name = "Continente") +  # Asegura que la leyenda muestra solo los continentes
  labs(title = "M.margen por País y Continente", x = "", y = "") +
  theme_minimal() +
  theme(legend.position = "bottom")  # Leyenda en la parte inferior

```

## Row {data-WIDTH="300"}

### UTILIDAD CONTINENTE

```{r}

utilidad_continente <- Appol_Datos %>%
  group_by(Continente) %>%
  summarise(Utilidad = sum(Utilidad, na.rm = TRUE))

plot_ly(utilidad_continente, labels = ~Continente, values = ~Utilidad, type = 'pie', hole = 0.5) %>%
  layout(title = "Suma de utilidad por continente", showlegend = TRUE)

```

### M.utilidad por Categoría Producto

```{r}
treemap_categoria <- Appol_Datos %>%
  group_by(Categoria_Producto) %>%
  summarise(Utilidad = sum(Utilidad, na.rm = TRUE))

ggplot(treemap_categoria, aes(area = Utilidad, fill = Categoria_Producto, label = Categoria_Producto)) +
  geom_treemap() +
  geom_treemap_text(color = "white", place = "center", grow = TRUE) +
  labs(title = "M.utilidad por Categoría Producto") +
  theme_minimal() +
  guides(fill = "none")  # Esto oculta la leyenda sin dañar el gráfico


```

### M.utilidad por tipo de Producto

```{r}
treemap_tipo <- Appol_Datos %>%
  group_by(`Tipo Producto`) %>%
  summarise(Utilidad = sum(Utilidad, na.rm = TRUE))

ggplot(treemap_tipo, aes(area = Utilidad, fill = `Tipo Producto`, label = `Tipo Producto`)) +
  geom_treemap() +
  geom_treemap_text(color = "white", place = "center", grow = TRUE) +
  labs(title = "M.utilidad por Tipo Producto") +
  theme_minimal() +
  guides(fill = "none")  # Esto oculta la leyenda sin dañar el gráfico

```