-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtime_series_multiple_series.R
110 lines (94 loc) · 3.42 KB
/
time_series_multiple_series.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Copyright (C) 2016 Pascal Jürgens and Andreas Jungherr
# See License.txt
# Tutorial: Time Series Analysis - Mentions of Candidates in Twitter
# load library
library(ggplot2)
library(reshape2)
library(scales)
# set working directory
setwd("...") # Put here the name of your working directory, you saved your Twitter data in
# load and prepare data - all messages
message_counts_hourly_df<-data.frame(read.csv("mention_counts_candidates_hour.csv"))
JebBush<-message_counts_hourly_df$jebbush
BenCarson<-message_counts_hourly_df$realbencarson
ChrisChristie<-message_counts_hourly_df$chrischristie
TedCruz<-message_counts_hourly_df$tedcruz
CarlyFiorina<-message_counts_hourly_df$carlyfiorina
MikeHuckabee<-message_counts_hourly_df$govmikehuckabee
JohnKasich<-message_counts_hourly_df$johnkasich
RandPaul<-message_counts_hourly_df$randpaul
MarcoRubio<-message_counts_hourly_df$marcorubio
DonaldTrump<-message_counts_hourly_df$realdonaldtrump
dates_hourly<-message_counts_hourly_df[,1]
dates_hourly<-as.POSIXct(strptime(dates_hourly,"%Y-%m-%d %k"))
plot_all_candidate_mentions_hourly_df<-data.frame(JebBush,
BenCarson,
ChrisChristie,
TedCruz,
CarlyFiorina,
MikeHuckabee,
JohnKasich,
RandPaul,
MarcoRubio,
DonaldTrump)
# plot data
setwd("...") # Put here the name of your working directory, you want to save your plots in
plot_all_candidate_mentions_hourly_melted<-melt(plot_all_candidate_mentions_hourly_df)
plot_all_candidate_mentions_hourly_melted_df<-data.frame(
date=as.POSIXct(strptime(dates_hourly,"%Y-%m-%d %H")),
names=factor(plot_all_candidate_mentions_hourly_melted$variable,
levels=c("JebBush",
"BenCarson",
"ChrisChristie",
"TedCruz",
"CarlyFiorina",
"MikeHuckabee",
"JohnKasich",
"RandPaul",
"MarcoRubio",
"DonaldTrump"), ordered=TRUE,
labels=c("Jeb Bush",
"Ben Carson",
"Chris Christie",
"Ted Cruz",
"Carly Fiorina",
"Mike Huckabee",
"John Kasich",
"Rand Paul",
"Marco Rubio",
"Donald Trump")),
mentions_count=plot_all_candidate_mentions_hourly_melted$value
)
debate_start=as.POSIXct(strptime(c("2015-10-28 17"),"%Y-%m-%d %H"))
plot_all_candidate_mentions_hourly<-ggplot(
plot_all_candidate_mentions_hourly_melted_df,aes(x=date,y=mentions_count,group=names))+
geom_line() +
theme_bw()+
facet_wrap(~names, nrow=5) +
theme(axis.text.x=element_text(angle=45,hjust=1))+
xlab("") +
scale_x_datetime(
breaks=date_breaks("1 day"),
minor_breaks=date_breaks("8 hours"),
labels=date_format("%m/%e", tz="CET"))+
geom_vline(xintercept=as.numeric(as.POSIXct(debate_start)),linetype = 2)+
ylab("Mention Volume, Hourly")
ggsave(file="Candidate mentions, hourly.pdf", width = 6, height = 8)
dev.off()
plot_all_candidate_mentions_hourly_scale_free<-ggplot(
plot_all_candidate_mentions_hourly_melted_df,aes(x=date,y=mentions_count,group=names))+
geom_line() +
theme_bw()+
facet_wrap(~names, nrow=5, scales = "free_y") +
theme(axis.text.x=element_text(angle=45,hjust=1))+
xlab("") +
scale_x_datetime(
breaks=date_breaks("1 day"),
minor_breaks=date_breaks("8 hours"),
labels=date_format("%m/%e", tz="CET"))+
geom_vline(xintercept=as.numeric(as.POSIXct(debate_start)),linetype = 2)+
ylab("Mention Volume, Hourly")
ggsave(file="Candidate mentions, hourly (scale free).pdf", width = 6, height = 8)
dev.off()
##########################################################################################
##########################################################################################