#' @title Frequency Graph: Line Plot
#' @description This function plots your data by datetime.
#' @param d Time Series Data
#' @param dn Name
#' @param f Frequency Interval
#' @param b Date Breaks
#' @param trend Trend Line
#' @param c Compare
#' @param cn Compare Name
#' @keywords Frequency
#' @export
#' @examples
#' ts.frequency.line()
ts.frequency.line <- function(d, dn, f, b, trend, c, cn) {
if('rtweet' %in% rownames(installed.packages()) == TRUE) {
require(rtweet)} else {
install.packages("rtweet", repos = "http://cran.us.r-project.org")
require(rtweet)}
if('ggplot2' %in% rownames(installed.packages()) == TRUE) {
require(ggplot2)} else {
install.packages("ggplot2", repos = "http://cran.us.r-project.org")
require(ggplot2)}
if('dplyr' %in% rownames(installed.packages()) == TRUE) {
require(dplyr)} else {
install.packages("dplyr", repos = "http://cran.us.r-project.org")
require(dplyr)}
if(missing(c)) {
# Make New Data from Input
nk.tweets <- d
name <- dn
frequency.time <- f
date.breaks <- b
c <- NA
cn <- NA
# Format Time Series Data
time.series.data <- ts_data(nk.tweets, paste0(frequency.time))
time.series.data <- mutate(time.series.data, query = paste(nk.name))
# Tweet Frequency Line Graph
ts.frequency.line <- ggplot(time.series.data, aes(x = time, y = n)) +
geom_line(aes(color = query), size = 1) +
scale_color_manual(values = c("#00AFBB", "#E7B800")) +
theme_minimal() + labs(
title = paste("Frequency of Tweets for Search Term", nk.name, "by", frequency.time),
x = "Date / Time", y = "Number of Tweets", color = "Search Term") +
scale_x_datetime(date_breaks = paste0(date.breaks), date_labels = "%m/%d, %H:%M") +
theme(axis.text.x = element_text(angle = 25, vjust = 1.0, hjust = 1.0))
ts.frequency.line
if(missing(trend)) {
return(ts.frequency.line)
} else {
# Tweet Frequency Area Graph with Trend Line
return(ts.frequency.line + stat_smooth(color = "#FC4E07", fill = "#FC4E07", method = "loess"))
}
} else {
# Make New Data from Input
nk.tweets <- d
name <- dn
frequency.time <- f
date.breaks <- b
cbo.tweets <- c
cbo.name <- cn
# Format Time Series Data
nk.frequency <- ts_data(nk.tweets, paste0(frequency.time))
# Format Time Series Data
cbo.frequency <- ts_data(cbo.tweets, paste0(frequency.time))
# Combine Frequency Data
nk.ts.reshape <- mutate(nk.frequency, query = paste(nk.name))
cbo.ts.reshape <- mutate(cbo.frequency, query = paste(cbo.name))
ts.frequency.combined <- rbind(nk.ts.reshape, cbo.ts.reshape)
# Tweet Frequency Line Graph
ts.frequency.line.combined <- ggplot(ts.frequency.combined, aes(x = time, y = n)) +
geom_line(aes(color = query), size = 1) +
scale_color_manual(values = c("#00AFBB", "#E7B800")) +
theme_minimal() + labs(
title = paste("Tweet Frequency for Search Terms", nk.name, "&", cbo.name, "by", frequency.time),
x = "Date / Time", y = "Number of Tweets", color = "Search Terms")
ts.frequency.line.combined
if(missing(trend)) {
return(ts.frequency.line.combined)
} else {
# Tweet Frequency Line Graph with Trend Line
return(ts.frequency.line.combined + stat_smooth(color = "#FC4E07", fill = "#FC4E07", method = "loess"))
}
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.