# 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
require(RPostgreSQL)
require(readr)  
require(DBI)
# # Get most recent tweets from our people of interest
people <- 
  tolower(c('Santi_ABASCAL',
    'Albert_Rivera',
    'InesArrimadas',
    'sanchezcastejon',
    'pablocasado_',
    'KRLS',
    'QuimTorraiPla',
    'perearagones',
    'ALevySoler',
    'elsa_artadi',
    'miqueliceta',
    'Pablo_Iglesias_'))

if(file.exists('tl.RData')){
  load('tl.RData')
} else {
  # Connect to the db
  pg = DBI::dbDriver("PostgreSQL")
  con = DBI::dbConnect(pg, dbname="twitter")
  tl <- RPostgreSQL::dbGetQuery(
    con,
    paste0("SELECT * FROM twitter")
  )
  save(tl, file = 'tl.RData')  
  dbDisconnect(con)
  }


# Search for the words rebeliation
find_rebel <- function(x){
  # grepl('lazos amarillos|llaços grocs', tolower(x))
  grepl('155', tolower(x))
}
tl <- tl %>%
  mutate(rebel = find_rebel(tweet))
pd <- tl %>%
  # filter(tolower(username) %in% tolower(people)) %>%
  filter(rebel) %>%
  filter(date >= '2017-01-01') %>%
  mutate(date = as.Date(cut(date, 'month'))) %>%
  group_by(date, 
           person = username) %>%
  tally %>%
  ungroup 
left <- expand.grid(# rebel = sort(unique(tl$rebel)),
                    date = seq(min(pd$date),
                               max(pd$date),
                               by = 'month'),
                    person = sort(unique(tl$username)))
pd <- left_join(left, pd)

pd$n[is.na(pd$n)] <- 0

# # Keep only relevant year
# pd <- pd %>% filter(date >= '2017-05-01')

make_plot <- function(p = 'adacolau'){
  ppd <- pd %>% filter(person %in% p) %>%
    mutate(person = paste0('@', person))
  ggplot(data = ppd,
       aes(x = date,
           y = n,
           group = person,
           color = person)) +
  geom_point() +
    geom_line() +
    geom_area(alpha = 0.3) +
  theme_databrew() +
  labs(x = 'Mes',
       y = 'Tweets (mensual)',
       # title = 'Tweets con la frase "lazos amarillos"*',
       title = 'Tweets con la palabra "liquidar"',
       # subtitle = '',
       # title = paste0(paste0('@', p), collapse = ','),
       # caption = 'Incluye "rebelar", "rebeldes", etc. Datos del Twitter API. Gráfico @joethebrew.') +
       # caption = 'Incluye "llaços grocs" también. @joethebrew') +
       caption = 'Datos descargados el 31 de diciembre de 2018.') +
  # scale_fill_manual(name = ' ',
  #                   values = c('darkorange',
  #                              'blue',
  #                              'green')) +
  theme(legend.position = 'none') +
    facet_wrap(~person, scales = 'free_y') +
  scale_x_date(breaks = sort(unique(pd$date)),
               labels = spanish_date(new_line = TRUE)) +
  theme(axis.text.x = element_text(#angle = 90,
                                   size = 7),
        plot.title = element_text(size = 24),
        plot.subtitle =  element_text(size = 35),
        strip.text = element_text(size = 25)) +
    scale_color_manual(name = '',
                       values = "black") + #databrew::make_colors(n = length(unique(ppd$person)))) +
    geom_text(aes(label = n),
              alpha = 0.6,
              size = 3.5,
              nudge_y = 0.8) 

}
make_plot(c('albert_rivera'))

make_simple_plot <- function(p = sort(unique(pd$person))){
  ppd <- pd %>% filter(person %in% p) %>%
    mutate(person = paste0('@', person))
  ppd <- ppd %>% arrange(n) %>%
    filter(!is.na(n),
           n > 21,
           !person %in% congress$user_name)
  ppd$person <- factor(ppd$person, levels = ppd$person)
  ggplot(data = ppd,
       aes(x = person,
           y = n,
           # group = person,
           fill = person)) +
  geom_bar(stat = 'identity',
           color = 'black',
           lwd = 0.1) +
  theme_databrew() +
  labs(x = '',
       y = 'Tweets',
       title = 'Tweets con las palabras "golpe" o "golpista"',
       # subtitle = '*Incluye también "golpista", "golpistas", etc.',
       subtitle = 'Año 2018') +
  theme(legend.position = 'none') +
  theme(#axis.text.x = element_text(angle = 90,
         #                          size = 7),
        plot.title = element_text(size = 21),
        # plot.subtitle =  element_text(size = 35),
        strip.text = element_text(size = 25)) +
    scale_fill_manual(name = '',
                       values = rev(databrew::make_colors(n = length(unique(ppd$person)), categorical = FALSE))) +
    geom_text(aes(label = n),
              alpha = 0.6,
              size = 3.5,
              nudge_y = 20) +
    coord_flip() +
    theme(axis.text.y = element_text(size = 16))

}
make_simple_plot()

ggsave('~/Desktop/golpe.png')


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