#' @title Frequency Graph: Area 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.area()
ts.frequency.area <- 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 Area Graph
ts.frequency.area <- ggplot(time.series.data, aes(x = time, y = n)) +
geom_area(aes(color = query, fill = query), alpha = 0.5, position = position_dodge(0.8)) +
scale_color_manual(values = c("#00AFBB", "#E7B800"), name = "Search Term") +
scale_fill_manual(values = c("#00AFBB", "#E7B800"), name = "Search Term") + labs(
title = paste("Frequency of Tweets for Search Term", nk.name, "by", frequency.time),
x = "Date / Time", y = "Number of Tweets") +
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.area
if(missing(trend)) {
return(ts.frequency.area)
} else {
# Tweet Frequency Area Graph with Trend Line
return(ts.frequency.area + 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
time.series.data <- ts_data(nk.tweets, paste0(frequency.time))
time.series.data <- mutate(time.series.data, query = paste(nk.name))
# Format Time Series Data
cbo.freq.primus <- ts_data(cbo.tweets, paste0(frequency.time))
cbo.frequency <- mutate(cbo.freq.primus, query = paste(cbo.name))
# Combine Frequency Data
time.series.data.reshape <- mutate(time.series.data, query = paste(nk.name))
cbo.frequency.reshape <- mutate(cbo.frequency, query = paste(cbo.name))
frequency.combined <- rbind(time.series.data.reshape, cbo.frequency.reshape)
# Combined Tweet Frequency Area Graph
ts.frequency.area.combined <- ggplot(frequency.combined, aes(x = time, y = n)) +
geom_area(aes(color = query, fill = query), alpha = 0.5, position = position_dodge(0.8)) +
scale_color_manual(values = c("#00AFBB", "#E7B800"), name = "Search Term") +
scale_fill_manual(values = c("#00AFBB", "#E7B800"), name = "Search Term") + labs(
title = paste("Tweet Frequency for Search Terms", nk.name, "&", cbo.name, "by", frequency.time),
x = "Date / Time", y = "Number of Tweets")
ts.frequency.area.combined
if(missing(trend)) {
return(ts.frequency.area.combined)
} else {
# Combined Tweet Frequency Area Graph with Trend Line
return(ts.frequency.area.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.