Skip to contents

This function creates a histogram of sentiment scores for all reviews.

Usage

sentiment_histogram(sentiment_df)

Arguments

sentiment_df

A data frame containing the output from analyze_sentiment.

Value

A ggplot object representing the histogram.

Examples

# \donttest{
# Create a temporary file with sample book IDs
temp_file <- tempfile(fileext = ".txt")
writeLines(c("1420", "2767052", "10210"), temp_file)

# Run the scrape_reviews function
reviews <- scrape_reviews(temp_file, num_reviews = 10, use_parallel = FALSE)
#> Total book IDs to process: 3
#> 2024-09-03 16:17:29.357793 scrape_goodreads_reviews: Completed! All book reviews extracted
#> Scraping run time = 8.48607754707336
#> Total books processed: 3

# Check if reviews were successfully scraped
if (nrow(reviews) > 0) {
  # Perform sentiment analysis
  sentiment_results <- analyze_sentiment(reviews, lexicon = "afinn")

  # Create histogram of sentiment scores
  sentiment_hist <- sentiment_histogram(sentiment_results)

  # Display the plot
  print(sentiment_hist)

  # Optionally, save the plot
  # ggsave("sentiment_hist.png", sentiment_hist, width = 8, height = 6)
} else {
  cat("No reviews found. Cannot create sentiment histogram.\n")
}
#> Warning: Removed 6 rows containing non-finite outside the scale range (`stat_bin()`).


# Clean up: remove the temporary file
file.remove(temp_file)
#> [1] TRUE
# }