# NOT RUN {
## stream tweets mentioning trump for 30 mins
rt <- stream_tweets(
q = "realdonaldtrump",
timeout = (60 * 60 * 30))
## plot tweet data aggregated by minute (default)
ts_plot(rt, by = "mins")
## use a different time increment, line width, and theme
ts_plot(rt, by = "30 secs", lwd = .75, theme = "inverse")
## filter data using regular expressions and
## plot each corresponding time series
ts_plot(rt, by = "mins",
theme = "gray",
main = "Partisanship in tweets about Trump",
filter = c("democrat|liberal|libs",
"republican|conservativ|gop"),
key = c("Democrats", "Republicans"))
## ts_plot also accepts data frames created via ts_filter
rt.ts <- ts_filter(
rt, "mins",
filter = c("democrat|liberal|libs",
"republican|conservativ|gop"),
key = c("Democrats", "Republicans"))
## printing should yield around 30 rows (give or take)
## since stream was 30 mins and aggregated by minute
rt.ts
## Pass data frame created by ts_filter to ts_plot
ts_plot(rt.ts, theme = "spacegray")
## the returned data frame from ts_filter also fits the
## tidyverse and includes three columns
## Column 1 - time Date-time obj of [median] time intervals
## Column 2 - freq Integer (class double) frequency counts
## Column 3 - filter Keys of different time series filters
## This makes it easy to pass the data along to ggplot
## but my themes are cooler anyway so why bother?
## library(ggplot2)
## rt.ts `%>%`
## ggplot(aes(x = time, y = freq, color = filter)) +
## geom_line()
# }
Run the code above in your browser using DataLab