Skip to main content
Biology LibreTexts

R Practice: Building Interdisciplinary Skillsets to Understand Environmental Attitudes (Part II: Sentiment Analysis)

  • Page ID
    101342
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

     

    Technical Learning Objective: In this module, students will learn how to run a Sentiment Analysis for textual data (a "corpus") in R.

     

     

    Sentiment Analysis of Silent Spring

    This is the second part of a two-part analysis. Please see Part I: Word Cloud prior to completing this activity. 

     

    As a reminder, in the first part of this module we looked at a word cloud of the impactful book "Silent Spring", by Rachel Carson. In this follow-up module, we will run another common analysis used for textual data - a sentiment analysis.  Sentiment analysis is used to determine the "tone" of a corpus, whether it is positive, negative, or neutral. 

     

    Loading Packages and Data  

    As always, we first need to install and load all necessary packages and data. 

    # Install
    install.packages("syuzhet") # for sentiment analysis - this specialized package needs to be installed, while the other commonly used packages are already installed on Libretexts
    
    # Load
    library("RColorBrewer")
    library("syuzhet")
    library("ggplot2")
    
    # here we are loading in the text file from a url, as the file has been uploaded to Libretexts
    text <- readLines(url("https://bio.libretexts.org/@api/deki/files/66200/SilentSpring.txt?origin=mt-web"))

     

     

    Exploring Emotional Sentiments in Silent Spring 

    Now, let's use the get_ncr_sentiment command, a specialized function in the syuzhet package, which will tell us which sentiments are tracked in our textual data. The head command allows us to explore the first 10 rows of data for the emotional sentiment in Silent Spring. Run the code below to figure out what emotions are classified by this package.

    # run nrc sentiment analysis to return data frame with each row classified as one of the following emotions, rather than a score: 
    d<-get_nrc_sentiment(text)
    
    # head(d,10) - to see top 10 lines of the get_nrc_sentiment dataframe, or display the emotions directly
    head (d,10)
    
    # [fill in the emotions here, there should be 8]

     

    Visualizing Emotional Sentiments in Silent Spring 

    Now we can visualize the sentiment frequency with a bar graph. Pay attention to the perceived sentiment values for each emotion. What conclusions can you draw from the graph? Where can you see some of this code being applicable in other fields?

    #transpose the text file with the data frame for the calculated sentiment.
    td<-data.frame(t(d))
    
    #The function rowSums computes column sums across rows for each level of a grouping variable.
    td_new <- data.frame(rowSums(td[2:253]))
    
    #Transformation and cleaning the data to make it easier to graph
    names(td_new)[1] <- "count" #here we are labeling the new data frame with a sting
    td_new <- cbind("sentiment" = rownames(td_new), td_new) # here we are adding the sentiments as names to be displayed with the bar graph
    rownames(td_new) <- NULL 
    td_new2<-td_new[1:8,] #Here we are including the sentiments into the new graphable data set.
    
    #Plot One - count of words associated with each sentiment
    quickplot(sentiment, data=td_new2, weight=count, geom="bar", fill=sentiment, ylab="count")+ggtitle("Before Silent Spring sentiments")

     

    Food for Thought

    What sentiments are most common in Silent Spring? What does this tell you about the approach taken by Rachel Carson?

     

    Sources

    Carson, R. (1962). Silent Spring. Crest Book. Mhatre, S., Sampaio, J., Torres, D., &amp; Abhishek, K. (2021, September 15). Text mining and sentiment analysis: Analysis with R. Simple Talk. Retrieved November 18, 2022, from https://www.red-gate.com/simple-talk...alysis-with-r/ 

    • Was this article helpful?