ggpage
ggpage copied to clipboard
Small mistake in one of the sample code (sentiment)
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")