#' @title rSentiment Barchart
#' @description Blank Description.
#' @param s Sentiment Data
#' @param n Name
#' Defaults to TRUE.
#' @keywords package
#' @return NULL
#' @export
#' @examples sentiment.barchart(nk.sentiment, nk.name, cbo.sentiment, cbo.name)
#' sentiment.barchart(nk.sentiment, nk.name) | sentiment.barchart(cbo.sentiment, cbo.name)
sentiment.barchart <- function(s, sn, c, cn) {
if('dplyr' %in% rownames(installed.packages()) == TRUE) {
require(dplyr)} else {
install.packages("dplyr", repos = "http://cran.us.r-project.org")
require(dplyr)}
if('reshape' %in% rownames(installed.packages()) == TRUE) {
require(reshape)} else {
install.packages("reshape", repos = "http://cran.us.r-project.org")
require(reshape)}
if('ggplot2' %in% rownames(installed.packages()) == TRUE) {
require(ggplot2)} else {
install.packages("ggplot2", repos = "http://cran.us.r-project.org")
require(ggplot2)}
if(missing(c)) {
nk.sentiment <- s
nk.name <- sn
c <- NA
cn <- NA
nk.reshape <- reshape2::melt(nk.sentiment,
variable.name = "emotion",
value.name = "sentiment",
id.vars = c("created_at", "favorite_count", "retweet_count","text"))
nk.reshape <- mutate(nk.reshape, query = paste(nk.name))
nk.barshape <- nk.reshape %>%
group_by(emotion) %>%
summarise(sentiment = sum(sentiment))
nk.barchart <- ggplot(data = nk.barshape, mapping = aes(x = emotion, y = sentiment))
nk.barchart <- nk.barchart +
geom_bar(aes(fill = emotion), stat = "identity", position = "dodge", show.legend = FALSE)
nk.barchart <- nk.barchart + theme_minimal() +
theme(legend.position = "bottom") +
labs(title = paste(nk.name, "NRC Sentiment Barchart"),
y = "Total Sentiment", x = "Emotion") +
geom_text(aes(label = nk.barshape$sentiment), position = position_dodge(width = 0.75), vjust = -0.25)
nk.barchart
return(nk.barchart)
} else {
nk.sentiment <- s
nk.name <- sn
cbo.sentiment <- c
cbo.name <- cn
nk.reshape <- reshape2::melt(nk.sentiment,
variable.name = "emotion",
value.name = "sentiment",
id.vars = c("created_at", "favorite_count", "retweet_count","text"))
nk.reshape <- mutate(nk.reshape, query = paste(nk.name))
nk.barshape <- nk.reshape %>%
group_by(emotion) %>%
summarise(sentiment = sum(sentiment))
cbo.reshape <- reshape2::melt(cbo.sentiment,
variable.name = "emotion",
value.name = "sentiment",
id.vars = c("created_at", "favorite_count", "retweet_count","text"))
cbo.reshape <- mutate(cbo.reshape, query = paste(cbo.name))
cbo.barshape <- cbo.reshape %>%
group_by(emotion) %>%
summarise(sentiment = sum(sentiment))
# Sentiment Comparison Barchart
nk.mutate <- mutate(nk.barshape, query = paste(nk.name))
cbo.mutate <- mutate(cbo.barshape, query = paste(cbo.name))
both.querys <- rbind(nk.mutate, cbo.mutate)
both.querys.aggr <- both.querys %>%
group_by(query, emotion) %>%
summarise(sentiment = sum(sentiment))
sentiment.comparison <- ggplot(data = both.querys.aggr, mapping = aes(x = emotion, y = sentiment, fill = query)) +
geom_bar(stat = "identity", position = "dodge") + theme_minimal() + theme(legend.position = "bottom") +
labs(title = paste0("NRC Sentiment Comparison for Search Terms ", nk.name, " and " , cbo.name), y = "Total Sentiment", x = "Emotion") +
scale_fill_discrete(name = "Search Terms", breaks=c(paste(nk.name), paste(cbo.name)), labels=c(paste(nk.name), paste(cbo.name))) +
geom_text(aes(label = both.querys.aggr$sentiment), position = position_dodge(width = 0.95), vjust = -1, size = 3)
sentiment.comparison
return(sentiment.comparison)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.