Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(tidytext)
- library(wordcloud)
- library(gutenbergr)
- library(tidyverse)
- # The Time Machine from Gutenberg
- tidy_hgwells <- gutenberg_download(c(35)) %>%
- unnest_tokens(word, text) %>%
- anti_join(stop_words) %>%
- count(word)
- # Wordcloud
- tidy_hgwells %>%
- with(wordcloud(word, n, max.words = 100))
- # 1.- Barchart
- tidy_hgwells %>%
- top_n(20) %>%
- arrange(desc(n)) %>%
- ggplot(aes(as.factor(reorder(word, n)),n)) +
- geom_col() +
- coord_flip() +
- scale_colour_brewer(palette="BuPu", direction=1) +
- labs(x = "Term",y = "Frequency", label= "Frequency")
- # 2.- Doughnut chart
- library(plotly)
- tidy_hgwells %>%
- top_n(20) %>%
- arrange(desc(n)) %>%
- plot_ly(labels = ~word, values = ~n, textposition = "outside",
- marker = list(color = brewer.pal(20, "Blues")),
- textinfo = 'label+percent', colors = n) %>%
- add_pie(hole = 0.6) %>%
- layout(showlegend = F)
- # 3.- Treemap
- library(treemap)
- tidy_hgwells %>%
- top_n(15) %>%
- treemap(index = "word",vSize = "n", type = "index", title = "")
- # 4.- Circle Packing
- library(packcircles)
- data <- tidy_hgwells %>%
- top_n(15) %>%
- arrange(desc(n))
- packing <- circleProgressiveLayout(data$n, sizetype = 'area')
- data <- cbind(data, packing)
- dat.gg <- circleLayoutVertices(packing, npoints = 50)
- ggplot() +
- geom_polygon(data = dat.gg, aes(x, y, group = id, fill=as.factor(id)),
- colour = "black", alpha = 0.6) +
- geom_text(data = data, aes(x, y, label = word)) +
- scale_size_continuous(range = c(1,4)) +
- theme_void() +
- theme(legend.position = "none") +
- coord_equal()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement