Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small mistake in one of the sample code (sentiment) #12

Open
AnttiRask opened this issue Aug 25, 2023 · 0 comments
Open

Small mistake in one of the sample code (sentiment) #12

AnttiRask opened this issue Aug 25, 2023 · 0 comments

Comments

@AnttiRask
Copy link

AnttiRask commented Aug 25, 2023

Old code:

[library](https://www.rdocumentation.org/packages/base/topics/library)(paletteer)
sentiment_types <- sentiments %>%
  [filter](https://www.rdocumentation.org/packages/stats/topics/filter)(lexicon == "nrc") %>%
  pull(sentiment) %>%
  [unique](https://www.rdocumentation.org/packages/base/topics/unique)()

prebuild <- [imap_dfr](https://purrr.tidyverse.org/reference/imap.html)(sentiment_types,
  ~ [ggpage_build](https://emilhvitfeldt.github.io/ggpage/reference/ggpage_build.html)(tinderbox) %>%
  left_join([filter](https://www.rdocumentation.org/packages/stats/topics/filter)([get_sentiments](https://www.rdocumentation.org/packages/tidytext/topics/get_sentiments)("nrc"), sentiment == .x), by = "word") %>%
    mutate(sentiment_state = .x,
           score = [as.numeric](https://www.rdocumentation.org/packages/base/topics/numeric)(![is.na](https://www.rdocumentation.org/packages/base/topics/NA)(sentiment)),
           score_smooth = zoo::[rollmean](https://www.rdocumentation.org/packages/zoo/topics/rollmean)(score, 5, 0)))

prebuild %>% 
  [ggpage_plot](https://emilhvitfeldt.github.io/ggpage/reference/ggpage_plot.html)(aes(fill = score_smooth), page.number = "top-left") +
  paletteer::[scale_fill_paletteer_c](https://www.rdocumentation.org/packages/paletteer/topics/ggplot2-scales-continuous)(viridis, cividis, direction = -1) +
  guides(fill = "none") +
  [transition_states](https://www.rdocumentation.org/packages/gganimate/topics/transition_states)(
    sentiment_state,
    transition_length = 10,
    state_length = 3
    ) +
  labs(title = "Sections with a sentiment of {closest_state}\nIn H.C. Andersen's Tinderbox")

Updated code:

library(paletteer)
sentiment_types <- get_sentiments("nrc") %>% 
    pull(sentiment) %>%
    unique()

prebuild <- imap_dfr(sentiment_types,
                     ~ ggpage_build(tinderbox) %>%
                         left_join(filter(get_sentiments("nrc"), sentiment == .x), by = "word") %>%
                         mutate(sentiment_state = .x,
                                score = as.numeric(!is.na(sentiment)),
                                score_smooth = zoo::rollmean(score, 5, 0)))

prebuild %>% 
    ggpage_plot(aes(fill = score_smooth), page.number = "top-left") +
    paletteer::scale_fill_paletteer_c("viridis::viridis", "viridis::cividis", direction = -1) +
    guides(fill = "none") +
    transition_states(
        sentiment_state,
        transition_length = 10,
        state_length = 3
    ) +
    labs(title = "Sections with a sentiment of {closest_state}\nIn H.C. Andersen's Tinderbox")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant