Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. library(tidytext)
  2. library(wordcloud)
  3. library(gutenbergr)
  4. library(tidyverse)
  5.  
  6. # The Time Machine from Gutenberg
  7. tidy_hgwells <- gutenberg_download(c(35)) %>%
  8. unnest_tokens(word, text) %>%
  9. anti_join(stop_words) %>%
  10. count(word)
  11.  
  12. # Wordcloud
  13. tidy_hgwells %>%
  14. with(wordcloud(word, n, max.words = 100))
  15.  
  16. # 1.- Barchart
  17. tidy_hgwells %>%
  18. top_n(20) %>%
  19. arrange(desc(n)) %>%
  20. ggplot(aes(as.factor(reorder(word, n)),n)) +
  21. geom_col() +
  22. coord_flip() +
  23. scale_colour_brewer(palette="BuPu", direction=1) +
  24. labs(x = "Term",y = "Frequency", label= "Frequency")
  25.  
  26. # 2.- Doughnut chart
  27. library(plotly)
  28. tidy_hgwells %>%
  29. top_n(20) %>%
  30. arrange(desc(n)) %>%
  31. plot_ly(labels = ~word, values = ~n, textposition = "outside",
  32. marker = list(color = brewer.pal(20, "Blues")),
  33. textinfo = 'label+percent', colors = n) %>%
  34. add_pie(hole = 0.6) %>%
  35. layout(showlegend = F)
  36.  
  37.  
  38. # 3.- Treemap
  39. library(treemap)
  40. tidy_hgwells %>%
  41. top_n(15) %>%
  42. treemap(index = "word",vSize = "n", type = "index", title = "")
  43.  
  44. # 4.- Circle Packing
  45. library(packcircles)
  46.  
  47. data <- tidy_hgwells %>%
  48. top_n(15) %>%
  49. arrange(desc(n))
  50.  
  51. packing <- circleProgressiveLayout(data$n, sizetype = 'area')
  52. data <- cbind(data, packing)
  53. dat.gg <- circleLayoutVertices(packing, npoints = 50)
  54.  
  55. ggplot() +
  56. geom_polygon(data = dat.gg, aes(x, y, group = id, fill=as.factor(id)),
  57. colour = "black", alpha = 0.6) +
  58. geom_text(data = data, aes(x, y, label = word)) +
  59. scale_size_continuous(range = c(1,4)) +
  60. theme_void() +
  61. theme(legend.position = "none") +
  62. coord_equal()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement