library(extrafont)
## Registering fonts with R
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.2     v dplyr   1.0.7
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
# https://wjschne.github.io/posts/bar-chart-labels-on-smooth-paths-in-ggplot2/

myfont <- "Roboto Condensed"

d <- tibble(Group = LETTERS[1:26],
            mu = rnorm(26, 0, .5),
            sigma = 1) %>% 
  mutate(x = pmap(list(mean = mu, sd = sigma), rnorm, n = 500)) %>% 
  unnest(x) %>% 
  mutate(Agree = cut_number(x, 6) %>% 
           factor(labels = c("Discordo\nTotalmente", "Discordo", "Discordo\num Pouco", 
                             "Concordo\num pouco", "Concordo", "Concordo\nTotalmente"))) %>% 
  group_by(Group, Agree) %>% 
  summarise(n = n(), .groups = "drop") %>% 
  group_by(Group) %>% 
  arrange(Group, Agree) %>% 
  mutate(p = n / sum(n)) %>% 
  ungroup()
d %>% 
  ggplot(mapping = aes(p, Group)) + 
  geom_col(aes(fill = fct_rev(Agree))) + 
  scale_fill_viridis_d(NULL, begin = .15, end = 0.8, direction = -1) +
  scale_x_continuous("Percentual", expand = expansion()) +
  scale_y_discrete("Grupo", expand = expansion()) + 
  coord_fixed(1 / 20.5,clip = "off") + 
  geom_text(aes(x = p, label = round(100 * p, 0)), 
            color = "white",
            family = myfont,
            position = position_stack(vjust = .5))
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database

d %>%  
  mutate(Group = fct_reorder(Group, .x = as.numeric(Agree) * p, .fun = mean)) %>% 
  ggplot(mapping = aes(p, Group)) + 
  geom_col(aes(fill = fct_rev(Agree))) + 
  scale_fill_viridis_d(NULL, begin = .15, end = 0.8, direction = -1) +
  scale_x_continuous("Percentual", expand = expansion()) +
  scale_y_discrete("Grupo", expand = expansion()) + 
  coord_fixed(1 / 20.5,clip = "off") + 
  geom_text(aes(x = p, label = round(100 * p, 0)), 
            color = "white",
            family = myfont,
            position = position_stack(vjust = .5))
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database

d <- tibble(Group = LETTERS[1:26],
            mu = rnorm(26, 0, .5),
            sigma = 1) %>% 
  mutate(x = pmap(list(mean = mu, sd = sigma), rnorm, n = 500)) %>% 
  unnest(x) %>% 
  mutate(Agree = cut_number(x, 6) %>% 
           factor(labels = c("Discordo\nTotalmente", "Discordo", "Discordo\num Pouco", 
                             "Concordo\num pouco", "Concordo", "Concordo\nTotalmente"))) %>% 
  mutate(Group = fct_reorder(Group, as.numeric(Agree), .fun = mean)) %>% 
  group_by(Group, Agree) %>% 
  summarise(n = n(), .groups = "drop") %>% 
  mutate(Group_position = as.numeric(Group)) %>% 
  group_by(Group) %>% 
  arrange(Group, Agree) %>% 
  mutate(p = n / sum(n),
         cp = cumsum(p),
         xpos = cp - p / 2) %>% 
  group_by(Agree) %>% 
  nest() %>% 
  mutate(fit = map(data, loess, formula = "xpos ~ Group_position", span = 0.45),
         xhat = map(fit, predict)) %>% 
  select(-fit) %>% 
  unnest(c(data, xhat)) %>% 
  mutate(Group = factor(Group, labels = rev(LETTERS[1:26])))
d
d %>% 
  ggplot(mapping = aes(p, Group)) + 
  geom_col(aes(fill = fct_rev(Agree))) + 
  geom_text(aes(x = xhat, label = ifelse(p > .01, round(100 * p, 0), "")), 
            color = "white",
            family = myfont) +
  scale_fill_viridis_d(begin = .15, end = 0.8, direction = -1) +
  scale_x_continuous("Percentual", expand = expansion()) +
  scale_y_discrete("Grupo", expand = expansion()) + 
  coord_fixed(1 / 20.5,clip = "off") + 
  theme_minimal(base_size = 13, base_family = myfont) + 
  theme(legend.position = "top", legend.box.spacing = unit(0.5, "mm"),
        legend.text = element_text(color = "white", 
                                   size = 13,
                                   margin = margin(b = -40),
                                   vjust = 0.5), 
        legend.spacing.x = unit(0,"mm"),
        axis.text.y = element_text(hjust = 0.5)) + 
  guides(fill = guide_legend(title = NULL, nrow = 1, 
                             reverse = T, 
                             label.position = "top", 
                             keyheight = unit(15, "mm"),
                             keywidth = unit(22.5, "mm"))) 
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family not
## found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database

## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database
## Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font
## family not found in Windows font database