html-parser.R 21.5 KB
Newer Older
1 2 3 4 5
# Script to count how many titles per hour that have certain words in home pages of newspapers.
# It generates some plots to view the data
# Ask @numeroteca or info@montera34.com for questions, suggestions and collaborations

# Load libraries -----
6 7
library(rvest)
library(stringr)
8
# library(R.utils) # opens gzip compresed file
9 10 11
library(gsubfn) # select text in the parenthesis with regex
library(tidyverse) # for ggplot

12
# Set search variables: words and datelimits -----
13 14 15
word <- "fondos reservados|caja b|Jorge Fernández Díaz|Bárcenas|destruir pruebas|Kitchen|Fernández Díaz"
word$explain <- "Fondos reservados para defender al PP"
# word <- "Cifuentes|Javier Ramos|Enrique Álvarez Conde|Pablo Chico|María Teresa Feito|Alicia López de los Cobos|Cecilia Rosado|Clara Souto|Amalia Calonge|Universidad Rey Juan Carlos|URJC"
16
# word <- "vox|Vox|VOX|Santiago Abascal|ortega smith|francisco serrano"
17

18 19 20 21 22 23 24 25
# Caso Fondos reservados para defender al PP
word1 <- "fondos reservados|caja b|Jorge Fernández Díaz|Bárcenas|destruir pruebas|Kitchen|Fernández Díaz"
word2 <- "PP"
word3 <- "caja b"
word4 <- "Bárcenas"
word5 <- "Kitchen"
word6 <- "Jorge Fernández Díaz|Fernández Díaz"

26
# Caso Master Cifuentes
27 28 29 30 31 32
# word1 <- "Cifuentes|Javier Ramos|Enrique Álvarez Conde|Pablo Chico|María Teresa Feito|Alicia López de los Cobos|Cecilia Rosado|Clara Souto|Amalia Calonge|Universidad Rey Juan Carlos|URJC"
# word2 <- "Cifuentes"
# word3 <- "Cristina Cifuentes"
# word4 <- "Universidad Rey Juan Carlos"
# word5 <- "URJC"
# word6 <- "Enrique Álvarez Conde"
33

numeroteca's avatar
numeroteca committed
34
# partidos y líderes
35 36 37 38 39 40
# word1 <- "vox|Vox|VOX|Santiago Abascal|Abascal"
# word2 <- "Podemos|Pablo Iglesias|Iglesias"
# word3 <- "PP|Pablo Casado|Casado"
# word4 <- "PSOE|Pedro Sánchez|Sánchez"
# word5 <- "C's|Cs|Ciudadanos|Albert Rivera|Rivera"
# word6 <- "PACMA|Silvia Barquero|Barquero"
41

numeroteca's avatar
numeroteca committed
42
# partidos y líderes
43 44 45 46 47 48
# word1 <- "vox|Vox"
# word2 <- "vox|Vox|VOX"
# word3 <- "vox|Vox|VOX|Abascal"
# word4 <- "vox|Vox|VOX|Santiago Abascal|Abascal"
# word5 <- "vox|Vox|VOX|Santiago Abascal|Abascal|Ortega Smith"
# word6 <- "vox|Vox|VOX|Santiago Abascal|Abascal|Ortega Smith|Rocio Monasterio|Monasterio"
numeroteca's avatar
numeroteca committed
49

50 51 52 53 54 55 56 57 58 59 60
word  <- ""
word <- data.frame(matrix(ncol = 1,nrow = 6 ))
names(word)  <- c("word")

word$word[1] <- word1
word$word[2] <- word2
word$word[3] <- word3
word$word[4] <- word4
word$word[5] <- word5
word$word[6] <- word6

61

62
# Select word to be displayed in plots
63 64 65 66 67 68 69 70 71 72 73 74 75 76
word1_explain <- "PPgate"
word2_explain <- "PP"
word3_explain <- "cajab"
word4_explain <- "Barcenas"
word5_explain <- "kitchen"
word6_explain <- "Jorge_Fdez_diaz"

# Select word to be displayed in plots
# word1_explain <- "Caso Master" 
# word2_explain <- "Cifuentes"
# word3_explain <- "Cristina Cifuentes"
# word4_explain <- "Universidad Rey Juan Carlos"
# word5_explain <- "URJC"
# word6_explain <- "Enrique Álvarez Conde"
77

numeroteca's avatar
numeroteca committed
78
# # Select word to be displayed in plots
79 80 81 82 83 84
# word1_explain <- "VOX" #
# word2_explain <- "Podemos" #
# word3_explain <- "PP" #
# word4_explain <- "PSOE" #
# word5_explain <- "Cs" #
# word6_explain <- "PACMA" #
85

numeroteca's avatar
numeroteca committed
86
# # Select word to be displayed in plots
87 88 89 90 91 92
# word1_explain <- "VOXmin" #
# word2_explain <- "VOXmay" #
# word3_explain <- "VOXabascal" #
# word4_explain <- "VOXsantiagoabascal" #
# word5_explain <- "VOXortega" #
# word6_explain <- "VOXmonasterio" #
numeroteca's avatar
numeroteca committed
93

94 95 96 97 98 99
word$explain[1] <- word1_explain
word$explain[2] <- word2_explain
word$explain[3] <- word3_explain
word$explain[4] <- word4_explain
word$explain[5] <- word5_explain
word$explain[6] <- word6_explain
100

101
# open compressed file
102 103
# gunzip("eldiario/http!www.eldiario.es!!!!@2018-04-06T23:01:07.888847+00:00.gz",remove=FALSE)
# no hace falta, read_html lee el .gz sin necesidad de descomprimir
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
# Testing parsing news in different newspapers ---------------------------
# ------- eldiario.es ----------------------
# reads html and stores it
page <- read_html("data/http!www.eldiario.es!!!!@2018-04-06T23:01:07.888847+00:00.gz")

# gets all the text in article titles. All articles are in h2 except the comics.
titles <- page %>% html_nodes("article h2 a") %>% html_text() %>% data.frame()
colnames(titles) <- "title"
titles$title <- as.character(titles$title)

# total of articles with link
n_news <- nrow(titles) 

# select news that contain cerating word
select_news <- data.frame(titles[grepl(word, titles$title),])

# Results
# number of articles that contain words
n_select_news<- nrow(select_news)
# Percentage of articles that contain words
percent <- n_select_news / n_news * 100

# ------- elconfidencial ----------------------
# reads html and stores it
page <- read_html("data/http!www.elconfidencial.com!|!!!@2018-03-22T09:01:11.318750+00:00.gz")

# gets all the text in article titles. All articles are in h2 except the comics.
titles <- page %>% html_nodes("article h3 a") %>% html_text() %>% data.frame()
colnames(titles) <- "title"

# total of articles with link
n_news <- nrow(titles) 

# select news that contain cerating word
select_news <- data.frame(titles[grepl(word, titles$title),])

# Results
# number of articles that contain words
n_select_news<- nrow(select_news)
# Percentage of articles that contain words
percent <- n_select_news / n_news * 100

# ------- elmundo ----------------------
# reads html and stores it
149
pageelmundo <- read_html("homepages_test/http!www.elmundo.es!!!!@2020-02-04T21:01:03.395289+00:00.gz") 
150 151

# gets all the text in article titles. All articles are in h2 except the comics.
152
titles <- pageelmundo %>% html_nodes("main article h2") %>% html_text() %>% data.frame() #TODO NO FUNCIONA
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
colnames(titles) <- "title"

# total of articles with link
n_news <- nrow(titles) 

# select news that contain cerating word
select_news <- data.frame(titles[grepl(word, titles$title),])

# Results
# number of articles that contain words
n_select_news<- nrow(select_news)
# Percentage of articles that contain words
percent <- n_select_news / n_news * 100

# ------- elpais ----------------------
# reads html and stores it
pageelpais <- read_html("data/http!www.elpais.com!!!!@2017-07-04T13:51:08.133418+00:00.gz")

# gets all the text in article titles. All articles are in h2 except the comics.
titles <- pageelpais %>% html_nodes("article h2 a") %>% html_text() %>% data.frame() #TODO NO FUNCIONA
colnames(titles) <- "title"

# total of articles with link
n_news <- nrow(titles) 

# select news that contain cerating word
select_news <- data.frame(titles[grepl(word, titles$title),])

# Results
# number of articles that contain words
n_select_news<- nrow(select_news)
# Percentage of articles that contain words
percent <- n_select_news / n_news * 100

# ------- La Razón ----------------------
# reads html and stores it
189
pagelarazon <- read_html("homepages_test/http!www.larazon.es!|!!!@2020-02-04T21:01:04.579808+00:00.gz", to="UTF-8") #TODO correct encoding
190 191

# gets all the text in article titles. All articles are in h2 except the comics.
192
titles <- pagelarazon %>% html_nodes("h2") %>% html_text() %>% data.frame() #TODO NO FUNCIONA
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
colnames(titles) <- "title"
titles

# total of articles with link
n_news <- nrow(titles) 

# select news that contain cerating word
select_news <- data.frame(titles[grepl(word, titles$title),])

# Results
# number of articles that contain words
n_select_news<- nrow(select_news)
# Percentage of articles that contain words
percent <- n_select_news / n_news * 100
# -------- La razón test ----------
# for (i in 1:nrow(selected)) {
#   if ( selected$newspaper[i] == "larazon") {
#   page <- read_html( paste("data/",selected$urls[i], sep = "") )
# 
#     titles <- page %>% html_nodes("article h2 a") %>% html_text() %>% data.frame()
# 
#   colnames(titles) <- "title"
#   # titles$title <- as.character(titles$title)
#   # total of articles with link
#   n_news <- nrow(titles)
#   print(paste("nº noticias:",n_news))
# 
#   # select news that contain cerating word
#   selected_news <- data.frame(titles[grepl(word, titles$title),])
#   print(selected_news)
#   # Results
#   # number of articles that contain words
#   n_selected_news<- nrow(selected_news)
#   print(n_selected_news)
#   print(paste("nº noticias Cifuentes:",n_selected_news))
#   # Percentage of articles that contain words
# 
#   print(paste("day:",selected$day[i],"hour:",selected$hour[i],selected$newspaper[i]))
#   }
# }

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
# ------- Cadenaser ----------------------
# reads html and stores it
pageser <- read_html("homepages_test/http!cadenaser.com!|!!!@2020-02-04T21:01:45.200707+00:00.gz")

# gets all the text in article titles. All articles are in h2 except the comics.
titles <- pageser %>% html_nodes("article h2 a") %>% html_text() %>% data.frame() #HA dejado de funcionar, parace que usan algo de js:
# {"@context":"https:\/\/schema.org","@type":"NewsArticle",
#  "mainEntityOfPage":{"@type":"WebPage","@id":"\/\/cadenaser.com\/ser\/2020\/02\/03\/ciencia\/1580724778_294079.html"},
#  "url":"\/\/cadenaser.com\/ser\/2020\/02\/03\/ciencia\/1580724778_294079.html",
#  "headline":"Google te conoce mejor que nadie: descubre cu\u00e1les son tus intereses seg\u00fan el buscador",
#  "author":{"@type":"Person","name":"David Justo"},
#  "contentLocation":{"@type":"Place","name":"Madrid"},
#  "datePublished":"2020-02-03T12:23:58+01:00","dateModified":"2020-02-03T12:23:58+01:00",
#  "image":{"@type":"ImageObject","url":"https:\/\/cadenaser00.epimg.net\/\/cadenaser00.epimg.net\/ser\/imagenes\/2020\/02\/03\/ciencia\/1580724778_294079_1580728804_portada_normal.jpg",
#  "width":"720","height":"720"},
#  "publisher":{"@type":"Organization","url":"https:\/\/cadenaser.com","name":"Cadena SER",
# "logo":{"@type":"ImageObject","url":"https:\/\/cadenaser00.epimg.net\/ser\/iconos\/v1.x\/v1.0\/logos\/logo_ser_cabecera_rss.png",
# "width":"600","height":"60"}}}
colnames(titles) <- "title"

# total of articles with link
n_news <- nrow(titles) 

# select news that contain cerating word
select_news <- data.frame(titles[grepl(word, titles$title),])

# Results
# number of articles that contain words
n_select_news<- nrow(select_news)
# Percentage of articles that contain words
percent <- n_select_news / n_news * 100
# ------- ABC ----------------------
# reads html and stores it
pageabc <- read_html("homepages_test/http!www.abc.es!|!!!@2020-02-04T21:01:03.836719+00:00.gz")

# gets all the text in article titles. All articles are in h2 except the comics.
titles <- pageabc %>% html_nodes("article h3 a") %>% html_text() %>% data.frame() #TODO NO FUNCIONA
colnames(titles) <- "title"

# total of articles with link
n_news <- nrow(titles) 

# select news that contain cerating word
select_news <- data.frame(titles[grepl(word, titles$title),])

# Results
# number of articles that contain words
n_select_news<- nrow(select_news)
# Percentage of articles that contain words
percent <- n_select_news / n_news * 100

# ------- Infolibre ----------------------
# reads html and stores it
pageinfolibre <- read_html("homepages_test/https!www.infolibre.es!|!!!@2020-02-04T21:01:11.035956+00:00.gz")

# gets all the text in article titles. All articles are in h2 except the comics.
titles <- pageinfolibre %>% html_nodes("#ctd h2 a") %>% html_text() %>% data.frame() #TODO NO FUNCIONA
colnames(titles) <- "title"

# total of articles with link
n_news <- nrow(titles) 

# select news that contain cerating word
select_news <- data.frame(titles[grepl(word, titles$title),])

# Results
# number of articles that contain words
n_select_news<- nrow(select_news)
# Percentage of articles that contain words
percent <- n_select_news / n_news * 100
304
# --------- Create data frame with all the newspapers + time and date---------------
305

306 307 308 309
# Read list of files. The list has been generated with this bash script, 
  # when you are located in the directory with all the .gz files: 
  # for f in *.gz; do echo "$f" >> mylist.txt; done
# CHANGE THIS: write path where your mylist.file is located
310
list <- read_csv("data/mylist.txt")
311 312
# add name to the one column file
names(list) <- "urls"
313 314

# extract name of newspaper
315 316
list$urls  <-  sub("file './", "", list$urls )
list$urls <-  sub("'", "", list$urls)
317
list$newspaper <- strapplyc( as.character(list$urls), "[a-z]*!([a-z]{1,61}.[a-zA-Z]{2,})", simplify = TRUE)
318

319 320 321 322 323 324 325 326 327 328 329
# list$newspaper <- as.factor(list$newspaper) No funciona convertirlo a factor
list$newspaper <-  sub("www.", "", list$newspaper)

# extract year,month, day, hour
list$year <- as.numeric(strapplyc( as.character(list$urls), ".*@([0-9]*)", simplify = TRUE))
list$month <- as.numeric(strapplyc( as.character(list$urls), ".*@[0-9]*-([0-9]*)", simplify = TRUE))
list$day <- as.numeric(strapplyc( as.character(list$urls), ".*@[0-9]*-[0-9]*-([0-9]*)", simplify = TRUE))
list$hour <- as.numeric(strapplyc( as.character(list$urls), ".*@[0-9]*-[0-9]*-[0-9]*T([0-9]*)", simplify = TRUE))

# create date
list$date <- as.Date( paste(list$day,"/",list$month,"/",list$year,sep = "" ), "%d/%m/%Y")
330
list$timestamp <- as.POSIXlt( paste(list$year,"-",list$month,"-",list$day," ",list$hour,":00:00", sep = "" ))
331

332
# Save created list of front pages 
333
save(list,file="data/list.Rda")
334 335

# You can avoid all of the above and just load the existing file
336 337
load("data/list.Rda")

338 339 340 341
# Check visually if files exists
ggplot(list[list$newspaper == "larazon", ]) +
  geom_point(aes(x=date,y=month), alpha = 0.005)

342 343 344
# which newspapers are stored, how many homepages each?
table(list$newspaper)

345 346
# Process home pages ---------------------------

347
# Create list of selected pages. Select timeframe and newspapers
348
selected <- list[(list$newspaper == "eldiario" | list$newspaper == "elconfidencial" | 
349 350 351 352 353 354
                    list$newspaper == "elpais"| 
                    # list$newspaper == "larazon" |
                    list$newspaper == "elespanol" | 
                    # list$newspaper == "cadenaser.com" | 
                    list$newspaper == "abc" | list$newspaper == "elmundo" | list$newspaper == "infolibre") &
                   list$date > "2020-01-20" & list$date < "2020-02-05", ]
355

356
# Create results dataframe
357 358 359 360
results <- ""
results <- data.frame(matrix(ncol = 1,nrow = nrow(selected)  ))
names(results)  <- c("newspaper")

361
# Loop to count how many titles per homepages have certain words
362
for (i in 1:nrow(selected)) {
363
  page <- read_html( paste("../storytracker/data-homepages/",selected$urls[i], sep = "") )
364
  # page <- read_html("homepages_test/http!www.elmundo.es!!!!@2020-02-04T21:01:03.395289+00:00.gz")
365
  
366 367
  # Depending on the newspaper defines the parser to select the news
  if ( selected$newspaper[i] == "eldiario" | selected$newspaper[i] == "elpais" ) {
368 369 370
    # eldiario
    # gets all the text in article titles. All articles are in h2 except the comics.
    titles <- page %>% html_nodes("article h2 a") %>% html_text() %>% data.frame()
371 372 373 374 375 376
  } else if ( selected$newspaper[i] == "elmundo" ) {
    print("elmundo")
    titles <- page %>% html_nodes("main article h2") %>% html_text() %>% data.frame()
  } else if ( selected$newspaper[i] == "larazon" ) {
      print("larazon")
      titles <- page %>% html_nodes("h2") %>% html_text() %>% data.frame()
377
  } else if ( selected$newspaper[i] == "cadenaser.com") {
378
    print("cadenaser")
379
    titles <- page %>% html_nodes("h2 a") %>% html_text() %>% data.frame()
380
  } else if ( selected$newspaper[i] == "elconfidencial" | selected$newspaper[i] == "elmundo" | selected$newspaper[i] == "elespanol") {
381
    titles <- page %>% html_nodes("article h3 a") %>% html_text() %>% data.frame()
382 383 384 385
  } else if ( selected$newspaper[i] == "infolibre" ) {
    titles <- page %>% html_nodes("#ctd h2 a") %>% html_text() %>% data.frame()
  } else if ( selected$newspaper[i] == "abc" ) {
    titles <- page %>% html_nodes("article h3 a") %>% html_text() %>% data.frame()
386
  }
387 388
  
  # renames columns name to title
389
  colnames(titles) <- "title"
390
  # titles$title <- as.character(titles$title)
391
  
392
  # newspaper name
393
  results$newspaper[i] <- selected$newspaper[i]
394 395
  
  # insert date and time
396 397 398
  results$day[i] <- selected$day[i]
  results$month[i] <- selected$month[i]
  results$year[i] <- selected$year[i]
399 400 401 402
  results$hour[i] <- selected$hour[i] 
  
  # total of articles with link
  n_news <- nrow(titles)
403
  print(paste("------------- nº noticias:",n_news,"in",selected$newspaper[i]))
404
  results$n_news[i] <- n_news
405
  
406 407 408 409 410 411 412
  # select news that contain certaing word
  # for (j in 1:1) { # for one word
  # for (j in 1:5) { # for 5 words
  for (j in 1:nrow(word)) { # for multiple words
    
    selected_news <- data.frame(titles[grepl(word$word[j], titles$title),]) # multiple words
    # selected_news <- data.frame(titles[grepl(word, titles$title),]) # one word
413 414 415 416
    
    # Results
    # number of articles that contain words
    n_selected_news <- nrow(selected_news)
417 418 419
    print(paste("1. nº noticias con las palabras",word$explain[j],n_selected_news))  # multiple words
    # print(paste("1. nº noticias con las palabras",word$explain[j],n_selected_news)) # one word
    
420 421 422
    # print(selected_news)
    # Percentage of articles that contain words
    percent <- round(n_selected_news / n_news * 100, digits = 2)
423
    print(paste("2. percent ",word$explain[j],percent))
424
    
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
    # creates columns that will be renamed afterwrads
    print("creates fake columns")
    results$a <- 0
    results$b <- 0
    results$c <- 0
    results$d <- 0
    results$e <- 0
    results$f <- 0
    results$aa <- 0
    results$bb <- 0
    results$cc <- 0
    results$dd <- 0
    results$ee <- 0
    results$ff <- 0
    results$aaa <- 0
    results$bbb <- 0
    results$ccc <- 0
    results$ddd <- 0
    results$eee <- 0
    results$fff <- 0
445 446
    
    # create column with variable names
447
    # TODO: this is cool feature but a pain when it comes to producing the plots
448 449 450 451 452 453 454 455 456 457
    names(results)[6+j] <- paste0("n_selected_news_",j,"_",word$explain[j])
    names(results)[7+j+nrow(word)] <- paste0("percent_",j,"_",word$explain[j])
    names(results)[8+j+2*nrow(word)] <- paste0("titles_",j,"_",word$explain[j])
    
    # insert value in results$n_selected_news_word_explain
    results[i,6+j] <- as.integer(n_selected_news)
    results[i,7+j+nrow(word)] <- percent
    results[i,8+j+2*nrow(word)] <- sapply(as.list(selected_news), paste0, collapse=";   ") 
    
  }
458
  
459
  print(paste("year:",selected$year[i], "month:",selected$month[i], "day:",selected$day[i],"hour:",selected$hour[i],selected$newspaper[i]))
460 461
}

462 463 464 465 466
# removes unused columns
results <- results %>% select(-a,-b,-c,-d,-e,-f,-aa,-bb,-cc,-dd,-ee,-ff,-aaa,-bbb,-ccc,-ddd,-eee,-fff)

# creates time stampt
results$date <- as.POSIXct( paste(results$year,"-",results$month,"-",results$day," ",results$hour,":00:00", sep = "" ))
467

468 469 470 471 472 473 474 475 476
# results5 <- ""
# results5 <- data.frame(matrix(ncol = 1,nrow = 5  ))
# names(results5)  <- c("newspaper")
# x <- as.list(selected_news2)
# results5$titles2[1] <- sapply(x, paste0, collapse=";    ") 
# 
# y <- as.character(selected_news1[1,1])
# writeLines(iconv(readLines("tmp.html"), from = "ANSI_X3.4-1986", to = "UTF8"), "tmp2.html")
# iconv(selected_news2[,1], 'utf-8', 'ascii', sub='')
477

478
# Save results 
479
save(results,file="data/results-ppgate_01.Rda")
480
# save(results,file="data/results-vox-01.Rda")
481
# save(results,file="data/results-generales2019-6-partidos-1-25abril.Rda")
numeroteca's avatar
numeroteca committed
482
# save(results,file="data/results-vox-diferencias-busqueda.Rda")
483
# Load other results
484
# load("data/results-cifuentes-01.Rda")
485 486

# --------  Analysis and comparision with Pageonex.com paper front pages data ---------
487 488 489 490 491 492 493 494 495 496
library("rjson")
# Get json from pageonex.com
json_file <- "http://pageonex.com/numeroteca/tfm-cifuentes/export.json"
json_data <- fromJSON(paste(readLines(json_file), collapse=""))

# Create empty dataf rame
df <- data.frame(matrix( ncol = 10,nrow = length(json_data$dates)  ))
# fill dataframe with pageonex data
for (i in 1:length(json_data$dates)) {
  df[i,] <- data.frame(json_data$data[[i]])
497 498
}

499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
# Make column and row names nicer 
colnames(df) <- json_data$media
colnames(df)[10] <- "media"
rownames(df) <- json_data$dates

# create variable with dates
df$date <- rownames(df)

library(reshape)
portadas <- melt(df, id=c("date"))
portadas$value <- portadas$value*100
portadas$date <- as.Date(portadas$date, "%Y-%m-%d")
class(portadas$date)

# Plot para varios periódico
ggplot(data=portadas) + 
  geom_line(aes(x=date, y=value, group=variable),color="#000000",size=0.2) +
  labs(title = "% portadas dedicada a escándalo Cifuentes en portada")

ggplot( data=portadas[portadas$variable=="media",],aes(x = date)) + 
  geom_bar(aes(weight = value)) +
  labs(title = "Media % portada dedicado a escándalo Cifuentes")

ggplot( data=portadas[!portadas$variable=="media",],aes(x = date)) + 
  geom_bar(aes(weight = value, fill=variable)) +
  labs(title = "% portada dedicado a escándalo Cifuentes")

ggplot( data=portadas[portadas$variable=="La Razón",],aes(x = date)) + 
  geom_bar(aes(weight = value)) +
  labs(title = "La Razón: % portada dedicado a escándalo Cifuentes")

ggplot( ) + 
  geom_bar(data=portadas[portadas$variable=="La Razón",], aes(x = timestamp,weight = value),fill="#A74a83") +
  geom_line(data=results[results$newspaper=="larazon",], aes(x=date, y=percent, group=newspaper),color="#e78ac3",size=0.7) +
  labs(title = "La Razón: % portada papel vs % noticias en digital dedicado a escándalo Cifuentes")

ggplot( ) + 
  geom_bar(data=portadas[portadas$variable=="El Pa",], aes(x = timestamp,weight = value),fill="#A74a83") +
  geom_line(data=results[results$newspaper=="larazon",], aes(x=date, y=percent, group=newspaper),color="#e78ac3",size=0.7) +
  labs(title = "La Razón: % portada papel vs % noticias en digital dedicado a escándalo Cifuentes")

ggplot( data=portadas,aes(x = variable) ) + 
  geom_bar(aes(weight = value/nrow(df))) +
  labs(title = "% dedicado a escándalo Cifuentes en portada") +
  coord_flip()
544

545 546 547 548
# Plot de la media
ggplot(data=portadas[portadas$variable=="media",]) + 
  geom_line(aes(x=date, y=value, group=variable),color="#000000",size=0.2) +
  labs(title = "% portadas dedicada a escándalo Cifuentes en portada")
549

550 551 552
portadas$timestamp <- as.POSIXlt( paste(portadas$date," ","00:00:00", sep = "" ))
summary(portadas)
class(portadas$timestamp)
553 554