# 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)
# Set up
source('get_data.R')
# There is now an object named tl, with all tweets
# Ensure no duplicates
tl <- tl %>% dplyr::distinct(.keep_all = TRUE) %>%
  filter(!duplicated(id))
# Flag the violence posts
tl <- tl %>%
  # Identify comandos posts
  mutate(comandos = grepl('comando', tolower(tweet)),
         kale = grepl('kale borroka', tolower(tweet))) %>%
    mutate(violence = grepl('violen', tolower(tweet))) %>%
  mutate(genero = grepl('machis|masclis|mujer|dona|done|géner|gèner|genero|marido|marit|novio|esposa|novia|doméstic|domèstic', tolower(tweet))) %>%
  mutate(violence_not_genero = violence & !genero) 

# By time
bt <- 
  tl %>%
  mutate(year_month = date_truncate(date, level = 'month') + 15) %>%
  group_by(date = year_month, 
           person = username) %>%
  summarise(n_violence_not_genero = length(which(violence_not_genero)),
            n_violence = length(which(violence)),
            n_comandos = length(which(comandos)),
            n_kale = length(which(kale)),
            d = n()) %>%
  ungroup %>%
  mutate(p_comandos = n_comandos / d * 100,
         p_kale = n_kale / d * 100,
         p_violence = n_violence / d * 100) 

bt_kale <- bt %>%
  filter(date >= '2017-08-01',
         date <= '2018-12-30')

bt <- bt %>%
  filter(date >= '2017-08-01',
         date <= '2018-11-30')
# Plot of use of violence among cronica global
make_cronica_plot <- function(language = 'en'){
  dater <- catalan_date(new_line = FALSE)
  plot_data <- bt 
  # plot_data <- plot_data %>%
  #   group_by(person) %>%
  #   filter(sum(n_kale) > 0) %>%
  #   ungroup
  plot_data <- plot_data %>% filter(person == 'cronicaglobal')

  # plot_data <- plot_data %>%
  #   filter(person %in% c('albert_rivera',
  #                        'inesarrimadas',
  #                        'ciudadanoscs',
  #                        'ciutadanscs'))
  gapper <- 5
    date_breaks <- sort(unique(plot_data$date))
  plot_data$person<- paste0('@', plot_data$person)

  label_df <- data_frame(date = as.Date('2017-10-01'),
                         y = 15,
                         label = 'La "Rebelión"')

  if(language == 'en'){
    title = 'Tweets from Cronica Global containing the word "violence"'
         subtitle = 'Monthly, August 2017-December 2018'
         caption = '*Removed tweets identified as being specific to gender violence: "machista, masclisme, mujer, dona, género, marido, etc.".\n*A tweet was flagged as containing the word "violence" if it contained the sub-string "violen", ie "violencia", "violentos"...\nData extracted/processed and chart created on 8 December 2018. Joe Brew. @joethebrew.'
         x = 'Month'
         y = 'Tweets'
  } else {
    title = 'Tuits de Cronica Global amb la paraula "violencia"'
         subtitle = 'Mensual, Agost 2017-Desesmbre 2018'
         caption = '*Removed tweets identified as being specific to gender violence: "machista, masclisme, mujer, dona, género, marido, etc.".\n*A tweet was flagged as containing the word "violence" if it contained the sub-string "violen", ie "violencia", "violentos"...\nDades descarregades/processades i gràfic creat el 8 de desembre 2018. Joe Brew. @joethebrew.'
         x = 'Mes'
         y = 'Tuits'
  }

  g <- ggplot(data = plot_data,
         aes(x = date,
             y = n_violence_not_genero)) +
    geom_bar(stat = 'identity',
             fill = '#ff6600',
             color = 'black',
             lwd = 0.3,
             aes(group = person
             # ,fill = person
             )) +
    theme_vilaweb() +
    labs(title = title,
         caption = caption,
         subtitle = subtitle,
         x = '',
         y = y) +
    # scale_fill_manual(name = '',
    #                   values = databrew::make_colors(n = length(unique(plot_data$person))))  +
    scale_y_continuous(breaks = seq(0, 100, 5)) +
    scale_x_date(breaks = sort(unique(plot_data$date)),
                 labels = dater(sort(unique(plot_data$date)))) +
    # facet_wrap(~person) +
    theme(axis.text.x = element_text(angle = 90,
                                     hjust = 1,
                                     size = 15)) +
    # geom_label(data = label_df,
    #            aes(x = date,
    #                y = y,
    #                label = label),
    #            fill = 'white',
    #            size = 6) +
    # geom_line(data = data.frame(date = rep(as.Date('2017-10-15'), 2),
    #                              y = c(12, 0)),
    #           aes(x = date,
    #               y = y)) +
    # geom_point(data = data.frame(date = rep(as.Date('2017-10-15'), 2),
    #                              y = c(12, 0)),
    #           aes(x = date,
    #               y = y)) +
    theme(plot.title = element_text(size = 18),
          plot.subtitle = element_text(size = 10),
          legend.text = element_text(size = 10,
                                     color = 'black'))
    # guides(fill = guide_legend(ncol = 3,
    #                            keywidth=0.3,
    #              keyheight=0.2,
    #              default.unit="inch")) 
    # theme(legend.position = 'right')
  return(g)
}
make_cronica_plot('ca')
# Plot of use of violence among cronica global
make_cronica_table <- function(){
  plot_data <- bt  %>% filter(person == 'cronicaglobal')
  knitr::kable(plot_data)
}
make_cronica_table()


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