---
title: "Clima e Saúde Perinatal"
author: "Climaterna"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: scroll # scroll fill
navbar:
- { icon: "fa-question-circle", href: "https://www.climaterna.com.br/", align: right }
- { icon: "fa-linkedin", href: "http://www.linkedin.com/company/bi0s-unicamp", align: right}
- { icon: "fa-instagram", href: "https://www.instagram.com/bi0s.unicamp", align: right}
source_code: embed
---
```{r setup, include=FALSE}
rm(list = ls())
library(highcharter) # Pacotes
library(dplyr)
library(viridisLite)
library(forecast)
library(treemap)
library(arules)
library(flexdashboard)
library(geobr) ; library(sf)
library(kableExtra) ; library(DT) # library(leaflet) ;
library(ggplot2) ; library(plotly) ; library(RColorBrewer) ; library(ggspatial)
thm <- hc_theme(colors = c("#1a6ecc", "#434348", "#90ed7d"),
chart = list(backgroundColor = "transparent",style = list(fontFamily = "Source Sans Pro")), xAxis = list(gridLineWidth = 1 ) )
```
# Prematuridade
Column {data-width=600}
-----------------------------------------------------------------------
### Custom
```{r}
# TODO: why is there a linked value here?
valueBox(value = "flex_dashboard", caption = "Pacote do R", color = "white", icon = "fab fa-r-project" )
```
### Prematuros no Brasil
```{r}
escolhendo_ano<- 2023
estado_mapa<- read_state(code_state = "all", year = 2020, showProgress = F)
load(paste0("~/Downloads/dados_agregados_estado_",escolhendo_ano-1,"-",escolhendo_ano,".RData"))
dados_agregado_estado[,c("NASCIMENTOS", "A_TERMO", "PREMATURO", "TARDIO_PREMATURO", "MODERADO_PREMATURO", "MUITO_PREMATURO", "EXTREMO_PREMATURO", "BAIXO_PESO")]<- (dados_agregado_estado[,c("NASCIMENTOS", "A_TERMO", "PREMATURO", "TARDIO_PREMATURO", "MODERADO_PREMATURO", "MUITO_PREMATURO", "EXTREMO_PREMATURO", "BAIXO_PESO")])/(dados_agregado_estado[,c("NASCIMENTOS")] )*100
dados_agregado_estado<-dados_agregado_estado[dados_agregado_estado$ANO==escolhendo_ano, ]
plot_mapa_estados <- left_join(estado_mapa, dados_agregado_estado, by = c("code_state"="UF" ))
# plotly with fs object:
#plot_geo(plot_mapa_estados, color = ~ PREMATURO, stroke = I("black"),colors = "viridis", alpha = 1, hoverinfo = "none" # showlegend = FALSE
# ) |> layout(geo = list(visible = FALSE)) # disable world map
cortes_fig1 <- cut(plot_mapa_estados$PREMATURO, breaks = c(10, 10.5, 11, 11.5, 12, 12.5, 20), labels = c("[10:10.5)", "[10.5:11)", "[11:11.5)", "[11.5:12)", "[12:12.5)", "12.5+"), rigth = FALSE) ; no_axis <- theme(axis.title=element_blank(), axis.text=element_blank(), axis.ticks=element_blank())
fig_1<- ggplot() +
geom_sf(data = plot_mapa_estados, aes(fill = cortes_fig1 ), color= NA, size= .15) +
geom_sf(data = estado_mapa, fill = NA, color= "black", size = .15, lwd = 0.45) + # Estados destacados
geom_sf_text(data=estado_mapa,aes(label = abbrev_state),size = 2.5, fontface="bold") + labs(subtitle=paste0( "Ano: ", escolhendo_ano) , size=8) +
scale_fill_manual(values = brewer.pal(n = 9, name = "YlOrRd")[c(1,2,3,5,8,9)], drop = FALSE, name="Prematuros (%)") +
theme_minimal() + theme(legend.title = element_text(size = 10), plot.subtitle = element_text(hjust = 0.5) )+
annotation_north_arrow( location = "br", which_north = "true", height = unit(1, "cm"), width = unit(1, "cm"), pad_x = unit(0.1, "in"), pad_y = unit(0.1, "in"),
style = north_arrow_fancy_orienteering ) + ggspatial::annotation_scale() + no_axis ;
#ggplotly(fig_1) #%>%highlight( "plotly_hover", selected = attrs_selected(line = list(color = "black")) )
fig_1
# Leaflet
#leaflet(plot_mapa_estados) %>% addTiles() %>% addPolygons()
#leaflet() %>% addTiles() %>% setView(-93.65, 42.0285, zoom = 17) %>%
#highchart() %>%
# hc_add_series_map(estado_mapa, dados_agregado_estado, name = "Estados",
# value = "PREMATURO", joinBy = c("code_state", "UF"),dataLabels = list(enabled = TRUE, format = '{point.properties.postalcode}')) %>%
# hc_colorAxis(stops = colstops) %>%
# hc_legend(valueDecimals = 0, valueSuffix = "%") %>%
# hc_mapNavigation(enabled = TRUE) %>% hc_add_theme(thm)
```
### Prematuros Tardios no Brasil
```{r}
### Sales Forecast
#AirPassengers %>%
# forecast(level = 90) %>%
# hchart() %>%
# hc_add_theme(thm)
cortes_fig2 <- cut(plot_mapa_estados$TARDIO_PREMATURO, breaks = c(7.5,8.0, 8.5, 9, 9.5, 10, 15), labels = c("[7.5:8)", "[8:8.5)", "[8.5:9)", "[9:9.5)", "[9.5:10)", "10+"), rigth = FALSE) ; no_axis <- theme(axis.title=element_blank(), axis.text=element_blank(), axis.ticks=element_blank())
fig_2<- ggplot() +
geom_sf(data = plot_mapa_estados, aes(fill = cortes_fig2 ), color= NA, size= .15) +
geom_sf(data = estado_mapa, fill = NA, color= "black", size = .15, lwd = 0.45) + # Estados destacados
geom_sf_text(data=estado_mapa,aes(label = abbrev_state),size = 2.5, fontface="bold") + labs(subtitle=paste0( "Ano: ", escolhendo_ano) , size=8) +
scale_fill_manual(values = brewer.pal(n = 9, name = "YlOrRd")[c(1,2,3,5,8,9)], drop = FALSE, name="Prematuros \n Tardios (%)") +
theme_minimal() + theme(legend.title = element_text(size = 10), plot.subtitle = element_text(hjust = 0.5) )+
annotation_north_arrow( location = "br", which_north = "true", height = unit(1, "cm"), width = unit(1, "cm"), pad_x = unit(0.1, "in"), pad_y = unit(0.1, "in"),
style = north_arrow_fancy_orienteering ) + ggspatial::annotation_scale() + no_axis ;
#ggplotly(fig_1) #%>%highlight( "plotly_hover", selected = attrs_selected(line = list(color = "black")) )
fig_2
```
### Exemplo Iteratividade - Estados
```{r}
#hcmap(map = "countries/br/br-all", showInLegend= FALSE) %>%
# hc_title(text = "Brasil")
teste_mapa<- left_join(dados_agregado_estado, st_drop_geometry(estado_mapa), by = c("UF" = "code_state" ))
#names(teste_mapa)
# Carte
hcmap("countries/br/br-all", data = teste_mapa, value = "PREMATURO", joinBy = c("hc-a2", "abbrev_state"), name = "Prematuros",
dataLabels = list(enabled = TRUE, format = '{point.abbrev_state}'), borderColor = "#FAFAFA", borderWidth = 0.1,
tooltip = list(valueDecimals = 2, valuePrefix = "", valueSuffix = " %")) %>%
#hc_add_series(data = villes, type = "mapbubble", name = "Villes", maxSize = '10%') %>%
# Escala no mapa
#hc_colorAxis(dataClasses = color_classes( c(10,11,12,13,14,20) )) %>% hc_legend(valueDecimals = 0, valueSuffix = "%") %>%
hc_mapNavigation(enabled = TRUE) %>%
hc_title(text = "Ano: 2023")
```
```{r}
### Exemplo Iteratividade - Estado com Escala
#mapdata <- get_data_from_map(download_map_data("countries/br/br-all"))
#data_fake <- mapdata %>%
# select(code = `hc-a2`) %>%
# mutate(value = sample(100:115,27,TRUE))#adiciona novas variáveis e preserva as existentes
#glimpse(data_fake)
#hcmap("countries/br/br-all", data = data_fake, value = "value",
# joinBy = c("hc-a2", "code"), name= "Exemplo",
# dataLabels = list(enabled = TRUE, format = '{point.code}'),
# tooltip = list(valueDecimals = 2, valuePrefix = "R$")) %>%
# hc_title(text = "Brasil") %>%
# hc_colorAxis(dataClasses = color_classes(c(seq(100, 115, by = 5)))) %>%
# hc_legend(layout = "vertical", align = "right", valueDecimals = 2)
#highchart() %>%
# hc_add_series_map(estado_mapa, dados_agregado_estado, name = "Estados",
# value = "PREMATURO", joinBy = c("code_state", "UF"),dataLabels = list(enabled = TRUE, format = '{point.properties.postalcode}')) %>%
# # hc_colorAxis(stops = colstops) %>%
# hc_legend(valueDecimals = 0, valueSuffix = "%") %>%
# hc_mapNavigation(enabled = TRUE) %>% hc_add_theme(thm)
### Exemplo Iteratividade - Cidade
```
Column {.tabset data-width=400}
-----------------------------------------------------------------------
### Prematuridade
```{r}
#dados_agregado_estado$PREMATURO%>%
# forecast(level = 0) %>% hchart() %>% hc_add_theme(thm)
#hchart(dados_agregado_estado, "line", hcaes(x = Nome_UF, y = PREMATURO ))
plot_ly(data = dados_agregado_estado, x = ~Nome_UF, y = ~PREMATURO, type = 'scatter', mode = 'lines+markers') %>% # , color = ~Species
layout(title = paste0("Prematuros - ",escolhendo_ano), plot_bgcolor = "white", xaxis = list(title = ''), yaxis = list(title = 'Prematuros (%)') ) # , legend = list(title=list(text='<b> Species of Iris </b>'))
```
### Tabela Dados
```{r, fig.keep='none'}
dados_agregado_estado_arredondado<- dados_agregado_estado
dados_agregado_estado_arredondado<- dados_agregado_estado %>% mutate_if(is.numeric, ~round(., 2))
datatable(dados_agregado_estado_arredondado[,!(names(dados_agregado_estado_arredondado) %in% c("NASCIMENTOS")) ], options = list(pageLength = 30), rownames = F )
#names(dados_agregado_estado_arredondado)
#kable(dados_agregado_estado[,c("Nome_UF", "PREMATURO")], digits = 2, caption = "Prematuridade 2023", row.names = F, align = "l" ) %>% scroll_box(width = "100%", height = "800px") #scroll_box(width = "100%", height = "800px")
#library(leaflet)
#library(quakes)
#devtools::install_github("RussellPolitzky/quake")
#eqs <- highlight_key(quakes)
#stations <- filter_slider("station", "Number of Stations", eqs, ~stations)
#p <- plot_ly(eqs, x = ~depth, y = ~mag) %>% add_markers(alpha = 0.5) %>% highlight("plotly_selected")
#map <- leaflet(eqs) %>% addTiles() %>% addCircles()
#bscols(widths = c(6, 6, 3), p, map, stations)
```
### 2012 - 2023
```{r}
#load(paste0("~/Downloads/dados_agregados_estado_",escolhendo_ano-1,"-",escolhendo_ano,".RData"))
#dados_agregado_estado[,c("NASCIMENTOS", "A_TERMO", "PREMATURO", "TARDIO_PREMATURO", "MODERADO_PREMATURO", "MUITO_PREMATURO", "EXTREMO_PREMATURO", "BAIXO_PESO")]<- (dados_agregado_estado[,c("NASCIMENTOS", "A_TERMO", "PREMATURO", "TARDIO_PREMATURO", "MODERADO_PREMATURO", "MUITO_PREMATURO", "EXTREMO_PREMATURO", "BAIXO_PESO")])/(dados_agregado_estado[,c("NASCIMENTOS")] )*100
#plot_ly(data = dados_agregado_estado, type = "scatter", mode = "lines+markers",
#x = ~Nome_UF, y = ~PREMATURO, frame = ~ANO, showlegend = FALSE) %>% # , color = ~Species
# layout(title = paste0("Prematuros: 2012-2023"), plot_bgcolor = "white", xaxis = list(title = ''), yaxis = list(title = 'Prematuros (%)') ) %>%
# animation_opts(frame = 1000, easing = "elastic", redraw = FALSE)
load(paste0("~/Downloads/dados_agregados_estado_2012-2023.RData"))
dados_agregado_estado_sem_geo<- sf::st_drop_geometry(dados_agregado_estado)
plot_ly(data = dados_agregado_estado_sem_geo, type = "bar",
x = ~Nome_UF, y = ~PREMATURO, frame = ~ANO, showlegend = FALSE) %>% # , color = ~Species
layout(title = paste0("Prematuros: 2012-2023"), plot_bgcolor = "white", xaxis = list(title = ''), yaxis = list(title = 'Prematuros (%)') ) %>%
animation_opts(frame = 1000, easing = "elastic", redraw = FALSE)
```
# Baixo Peso
# Mortalidade Infantil {data-icon="fa-heartbeat"}
# Clima {data-icon="fa-sun"}
# Sobre {data-icon="glyphicon-time"}
```{r}
#https://www.w3schools.com/icons/fontawesome5_icons_energy.asp
```