Home > Net >  Twitter Data Sentiment Analysis
Twitter Data Sentiment Analysis

Time:05-20

I'm very novice so, apologies if my question is trivial. I am trying to do sentiment analysis on some twitter data I downloaded but am having trouble. I am trying to follow this example:

enter image description here

which creates a bar plot that shows positive/negative sentiment counts. The code for the example is here**

original_books %>% 

  unnest_tokens(output = word,input = text) %>%   

  inner_join(get_sentiments("bing")) %>%  
  count(book, index, sentiment) %>% 

  pivot_wider(names_from = sentiment,
              values_from = n) %>% 

  mutate(sent_score = positive - negative) %>% 

  ggplot()   

  geom_col(aes(x = index, y = sent_score,
               fill = book),
           show.legend = F)  

  facet_wrap(~book,scales = "free_x")

Here is the code I have so far for my own analysis:

#twitter scraping
ref <- search_tweets(
  "#refugee", n = 18000, include_rts = FALSE,lang = "en"
)

 
data(stop_words)


new_stops <- tibble(word = c("https", "t.co", "1", "refugee", "#refugee", "amp", "refugees",
                             "day", "2022", "dont", "0", "2", "@refugees", "4", "2021") ,lexicon = "sabs")
 
full_stop <- stop_words %>% 
  bind_rows(new_stops) #bind_rows adds more rows (way to merge data)

Now I want to make a bar graph similar to the one above but I get an error because I don't have a column called "index." I tried making one but it didn't work. Here is the code I am trying to use:

ref %>% 

  unnest_tokens(word,text,token = "tweets") %>% 

  anti_join(full_stop) %>% 

  inner_join(get_sentiments("bing")) %>% 

  count(word, index, sentiment) %>% 

  pivot_wider(names_from = sentiment,
              values_from = n) %>% 

  mutate(sent_score = positive - negative) %>% 

  ggplot()   #plot the overall sentiment (pos - neg) versus index, 
  geom_col(aes(x = index, y = sent_score), show.legend = F) 

Here is an image of the error

enter image description here

Any suggestions are really appreciated! Thank you

Contents of ref Resulting figure with a bar for each of the tweets and its associated sentiment score.

  • Related