Mi primer informe en R markdown
Pasos para la pre-entrega y Entrega de proyecto final.
R Markdown documents are fully reproducible. Use a productive notebook interface to weave together narrative text and code to produce elegantly formatted output. Use multiple languages including R, Python, and SQL.
Más información sobre armado de columnas en rmarkdown-cookbook
Pasos previos
- Preparo las librerías con las que voy a trabajar
#install.packages("DT")
library(tidyverse)
#gráficos
library(plotly)
#tablas
library(DT)
library(gt)
Revisión del dataset con el que voy a trabajar
Descargamos la base Spotify y Youtube en Kaggle.com
Para crear un nuevo chunk de ejecución de código utilizo \(CTRL + ALT + I\)
Manipulación de dataset
Trasformo el dataset acorde a mi pregunta-problema y objetivo de investigación.
Para los siguientes gráficos realizo la sumarización de promedios de cantidad de streaming.
\(\text{porcentaje Youtube} = \frac{\text{(Promedio Youtube)}}{\text{(Promedio Total)}}*100\)
\(\text{porcentaje Spotify} = \frac{\text{(Promedio Spotify)}}{\text{(Promedio Total)}}*100\)
Spotify_youtube_reducido <- Spotify_youtube %>%
select(Artist, Track, Album_type, Stream, Channel, Views) %>%
group_by(Artist) %>%
summarise(
promedio_youtube = mean(Views),
promedio_spotify = mean(Stream),
promedio_total = mean(Views+Stream),
porcent_Y = round(promedio_youtube/promedio_total*100,2),
porcent_S = round(promedio_spotify/promedio_total*100,2)) %>%
arrange(desc(promedio_total)) %>%
head(20)
Armado de tablas
Veamos algunos ejemplos de generación de tablas desde Rmarkdown con algunas librerías
Tabla simple
Un ejemplo de tabla simple con las fechas de entrega de proyecto final
| Entregas | Fecha inicio | Fecha Límite |
|---|---|---|
| Pre-entrega | 03 de abril | 10 de abril |
| Entrega | 19 de abril | 9 de mayo |
Tabla con librería gt
- Conoce más información sobre la libreria en su web oficial
gt(Spotify_youtube_reducido) %>%
tab_header(
title = "Artistas más escuchados en Spotify y Youtube") %>% #agrego titulo
tab_source_note(
source_note = md("Fuente: kaggle.com") ) # agrego fuente o pie de página
| Artistas más escuchados en Spotify y Youtube | |||||
| Artist | promedio_youtube | promedio_spotify | promedio_total | porcent_Y | porcent_S |
|---|---|---|---|---|---|
| Ed Sheeran | 1546020777 | 1439488156 | 2985508933 | 51.78 | 48.22 |
| Justin Bieber | 1099106024 | 1209776742 | 2308882766 | 47.60 | 52.40 |
| Coldplay | 999727788 | 1177847824 | 2177575612 | 45.91 | 54.09 |
| Post Malone | 639442021 | 1525126385 | 2164568406 | 29.54 | 70.46 |
| Dua Lipa | 821633931 | 1340807627 | 2162441558 | 38.00 | 62.00 |
| Bruno Mars | 1024091923 | 1089786295 | 2113878218 | 48.45 | 51.55 |
| Imagine Dragons | 909378524 | 1185831093 | 2095209617 | 43.40 | 56.60 |
| The Weeknd | 704603315 | 1303197338 | 2007800653 | 35.09 | 64.91 |
| Katy Perry | 1312063208 | 660732870 | 1972796077 | 66.51 | 33.49 |
| Calvin Harris | 975847614 | 954854185 | 1930701799 | 50.54 | 49.46 |
| Charlie Puth | 1216759419 | 704118665 | 1920878084 | 63.34 | 36.66 |
| Maroon 5 | 831125233 | 1072250762 | 1903375995 | 43.67 | 56.33 |
| Eminem | 911932371 | 974858773 | 1886791144 | 48.33 | 51.67 |
| Shawn Mendes | 735104434 | 1011131373 | 1746235807 | 42.10 | 57.90 |
| DJ Snake | 918026836 | 773946280 | 1691973115 | 54.26 | 45.74 |
| Khalid | 539824628 | 1138683992 | 1678508619 | 32.16 | 67.84 |
| Luis Fonsi | 1162811303 | 505432620 | 1668243923 | 69.70 | 30.30 |
| Rihanna | 806702671 | 857266696 | 1663969367 | 48.48 | 51.52 |
| Daddy Yankee | 1087192684 | 513546248 | 1600738932 | 67.92 | 32.08 |
| The Chainsmokers | 544222389 | 1039670126 | 1583892515 | 34.36 | 65.64 |
| Fuente: kaggle.com | |||||
Tablas con libraría DataTable
- Conoce más información sobre la libreria en su web oficial
datatable(Spotify_youtube_reducido, class = 'cell-border stripe')
Visualización de datos
Gráficos con ggplot2
Otra forma de agregar imágenes
ggplot(Spotify_youtube_reducido %>% head(10), aes(x = Artist, y = reorder(porcent_Y, desc(Artist)))) +
geom_col(fill = "pink", alpha = 1) +
theme_bw() +
coord_flip() +
labs(
title = "Porcentaje de views promedio en youtube ",
subtitle = "Primeros 10 artistas",
caption = "fuente: kaggle",
x = "Artista",
y = "Porcentaje") +
geom_text(aes(x = Artist, label = porcent_Y),
hjust = 1)
Elaboro gráfico interactivo con ggplotly
En un formato HTML puedo realizar lectura de la información interactiva brindada por la libreria
Con la función ggplotly se pueden modificar gráficos estáticos de ggplot2 en gráficos dinámicos como el siguiente
ggplotly(
ggplot(Spotify_Youtube_reducido2,
aes(x = Artist, y =Porcentaje, fill = Plataforma)) +
geom_col(alpha = 0.5)+
coord_flip() +
theme_bw())
Otro estilo de gráfico facetado con facet_wrap
ggplot(Spotify_Youtube_reducido2 %>% head(10),
aes(x = Plataforma, y =Porcentaje, fill = Plataforma)) +
geom_col()+
facet_wrap(~Artist, ncol =2) +
coord_flip()
Gráficos con la librería de Plotly
Ejemplo de armado de columnas para combiar graficos y textos o imagenes y texto.
Plotly’s R graphing library makes interactive, publication-quality graphs. Examples of how to make line plots, scatter plots, area charts, bar charts, error bars, box plots, histograms, heatmaps, subplots, multiple-axes, and 3D (WebGL based) charts.
Ejemplo tomado de la web de plotly.com para R
Fuentes bibliográficas
- Diferencias entre los archivos R markdown y notebook https://argoshare.is.ed.ac.uk/healthyr_book/what-is-the-difference-between-a-notebook-and-an-r-markdown-file.html
- RMD formats https://bookdown.org/yihui/rmarkdown/rmdformats.html
- Multicolumna https://bookdown.org/yihui/rmarkdown-cookbook/multi-column.html
Librerías: