---
title: "Día 2 #30díasdegráficos (líneas). Estructura sectorial del Perú. 1950-2018 (ESP)"
author: "Rubén F. Bustillo"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    source_code: embed
    vertical_layout: fill
    theme: spacelab
---



```{r setup, include=FALSE}


# PACKAGES / LIBRARIES:

library(flexdashboard)
library(plotly)
library(readxl)

Peru_pib_sectores <- read_excel("C:/Users/Usuario/Desktop/r_que_r/r_que_r/content/datasets/Peru_pib_sectores.xlsx")


Peru_pib_sectores$Agricult. <- format(Peru_pib_sectores$Agricult., digits = 3)
Peru_pib_sectores$Pesca <- format(Peru_pib_sectores$Pesca, digits = 3)
Peru_pib_sectores$Extrac. <- format(Peru_pib_sectores$Extrac., digits = 3)
Peru_pib_sectores$Manufact. <- format(Peru_pib_sectores$Manufact., digits = 3)
Peru_pib_sectores$Electr. <- format(Peru_pib_sectores$Electr., digits = 3)
Peru_pib_sectores$Construcc. <- format(Peru_pib_sectores$Construcc., digits = 3)
Peru_pib_sectores$Comerc. <- format(Peru_pib_sectores$Comerc., digits = 3)
Peru_pib_sectores$Adm. <- format(Peru_pib_sectores$Adm., digits = 3)



```



```{r}

margin <- list(autoexpand = TRUE,
               l = 100,
               r = 200,
               t = 110,
               b = 100)

fig <- plot_ly(Peru_pib_sectores)
fig <- fig %>%
  add_trace(x = ~year, y= ~PIB, type = 'bar', name = 'PIB Total',
            marker = list(color = 'dimgrey'),
            hoverinfo = "text",
            text = ~paste(year,',', PIB, 'mills'))
fig <- fig %>%
  add_trace(x = ~year, y = ~Agricult., type = 'scatter', mode = 'lines', name = 'Agricultura', yaxis = 'y2',
            line = list(color = 'limegreen', width = 4),
            hovermode = "compare",
            text = ~paste(Agricult., '%'))
fig <- fig %>%
  add_trace(x = ~year, y = ~Pesca, type = 'scatter', mode = 'lines', name = 'Pesca', yaxis = 'y2',
            line = list(color = 'lightblue', width = 4),
            hovermode = "compare",
            text = ~paste(Pesca, '%'))
fig <- fig %>%
  add_trace(x = ~year, y = ~Extrac., type = 'scatter', mode = 'lines', name = 'Extracción de Petroleo y Minerales', yaxis = 'y2',
            line = list(color = 'yellow', width = 4),
            hovermode = "compare",
            text = ~paste(Extrac., '%'))
fig <- fig %>%
  add_trace(x = ~year, y = ~Manufact., type = 'scatter', mode = 'lines', name = 'Manufactura', yaxis = 'y2',
            line = list(color = 'red', width = 4),
            hovermode = "compare",
            text = ~paste(Manufact., '%'))
fig <- fig %>%
  add_trace(x = ~year, y = ~Electr., type = 'scatter', mode = 'lines', name = 'Electricidad y Agua', yaxis = 'y2',
            line = list(color = 'royalblue', width = 4),
            hovermode = "compare",
            text = ~paste(Electr., '%'))
fig <- fig %>%
  add_trace(x = ~year, y = ~Construcc., type = 'scatter', mode = 'lines', name = 'Construcción', yaxis = 'y2',
            line = list(color = 'brown', width = 4),
            hovermode = "compare",
            text = ~paste(Construcc., '%'))
fig <- fig %>%
  add_trace(x = ~year, y = ~Comerc., type = 'scatter', mode = 'lines', name = 'Comercio', yaxis = 'y2',
            line = list(color = 'orange', width = 4),
            hovermode = "compare",
            text = ~paste(Comerc., '%'))
fig <- fig %>%
  add_trace(x = ~year, y = ~Adm., type = 'scatter', mode = 'lines', name = 'Adm. Pública y Defensa', yaxis = 'y2',
            line = list(color = 'magenta', width = 4),
            hovermode = "compare",
            text = ~paste(Adm., '%'))
fig <- fig %>%
  add_trace(x = ~year, y = ~OtrosSer., type = 'scatter', mode = 'lines', name = 'Otros Servicios', yaxis = 'y2',
            line = list(color = 'navyblue', width = 4),
           hovermode = "compare",
            text = ~paste(OtrosSer., '%'))

fig <- fig %>% 
  layout(title = 'Participación de los grandes sectores económicos (Nivel 9) \n sobre el PIB total (Perú. 1950-2018) \n (Valores a precios constantes de 2007)',
                      xaxis = list(title = ""),
                      yaxis = list(side = 'left', title = 'Producto Bruto Interno (Millones de soles)', showgrid = TRUE, zeroline = FALSE),
                      yaxis2 = list(side = 'right', overlaying = "y", title = '(%)', showgrid = FALSE, zeroline = FALSE),
         margin = margin) %>%
  layout(annotations = list(x = 1 , y = -0.05, text = "Fuente: INEI",
                 showarrow = F, xref='paper', yref='paper', 
                 xanchor='right', yanchor='auto', xshift=0, yshift=0,
                 font=list(size=10, color="darkgrey")))


fig <- fig %>%
  layout(plot_bgcolor = 'black',
         paper_bgcolor = 'black',
         font = list(color = 'white')) 



fig



```