Wordcloud and plotting sentiment score error

creating a wordcloud, but its not plotting and am getting this error ;

## Wordcloud code
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)
dev.off()

error:

png 
  2

am also trying to plot my sentiments graph with this code...

## Sentiment Analysis
# Build a term-document matrix -2- ON A TABLE 
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
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")

error:

Error in FUN(X[[i]], ...) : object 'sentiment' not found

please someone help me out?....

System Information:

  • RStudio Edition: (Desktop or Server)
  • RStudio Version:
  • OS Version:
  • R Version:
  • sessionInfo():

Referred here from support.rstudio.com

Hi! Welcome!

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! :grin:

2 Likes

Hi jcblum,

Thanks for your response... however, I have installed and loaded the below libraries and still am not to visualize my plots!

library(twitteR)
library(ROAuth)
library(plyr)
library(dplyr)
library(stringr)
library(ggplot2)
library(httr)
library(tm)
library(SnowballC)
library(wordcloud)
library(SentimentAnalysis)
library(RCurl)
library(syuzhet)
library(tibble)
library(corpus)
library(RColorBrewer)
library(choroplethr)
library(choroplethrMaps)
library(openintro)
library(fiftystater)
library(colorplaner)
library(e1071)
library(caret)

code example;

featurePlot(x = SentimentScores[, 1:2], 
            y = SentimentScores$sentiment, 
            plot = "pairs",
            ## Add a key at the top
            auto.key = list(columns = 2))

and...

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)

please help!

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?

1 Like

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")