Skip to contents

This function plots the average sentiment score over time.

Usage

sentiment_trend(sentiment_df, time_period = "month", show_smooth_trend = FALSE)

Arguments

sentiment_df

A data frame containing the output from analyze_sentiment.

time_period

A string specifying the time period for grouping ("day", "week", "month", "year").

show_smooth_trend

A logical value indicating whether to show the overall smooth trend line (default: TRUE).

Value

A ggplot object representing the sentiment trend.

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-10-25 03:03:00.108677 scrape_goodreads_reviews: Completed! All book reviews extracted
#> Scraping run time = 7.25992584228516
#> 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
  senti_trend <- sentiment_trend(sentiment_results)

  # Display the plot
  print(senti_trend)

  # Optionally, save the plot
  # ggsave("senti_trend.png", senti_trend, width = 8, height = 6)
} else {
  cat("No reviews found. Cannot create sentiment trend\n")
}
#> Warning: Removed 4 rows containing missing values or values outside the scale range
#> (`geom_point()`).


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