Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. ################################################################################
  2. #
  3. # Marc Belzunces (Twitter: @marcbeldata)
  4. # marcbeldata.github.io 2017-07-20
  5. #
  6. # Playing with ggjoy.
  7. # Evolution of vote in Catalan regional elections (1980-2015)
  8. #
  9. ################################################################################
  10.  
  11. library(tidyverse)
  12. library(forcats)
  13. library(wpp2015)
  14. library(ggjoy)
  15. library(viridis)
  16. library(grid)
  17. library(gridExtra)
  18. library(RColorBrewer)
  19.  
  20. #Data obtained and processed from Idescat (not public)
  21. df <- read_csv("Catalan Parliament 1980-2015.csv")
  22. glimpse(df)
  23. head(df)
  24.  
  25. #Vote for the main parties
  26. #CiU
  27. p1 <- df %>% select(Municipality, INE, Year, CiU_percent) %>%
  28. filter(!is.na(CiU_percent)) %>%
  29. mutate(
  30. Municipality = as.factor(Municipality),
  31. Year = as.factor(Year)
  32. ) %>%
  33. ggplot(aes(x = CiU_percent, y = Year %>% fct_rev())) +
  34. geom_joy(aes(fill = Year))+
  35. scale_fill_viridis(discrete = T, option = "D", direction = -1,
  36. begin = .1, end = .9) +
  37. labs(x = "Percent Vote",
  38. y = "Election's Year",
  39. title = "CiU's vote in Catalan elections",
  40. subtitle = "Analysis unit: municipalities",
  41. caption = "Marc Belzunces (@marcbeldata)") +
  42. theme_minimal(base_size = 15) +
  43. theme(legend.position = "none")
  44.  
  45. #ERC
  46. p2 <- df %>% select(Municipality, INE, Year, ERC_percent) %>%
  47. filter(!is.na(ERC_percent)) %>%
  48. mutate(
  49. Municipality = as.factor(Municipality),
  50. Year = as.factor(Year)
  51. ) %>%
  52. ggplot(aes(x = ERC_percent, y = Year %>% fct_rev())) +
  53. geom_joy(aes(fill = Year))+
  54. scale_fill_viridis(discrete = T, option = "A", direction = -1,
  55. begin = .1, end = .9) +
  56. labs(x = "Percent Vote",
  57. y = "Election's Year",
  58. title = "ERC's vote in Catalan elections",
  59. subtitle = "Analysis unit: municipalities",
  60. caption = "Marc Belzunces (@marcbeldata)") +
  61. theme_minimal(base_size = 15) +
  62. theme(legend.position = "none")
  63.  
  64.  
  65. #PSC
  66. p3 <- df %>% select(Municipality, INE, Year, PSC_percent) %>%
  67. filter(!is.na(PSC_percent)) %>%
  68. mutate(
  69. Municipality = as.factor(Municipality),
  70. Year = as.factor(Year)
  71. ) %>%
  72. ggplot(aes(x = PSC_percent, y = Year %>% fct_rev())) +
  73. geom_joy(aes(fill = Year))+
  74. scale_fill_viridis(discrete = T, option = "B", direction = -1,
  75. begin = .1, end = .9) +
  76. labs(x = "Percent Vote",
  77. y = "Election's Year",
  78. title = "PSC's vote in Catalan elections",
  79. subtitle = "Analysis unit: municipalities",
  80. caption = "Marc Belzunces (@marcbeldata)") +
  81. theme_minimal(base_size = 15) +
  82. theme(legend.position = "none")
  83.  
  84. #PP
  85. p4 <- df %>% select(Municipality, INE, Year, PP_percent) %>%
  86. filter(!is.na(PP_percent)) %>%
  87. mutate(
  88. Municipality = as.factor(Municipality),
  89. Year = as.factor(Year)
  90. ) %>%
  91. ggplot(aes(x = PP_percent, y = Year %>% fct_rev())) +
  92. geom_joy(aes(fill = Year))+
  93. scale_fill_viridis(discrete = T, option = "C", direction = -1,
  94. begin = .1, end = .9) +
  95. labs(x = "Percent Vote",
  96. y = "Election's Year",
  97. title = "PP's vote in Catalan elections",
  98. subtitle = "Analysis unit: municipalities",
  99. caption = "Marc Belzunces (@marcbeldata)") +
  100. theme_minimal(base_size = 15) +
  101. theme(legend.position = "none")
  102.  
  103. #Comuns
  104. p5 <- df %>% select(Municipality, INE, Year, CSQP_percent) %>%
  105. filter(!is.na(CSQP_percent)) %>%
  106. mutate(
  107. Municipality = as.factor(Municipality),
  108. Year = as.factor(Year)
  109. ) %>%
  110. ggplot(aes(x = CSQP_percent, y = Year %>% fct_rev())) +
  111. geom_joy(aes(fill = Year))+
  112. scale_fill_viridis(discrete = T, option = "D", direction = -1,
  113. begin = .1, end = .9) +
  114. labs(x = "Percent Vote",
  115. y = "Election's Year",
  116. title = "ICV/CSQP's vote in Catalan elections",
  117. subtitle = "Analysis unit: municipalities",
  118. caption = "Marc Belzunces (@marcbeldata)") +
  119. theme_minimal(base_size = 15) +
  120. theme(legend.position = "none")
  121.  
  122. #Cs
  123. p6 <- df %>% select(Municipality, INE, Year, Cs_percent) %>%
  124. filter(!is.na(Cs_percent)) %>%
  125. mutate(
  126. Municipality = as.factor(Municipality),
  127. Year = as.factor(Year)
  128. ) %>%
  129. ggplot(aes(x = Cs_percent, y = Year %>% fct_rev())) +
  130. geom_joy(aes(fill = Year))+
  131. scale_fill_viridis(discrete = T, option = "A", direction = -1,
  132. begin = .1, end = .9) +
  133. labs(x = "Percent Vote",
  134. y = "Election's Year",
  135. title = "Cs's vote in Catalan elections",
  136. subtitle = "Analysis unit: municipalities",
  137. caption = "Marc Belzunces (@marcbeldata)") +
  138. theme_minimal(base_size = 15) +
  139. theme(legend.position = "none")
  140.  
  141. grid.arrange(p1, p2, p3, p4, p5, p6, ncol = 2)
  142.  
  143.  
  144. #Vote in terms of independence of Catalonia/Unions to Spain
  145. #Generating a new 11 color palette (thanks to Ilya Kashnitsky tip)
  146. green <- brewer.pal(11,"BrBG")[7:11]
  147. int <- colorRampPalette(green)
  148. eleven <- int(11)
  149.  
  150. brown <- brewer.pal(11,"BrBG")[5:1]
  151. interpol <- colorRampPalette(brown)
  152. eleven2 <- interpol(11)
  153.  
  154. #Indy
  155. p7 <- df %>% select(Municipality, INE, Year, Indy_percent) %>%
  156. filter(!is.na(Indy_percent)) %>%
  157. mutate(
  158. Municipality = as.factor(Municipality),
  159. Year = as.factor(Year)
  160. ) %>%
  161. ggplot(aes(x = Indy_percent, y = Year %>% fct_rev())) +
  162. geom_joy(aes(fill = Year))+
  163. scale_fill_manual(values = c("#C7EAE5", "#AADED6", "#8ED2C8", "#71C2B6", "#52ACA3",
  164. "#35978F", "#20837B", "#0B6F67", "#005D54", "#004C42",
  165. "#003C30")) + #eleven palette colors
  166. labs(x = "Percent Vote",
  167. y = "Election's Year",
  168. title = "Indy parties' vote in Catalan elections",
  169. subtitle = "Analysis unit: municipalities",
  170. caption = "Marc Belzunces (@marcbeldata)") +
  171. theme_minimal(base_size = 15) +
  172. theme(legend.position = "none")
  173.  
  174. #Unio
  175. p8 <- df %>% select(Municipality, INE, Year, Unio_percent) %>%
  176. filter(!is.na(Unio_percent)) %>%
  177. mutate(
  178. Municipality = as.factor(Municipality),
  179. Year = as.factor(Year)
  180. ) %>%
  181. ggplot(aes(x = Unio_percent, y = Year %>% fct_rev())) +
  182. geom_joy(aes(fill = Year))+
  183. scale_fill_manual(values = c("#F6E8C3", "#ECD8A6", "#E3C98B", "#D8B46C",
  184. "#CB9B4C", "#BF812D", "#AA6D1E", "#965A10",
  185. "#804A09", "#6A3D07", "#543005")) + #eleven2 palette colors
  186. labs(x = "Percent Vote",
  187. y = "Election's Year",
  188. title = "Unionist parties' vote in Catalan elections",
  189. subtitle = "Analysis unit: municipalities",
  190. caption = "Marc Belzunces (@marcbeldata)") +
  191. theme_minimal(base_size = 15) +
  192. theme(legend.position = "none")
  193.  
  194. grid.arrange(p7,p8, ncol = 2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement