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}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

     

    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?