Sheet A. tooltip_chart()

Sheet B. tooltip_table()

Sheet C. tooltip_chart() & tooltip_table()

---
title: "Data Visualizations: tooltip_chart() and tooltip_table()"
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(tidyverse)
library(highcharter)
library(readxl)

options(scipen=10000)

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


peru_df <- peru_df %>%
  filter(Year %in% c(2001 : 2016)) %>%
  filter(id != "PE") %>%
  mutate(Geo = case_when(
    Geo == "Costa"  ~ "Coast",
    Geo == "Sierra" ~ "Highlands",
    Geo == "Selva" ~ "Forest"
  ))


peru_df_A <- peru_df %>%
  arrange(desc(Year)) %>%
  distinct(Name, .keep_all = TRUE)


peru_df_B<- peru_df %>%
  nest(-Name) %>%
  mutate(data = map(data, mutate_mapping, hcaes(x= Year, y = PEAO_AGR_PER), drop = TRUE),
         data = map(data, list_parse)) %>%
  rename(ttdata = data)


peru_df_plot<-left_join(peru_df_A, peru_df_B)


```



Sheet A. `tooltip_chart()`
====================================================================================

```{r}

hchart( peru_df_plot, "scatter", hcaes(VABpc_07, PEAO_AGR_PER, name = Region, size = Poblacion, group = Geo)) %>%
  hc_xAxis(type = "logarithmic", title = list(text = "GVA per capita (log scale)")) %>% 
  hc_yAxis(title = list(text = "% PEAO in agriculture")) %>%
  hc_colors(c("#2980b9", "#2ecc71", "#d35400" )) %>%
  hc_tooltip(
    useHTML = TRUE,
    headerFormat = "{point.key}",
    pointFormatter = tooltip_chart(accesor = "ttdata")
  ) %>%
  hc_title (text = "GVA per capita VS. % PEAO in Agriculture, 2016") %>%
  hc_subtitle(text = "PEAO: Actively occupied population. GVA per capita in constant 2007 PEN") %>%
  hc_credits(enabled = TRUE, text = "Source: INEI") %>%
  hc_add_theme(hc_theme_darkunica())

```


Sheet B. `tooltip_table()`
==========================================================================================

```{r}

peru_df_plot_b <- peru_df_plot %>%
  mutate( "Population" = Poblacion,
         "GVA pc" = VABpc_07,
         "% AGR" = PEAO_AGR_PER)

vars = c("Region", "Population", "GVA pc", "% AGR")
tt <- tooltip_table(x = vars, y = sprintf("{point.%s}", vars))


peru_df_plot_b$Population <- format(peru_df_plot_b$Population, big.mark = ",")
peru_df_plot_b$'GVA pc' <- format(peru_df_plot_b$'GVA pc', big.mark = ",")

hchart( peru_df_plot_b, "scatter", hcaes(VABpc_07, PEAO_AGR_PER, name = Region, size = Poblacion, group = Geo)) %>%
  hc_xAxis(type = "logarithmic", title = list(text = "GVA per capita (log scale)")) %>% 
  hc_yAxis(title = list(text = "% PEAO in agriculture")) %>%
  hc_colors(c("#2980b9", "#2ecc71", "#d35400" )) %>%
  hc_tooltip(useHTML = TRUE, pointFormat = tt) %>%
  hc_title (text = "GVA per capita VS. % PEAO in Agriculture, 2016") %>%
  hc_subtitle(text = "PEAO: Actively occupied population. GVA per capita in constant 2007 PEN") %>%
  hc_credits(enabled = TRUE, text = "Source: INEI") %>%
  hc_add_theme(hc_theme_darkunica())


```


Sheet C. `tooltip_chart()` & `tooltip_table()`
================================================================================

```{r}

# To create this plot I have followed jbkunst indications


pfmc_P <- tooltip_chart(
  accesor = "ttdata",
  hc_opts = list(
    xAxis = list(type = "category")
  )
  )

vars_B <- c("Region", "Population", "PEAO")
tt_P <- tooltip_table(x = vars_B, y = sprintf("{point.%s}", vars_B))


ttt_P <- tt_P %>% 
  str_remove_all("point\\.") %>% 
  str_remove_all("\\s+")


peru_df_plot_b <- peru_df_plot %>%
  mutate( "Population" = Poblacion,
          "GVA pc" = VABpc_07,
          "% AGR" = PEAO_AGR_PER)

peru_df_plot_b$Population <- format(peru_df_plot_b$Population, big.mark = ",", digits =  0)
peru_df_plot_b$PEAO <- format(peru_df_plot_b$PEAO, big.mark = ",", digits = 0)

peru_df_plot_b <- peru_df_plot_b %>% 
  mutate(
    tttext_P = str_glue_data(peru_df_plot_b, ttt_P)
  )


pfmc_P <- tooltip_chart(
  width = 400,
  height = 400,
  accesor = "ttdata",
  hc_opts = list(
    title = list(text = "point.tttext_P", useHTML = TRUE),
    xAxis = list(type = "category")
  )
)



hchart( peru_df_plot_b, "scatter", hcaes(VABpc_07, PEAO_AGR_PER, name = Region, size = Poblacion, group = Geo)) %>%
  hc_xAxis(type = "logarithmic", title = list(text = "GVA per capita (log scale)")) %>% 
  hc_yAxis(title = list(text = "% PEAO in agriculture")) %>%
  hc_colors(c("#2980b9", "#2ecc71", "#d35400" )) %>%
  hc_tooltip(useHTML = TRUE, pointFormatter = pfmc_P) %>%
  hc_title (text = "GVA per capita VS. % PEAO in Agriculture, 2016") %>%
  hc_subtitle(text = "PEAO: Actively occupied population. GVA per capita in constant 2007 PEN") %>%
  hc_credits(enabled = TRUE, text = "Source: INEI") %>%
  hc_add_theme(hc_theme_darkunica())



```