-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathproporcao_cabecas_cluster.Rmd
156 lines (122 loc) · 4.33 KB
/
proporcao_cabecas_cluster.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
---
title: "proporcao_cabecas_cluster"
author: "Rodolfo Viana"
date: "17-12-2015"
output: html_document
---
Mostra a proporção de deputados cabeças por clust
```{r}
#Bibliotecas necessárias
library(ggplot2)
library(plyr)
library(dplyr)
library(reshape2)
require(cluster)
require(ade4)
require(scales)
require(FactoMineR)
require(tidyr)
source("R/camara-lib.R")
```
```{r}
votos_por_deputado <- recuperar_votos_por_deputado(arquivo.votos = "dados/votacoes.csv", corrigir.migracoes = TRUE)
mca <- MCA(votos_por_deputado,
ncp = 6, # Default is 5
graph = FALSE,
quali.sup = c(1:4),
na.method = "Average") # NA or Average
mca1_obs_df <- data.frame(mca$ind$coord,
nome = votos_por_deputado$nome,
partido = votos_por_deputado$partido,
uf = votos_por_deputado$uf,
id_dep = votos_por_deputado$id_dep)
mca1_obs_df$id_dep <- as.integer(as.character(mca1_obs_df$id_dep))
hcpc <- clusterizar(mca,4)
clusters <- obter_clusters(hcpc)
```
```{r}
mca1_obs_df <- cbind(mca1_obs_df, select(clusters,clust))
mca1_obs_df$clust <- as.factor(mca1_obs_df$clust)
```
```{r}
# Cabeças
cabecas <- read.table("data/cabecas.csv", header=TRUE, quote="\"")
```
```{r}
# Partidos icônicos
mca1_obs_df$destaque_partido = factor(ifelse(mca1_obs_df$partido %in%
c("pmdb", "psdb", "pt", "psol"),
as.character(mca1_obs_df$partido),
"outros"))
# Destaque dos deputados que estão na lista dos cabeças de 2015
mca1_obs_df$destaque_cabeca <- mca1_obs_df$nome %in% cabecas$Cabeca
```
Plot mostrando a proporção de deputados por clust
```{r}
p <- plotCluster(mca1_obs_df) +
geom_point(data = filter(mca1_obs_df, destaque_cabeca == TRUE),
size = 9, alpha = 0.3, colour = "Black") +
coord_equal()
png("plot/cabecas/deputados_cabeca_clust_dep.png", width = 960, height = 960)
p
dev.off()
png("plot/cabecas/deputados_cabeca_clust_dep_nome.png", width = 800, height = 600)
p + geom_text(data = filter(mca1_obs_df, destaque_cabeca == TRUE),
aes(x = Dim.1, y = Dim.2, label = paste(nome, "-", toupper(partido))),
check_overlap = TRUE,
colour = "black", alpha = 0.5, size = 3, hjust = -.15)
dev.off()
png("plot/cabecas/deputados_cabeca_clust_dep_nome_gg.png", width = 4000, height = 3000)
p + geom_text(data = filter(mca1_obs_df, destaque_cabeca == TRUE),
aes(x = Dim.1, y = Dim.2, label = paste(nome, "-", toupper(partido))),
check_overlap = TRUE,
colour = "black", alpha = 0.5, size = 9, hjust = -.15)
dev.off()
```
```{r}
levels(mca1_obs_df$clust) <- c("Governo", "Alinhados com PMDB", "Oposição de esquerda", "Oposição")
```
Número de deputados cabeças por clust
```{r}
capitas <- mca1_obs_df %>%
filter(destaque_cabeca == T) %>%
select(clust) %>%
table()
p <- ggplot(data = filter(mca1_obs_df, destaque_cabeca),
aes(x=reorder(clust, capitas[clust]), fill = clust)) +
geom_bar(width = .5) +
theme_pander() +
scale_fill_manual(values = c("#fdcdac", "#f4cae4", "#b3e2cd", "#cbd5e8")) +
labs(y='Nº de Deputados', x='') +
theme(axis.ticks = element_blank(),
axis.text = element_text(size = rel(1.5)),
legend.position="none") +
coord_flip()
p
png("plot/cabecas/deputados_n_cabeca_clust.png", width = 850, height = 500)
p
dev.off()
```
Proporção de deputados cabeças por clust
```{r}
toPlot <- mca1_obs_df %>%
group_by(clust, destaque_cabeca) %>%
summarise(count=n()) %>%
mutate(perc=count/sum(count)) %>%
ungroup() %>%
arrange(clust, -destaque_cabeca, perc)
ordem = rep(filter(toPlot, destaque_cabeca == T)$perc, each = 2)
p <- ggplot(toPlot, aes(x = reorder(clust, ordem), y = perc, fill = destaque_cabeca)) +
geom_bar(stat = "identity", width = .4) +
theme_pander() +
scale_y_continuous(labels=percent) +
labs(x = "", y = "% dos deputados que são cabeças") +
theme(legend.position="none",
axis.text = element_text(size = rel(1.5))) +
scale_fill_brewer(palette = "YlOrBr") +
coord_flip()
p
png("plot/cabecas/proprocao_cabeca_clust.png", width = 850, height = 500)
p
dev.off()
```