For the second problem, I think it’s just a typo. The code names the variables in SentimentScores as Sentiment and Scores, but in the ggplot call, the x variable is given as sentiment (all lowercase). R is case sensitive, so x needs to be specified as Sentiment, instead.
For the first problem, this will be a lot easier to solve if helpers can run the code and experiment with it. Can you turn this into a self-contained, reproducible example? You’re pretty close — the additional components you’d need to supply are:
sample data
the relevant library() calls
See the link above for lots more help on turning your problem into a reproducible example. It’s the single best thing you can do to make getting an answer more likely!
I don't think you need to load all those packages (at least, not for the snippets of code you've posted so far). You only need to load the packages that contain the functions you are actually directly using in your code.
It's still hard to understand what's going wrong, because I can't try out your code without at least a sample of your data. This code is referencing two different data sets: SentimentScores and some_txt6. Can you provide a sample of each?
In the meantime, your featurePlot seems to have the same typo as in the first post — SentimentScores$sentiment instead of SentimentScores$Sentiment. Does making that change help at all?
Hi jcblum,
Thanks again for your continued response... this is my code right after creating a scv file. most of it is without error when I run, but still am unable see the plotted image...
## Wordcorpus- (Bag-of-words)
# Load the data as a corpus
some_txt6 <- Corpus(VectorSource(some_txt5))
toSpace <- content_transformer(function (x , pattern ) gsub(pattern, " ", x))
cleancorpus <- function(some_txt6){
some_txt6 <- tm_map(some_txt6, removePunctuation)
some_txt6 <- tm_map(some_txt6, content_transformer(tolower))
some_txt6 <- tm_map(some_txt6, removeWords, stopwords("english"))
some_txt6 <- tm_map(some_txt6, stripWhitespace)
some_txt6 <- tm_map(some_txt6, stemDocument)
return(some_txt6)
}
# Build a term-document matrix
dtm <- TermDocumentMatrix(some_txt6)
m <- as.matrix(dtm)
v <- sort(rowSums(m),decreasing=TRUE)
d <- data.frame(word = names(v),freq=v)
head(d, 10)
## the result
str(dtm)
## Wordcloud
library(tm)
library(SnowballC)
library(wordcloud)
library(RColorBrewer)
pal <- brewer.pal(8, "Dark2")
png("wordcloud_packages.png", width=1500,height=1500)
wordcloud(some_txt6, min.freq = 5, max.words=Inf, random.order=FALSE, rot.per=0.35,
colors=pal)
## Sentiment Analysis
library(ggplot2)
library(syuzhet)
# Build a term-document matrix -2-
mysentiment <- get_nrc_sentiment(some_txt5)
SentimentScores <- data.frame(colSums(mysentiment[,]))
names(SentimentScores) <- "Score"
SentimentScores <- cbind("sentiment" = rownames(SentimentScores), SentimentScores)
rownames(SentimentScores) <- NULL
# plot Sentiment Score data on a graph
#(working but not plotting image)
ggplot(SentimentScores, aes(x = factor(Sentiment), y = Score, fill = factor(Sentiment))) +
geom_bar(stat="identity", width = 0.7) +
labs(x = "Sentiment", y = "Score", fill = "Score") +
theme_minimal(base_size = 14)
# plot Sentiment Score data on a graph (not working)
ggplot(data = SentimentScores, aes(x = Sentiment, y=Score)) +
geom_bar(aes(fill = Sentiment), stat = "identity") +
theme(legend.position = "none") +
xlab("Sentiment") + ylab("Score") +
ggtitle("Total Sentiment Score Based on Tweets")