library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(tidyr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# Create a scatter plot using plotly with different colors for nivel_numero
# Count the number of unique uid_servidor by orgao_superior and nivel_numero
cargos <- read.csv("cargos_DAS_assignment.csv", sep = ";")
cargos_2021 <- cargos |>
  filter(ano == 2021)


uid_count <- cargos_2021 %>%
  group_by(orgao_superior, nivel_numero) %>%
  #summarise(num_servidores = n_distinct(uid_servidor))
  summarise(num_servidores = n_distinct(uid_servidor))
## `summarise()` has grouped output by 'orgao_superior'. You can override using
## the `.groups` argument.
fig <- plot_ly(uid_count, x = ~orgao_superior, y = ~num_servidores, 
               color = ~as.factor(nivel_numero), 
               size = ~num_servidores, 
               type = 'scatter', 
               mode = 'markers') %>%
 add_annotations(text = "Political Appointees in Brazilian Ministries (2021)",
                  x = 0.5, y = 1.15, 
                  xref = "paper", yref = "paper",
                  showarrow = FALSE, 
                  font = list(size = 20)) %>%
  config(displayModeBar = TRUE)

# Axis titles using `xaxis` and `yaxis` options
fig$x$layout$xaxis$title <- "Ministry"
fig$x$layout$yaxis$title <- "Number of appointees" 

# To display the plot
fig
## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.