# Basic knitr options
library(knitr)
opts_chunk$set(comment = NA, 
               echo = FALSE, 
               warning = FALSE, 
               message = FALSE, 
               error = TRUE, 
               cache = FALSE,
               fig.path = 'figures/')
# Libraries
library(vilaweb)
library(rtweet)
library(tidyverse)
library(databrew)
library(translateR)
library(sentimentr) # https://github.com/trinker/sentimentr
df <- vilaweb::ceo
df$date <- paste0(df$ANY, df$MES)
table(df$P16A_REC)
x <- df %>%
  filter(ANY >= 2018) %>%
  group_by(#tv = P16A_REC,
           independence = P31,
           language = C704) %>%
  summarise(n = sum(PONDERA)) %>%
  group_by(language) %>%
  mutate(p = n / sum(n) * 100) %>%
  ungroup
x %>%
  filter(independence == 'Sí')
# Language and constitution
df <- vilaweb::ceo
df$date <- paste0(df$ANY, df$MES)
df$constitution <- df$P95
# Keep only the survey where they asked about the constitution
df <- df %>% filter(!is.na(constitution))
df <- df %>%
  mutate(language = C704) %>%
  mutate(language = as.character(language)) %>%
  mutate(language = 
           ifelse(language %in% c('No ho sap', 'No contesta'),
                  'NS/NC',
                  ifelse(grepl('Català', language),
                         'Català',
                         ifelse(grepl('Totes dues', language),
                                'Català/Castellà\nigual',
                                ifelse(grepl('Catell', language),
                                       'Castellà',
                                       ifelse(grepl('Altres', language),
                                              'Altres llengües\n/combinacions', language)))))) %>%
  mutate(constitution = as.character(constitution)) %>%
  # filter(constitution %in% c('Votaria sí', 'Votaria no'))
   mutate(constitution = ifelse(constitution %in% c('Votaria sí',
                                                       'Votaria no'),
                                   constitution,
                                   'NS/NC/Nul'))
df$constitution <- factor(df$constitution,
                          levels = rev(c('Votaria sí',
                                     'NS/NC/Nul',
                                     'Votaria no')))

x <- df %>%
  filter(language != 'NS/NC') %>%
  group_by(constitution,
           language) %>%
  summarise(n = sum(PONDERA)) %>%
  group_by(language) %>%
  mutate(p = n / sum(n) * 100) %>%
  ungroup


x <- x %>%
  group_by(language) %>%
  mutate(ordy = p[constitution == 'Votaria sí']) %>%
  arrange(ordy)
x$language <- factor(x$language, levels = unique(x$language))

bp <- function(x,z){RColorBrewer::brewer.pal(n = 8,name =x)[z]}
  cols <- rev(c(bp('Blues', 5),
                bp('Greys', 6),
                bp('Oranges', 5)))


ggplot(data = x,
       aes(x = language,
           y = p,
           fill = constitution)) +
  geom_bar(stat = 'identity',
           position = position_dodge(width = 0.7)) +
  theme_databrew() +
  labs(x = 'Llengua considerada "pròpia"',
       y = 'Percentage',
       title = 'Suport de la constitució espanyola entre catalans',
    subtitle = 'Si es tornés a celebrar un referèndum per decidir sobre l’actual\nConstitució espanyola aprovada el 1978, tal com és ara, vostè què faria?',
    caption = 'Dades del Baròmetre d\'Opinió Pública, 3a onada 2018.\nMostra: 1500 residents de Catalunya amb ciutadania espayola.\nElaboració del gràfic: Joe Brew | @joethebrew') +
  scale_fill_manual(name = '',
                    values = cols) +
  geom_text(aes(label = round(p, digits = 1),
                y = p -4),
            position = position_dodge(width = 0.6),
            alpha = 0.7,
            color = 'white',
            size = 3)
ggsave('~/Desktop/lang.png')

y <-  df %>%
  filter(language != 'NS/NC') %>%
  group_by(constitution) %>%
  summarise(n = sum(PONDERA)) %>%
  mutate(p = n / sum(n) * 100) %>%
  ungroup

ggplot(data = y,
       aes(x = constitution,
           y = p,
           fill = constitution)) +
  geom_bar(stat = 'identity',
           position = position_dodge(width = 0.7)) +
  theme_databrew() +
  labs(x = '',
       y = 'Percentage',
       title = 'Suport de la constitució espanyola entre catalans',
    subtitle = 'Si es tornés a celebrar un referèndum per decidir sobre l’actual\nConstitució espanyola aprovada el 1978, tal com és ara, vostè què faria?',
    caption = 'Dades del Baròmetre d\'Opinió Pública, 3a onada 2018.\nMostra: 1500 residents de Catalunya amb ciutadania espayola.\nElaboració del gràfic: Joe Brew | @joethebrew') +
  scale_fill_manual(name = '',
                    values = cols) +
  geom_text(aes(label = round(p, digits = 1),
                y = p -4),
            position = position_dodge(width = 0.6),
            alpha = 0.7,
            color = 'white',
            size = 5) +
  theme(legend.position = 'none')
ggsave('~/Desktop/lang.png')


joebrew/vilaweb documentation built on Sept. 11, 2020, 3:42 a.m.