---
title: "Figuras"
author: "René Díaz Flores"
output:
flexdashboard::flex_dashboard:
orientation: rows
social: menu
source_code: embed
---
```{r setup, include=FALSE}
library(pacman)
p_load("data.table","ggthemes","lubridate","plyr","flexdashboard","readxl","tidyverse","plotly",
"ggExtra")
BP <- read_excel("SERIES SUBSISTEMA BANCA PRIVADA 2022_06.xlsx",
sheet = "BAL", skip = 5)%>%
select(CODIGO, CUENTA,`2020-01`:`2022-06`)
# Make some noisily increasing data
set.seed(955)
dat <- data.frame(cond = rep(c("A", "B"), each=10),
xvar = 1:20 + rnorm(20,sd=3),
yvar = 1:20 + rnorm(20,sd=3))
```
Balance General
=======================================================================
Row
-----------------------------------------------------------------------
### Evolución Cartera de Créditos
```{r}
Cartera <- BP %>%
filter(CODIGO == 14) %>%
pivot_longer(cols = `2020-01`:`2022-06`,
names_to = c("Meses"),
values_to = c("Cartera de Crédito"))%>%
mutate(Meses = seq(ymd('2020-01-01'),ymd('2022-06-01'), by = 'month'),
Meses_II = month(Meses)) %>%
filter(Meses_II %in% c(3,6,9,12))%>%
mutate(`Cartera de Crédito` = round(`Cartera de Crédito`,2),
Variacion = (`Cartera de Crédito`/lag(`Cartera de Crédito`)-1))%>%
mutate_if(is.numeric, ~ifelse(is.na(.),0,.))%>%
mutate_at(vars(`Cartera de Crédito`),list(~round(./1000000,2)))%>%
mutate(var_iii = (Variacion*340)+30)
Composicion <- Cartera %>%
select(Meses, Variacion)
Max_Min <- Cartera %>%
summarise_at(vars(`Cartera de Crédito`, Variacion),
list(Min=min, Max=max))
HG <- theme(axis.line = element_line(size=1, colour = "black"),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.text.x = element_text(colour="black", size = 12),
axis.text.y = element_text(colour="black", size = 14),
axis.title = element_text(size = 15))
af <- ggplot() +
geom_bar(data=Cartera, aes(x=Meses, y = `Cartera de Crédito`),stat = "identity",colour="gray2",
fill="cadetblue3") +
#theme_minimal() +ggExtra::removeGrid()
theme_economist()
af <- af + scale_x_date(date_labels = '%Y%-%b',
breaks = as.Date(c( '2020-03-01',
'2020-06-01',
'2020-09-01',
'2020-12-01',
'2021-03-01',
'2021-06-01',
'2021-09-01',
'2021-12-01',
'2022-03-01',
'2022-06-01')))
all<- af + geom_line(data=Cartera,mapping = aes(x = Meses, y = Variacion*340+30),
color="black", size=0.6,linetype = 1)+
geom_point(data=Cartera,mapping = aes(x = Meses, y = Variacion*340+30),
color="darkblue", size=1)+
scale_y_continuous(expand = c(0,0),
sec.axis = ggplot2::sec_axis(~(.-30)/340,
name = "Variación Porcentual",
labels = scales::label_percent()))
#all
ggplotly(all)
```
geom_density
=======================================================================
Row
-----------------------------------------------------------------------
### stat_density Example
```{r}
dfGamma = data.frame(nu75 = rgamma(100, 0.75),
nu1 = rgamma(100, 1),
nu2 = rgamma(100, 2))
dfGamma = stack(dfGamma)
p <- ggplot(dfGamma, aes(x = values)) +
stat_density(aes(group = ind, color = ind),position="identity",geom="line")
ggplotly(p)
```
### stat_density Example
```{r}
dfGamma = data.frame(nu75 = rgamma(100, 0.75),
nu1 = rgamma(100, 1),
nu2 = rgamma(100, 2))
dfGamma = stack(dfGamma)
p <- ggplot(dfGamma, aes(x = values)) +
stat_density(aes(group = ind, color = ind),position="identity",geom="line")
ggplotly(p)
```
Row
-----------------------------------------------------------------------
### geom_density and facet_wrap Together
```{r}
dd<-data.frame(matrix(rnorm(144, mean=2, sd=2),72,2),c(rep("A",24),rep("B",24),rep("C",24)))
colnames(dd) <- c("x_value", "Predicted_value", "State_CD")
dd <- data.frame(
predicted = rnorm(72, mean = 2, sd = 2),
state = rep(c("A", "B", "C"), each = 24)
)
grid <- with(dd, seq(min(predicted), max(predicted), length = 100))
normaldens <- ddply(dd, "state", function(df) {
data.frame(
predicted = grid,
density = dnorm(grid, mean(df$predicted), sd(df$predicted))
)
})
p <- ggplot(dd, aes(predicted)) +
geom_density() +
geom_line(aes(y = density), data = normaldens, colour = "red") +
facet_wrap(~ state)
ggplotly(p)
```
### Density and Scatterplot Overlay Using geom_density
```{r}
df <- data.frame(x <- rchisq(1000, 10, 10),
y <- rnorm(1000))
p <- ggplot(df, aes(x, y)) +
geom_point(alpha = 0.5) +
geom_density_2d() +
theme(panel.background = element_rect(fill = '#ffffff'))
ggplotly(p)
```