Collecting, Analyzing and Presenting data about the participation in #ilovefs day

plotte.R 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. ################################################################################
  2. # Copyright (c) 2018 Free Software Foundation Europe e.V. <contact@fsfe.org>
  3. # Author 2018 Jan Weymeirsch <janwey@fsfe.org>
  4. # SPDX-License-Identifier: GPL-3.0
  5. ################################################################################
  6. ## Loading Packages {{{ ----
  7. ### ggplot2 for plots
  8. install.packages("ggplot2")
  9. library("ggplot2")
  10. ### arrange ggplot2 plots
  11. install.packages("gridExtra")
  12. library("gridExtra")
  13. # }}}
  14. ## Loading Data {{{ ----
  15. load(file = "./data/ilovefs-all_2018-02-20_14-57-16.RData")
  16. ### following requires you to extract the hashtags first (see further down).
  17. ### this can later be imported via a CSV file again
  18. hash_ment <- read.csv2(file = "./data/tags_mentions.csv", sep = ",", header = FALSE)
  19. # }}}
  20. ## Extract Hashtags {{{ ----
  21. ### Fediverse
  22. mastodon_hashtags_l <- stri_extract_all(str = mastodon$text, regex = "#\\w+")
  23. mastodon_hashtags <- c()
  24. for(i in c(1:length(mastodon_hashtags_l))){
  25. for(j in c(1:length(mastodon_hashtags_l[[i]]))){
  26. mastodon_hashtags <- c(mastodon_hashtags, mastodon_hashtags_l[[i]][j])
  27. }
  28. }
  29. mastodon_hashtags <- sub(x = mastodon_hashtags, pattern = ",", replace = "")
  30. mastodon_hashtags <- sub(x = mastodon_hashtags, pattern = "\\:", replace = "")
  31. mastodon_hashtags <- sub(x = mastodon_hashtags, pattern = "\\?", replace = "")
  32. mastodon_hashtags <- sub(x = mastodon_hashtags, pattern = "\\!", replace = "")
  33. mastodon_hashtags <- sub(x = mastodon_hashtags, pattern = "…", replace = "")
  34. unique(mastodon_hashtags)
  35. ### Twitter
  36. twitter_hashtags_l <- stri_extract_all(str = twitter$text, regex = "#\\w+")
  37. twitter_hashtags <- c()
  38. for(i in c(1:length(twitter_hashtags_l))){
  39. for(j in c(1:length(twitter_hashtags_l[[i]]))){
  40. twitter_hashtags <- c(twitter_hashtags, twitter_hashtags_l[[i]][j])
  41. }
  42. }
  43. twitter_hashtags <- sub(x = twitter_hashtags, pattern = ",", replace = "")
  44. twitter_hashtags <- sub(x = twitter_hashtags, pattern = "\\:", replace = "")
  45. twitter_hashtags <- sub(x = twitter_hashtags, pattern = "\\?", replace = "")
  46. twitter_hashtags <- sub(x = twitter_hashtags, pattern = "\\!", replace = "")
  47. twitter_hashtags <- sub(x = twitter_hashtags, pattern = "…", replace = "")
  48. unique(twitter_hashtags)
  49. # }}}
  50. ## Extract Mentions {{{ ----
  51. ### Fediverse
  52. mastodon_mentions_l <- stri_extract_all(str = mastodon$text, regex = "\\@\\S+")
  53. mastodon_mentions <- c()
  54. for(i in c(1:length(mastodon_mentions_l))){
  55. for(j in c(1:length(mastodon_mentions_l[[i]]))){
  56. mastodon_mentions <- c(mastodon_mentions, mastodon_mentions_l[[i]][j])
  57. }
  58. }
  59. mastodon_mentions <- sub(x = mastodon_mentions, pattern = ",", replace = "")
  60. mastodon_mentions <- sub(x = mastodon_mentions, pattern = "\\:", replace = "")
  61. mastodon_mentions <- sub(x = mastodon_mentions, pattern = "\\?", replace = "")
  62. mastodon_mentions <- sub(x = mastodon_mentions, pattern = "\\!", replace = "")
  63. mastodon_mentions <- sub(x = mastodon_mentions, pattern = "…", replace = "")
  64. unique(mastodon_mentions)
  65. ### Twitter
  66. twitter_mentions_l <- stri_extract_all(str = twitter$text, regex = "\\@\\S+")
  67. twitter_mentions <- c()
  68. for(i in c(1:length(twitter_mentions_l))){
  69. for(j in c(1:length(twitter_mentions_l[[i]]))){
  70. twitter_mentions <- c(twitter_mentions, twitter_mentions_l[[i]][j])
  71. }
  72. }
  73. twitter_mentions <- sub(x = twitter_mentions, pattern = ",", replace = "")
  74. twitter_mentions <- sub(x = twitter_mentions, pattern = "\\:", replace = "")
  75. twitter_mentions <- sub(x = twitter_mentions, pattern = "\\?", replace = "")
  76. twitter_mentions <- sub(x = twitter_mentions, pattern = "\\!", replace = "")
  77. twitter_mentions <- sub(x = twitter_mentions, pattern = "…", replace = "")
  78. unique(twitter_mentions)
  79. # }}}
  80. ### Participation per Platform {{{ ----
  81. #### Calculating Platform numbers
  82. ##### Platform (Twitter/Fediverse)
  83. twitter_number <- rep(x = "twitter", times = length(twitter$text))
  84. fediver_number <- rep(x = "fediverse", times = length(mastodon$text))
  85. platform <- factor(c(twitter_number, fediver_number), levels = c("fediverse", "twitter"))
  86. ##### Instances (Fediverse)
  87. msoc <- grep(x = as.character(mastodon$acct), pattern = "@", invert = TRUE)
  88. instances <- sub(x = as.character(mastodon$acct), pattern = ".*\\@", replace = "")
  89. instances[msoc] <- "mastodon.social"
  90. instances <- as.factor(instances)
  91. #### Plotting the results
  92. pdf(file = "./plots/participation_platform.pdf", height = 10, width = 20)
  93. par(mfrow = c(1,2))
  94. ##### Platform (Twitter/Fediverse)
  95. plot(x = platform,
  96. col = c("#a22430", "#1da1f2"),
  97. ylim = c(0, ceiling(max(table(platform))/100) * 100),
  98. main = "#ilovefs participation by Platform",
  99. names.arg = c("The Fediverse", "Twitter"))
  100. ##### Instances (Fediverse)
  101. plot(x = instances,
  102. horiz = FALSE,
  103. col = rainbow(n = length(unique(instances))),
  104. ylim = c(0, ceiling(max(table(instances))/10) * 10),
  105. main = "#ilovefs participation by Instance",
  106. names.arg = "")
  107. legend(x = "topleft", legend = levels(instances),
  108. fill = rainbow(n = length(unique(instances))),
  109. bty = "n", ncol = 2)
  110. dev.off()
  111. # }}}
  112. ### Time and Date {{{ ----
  113. #### Transform time into POSIX dates
  114. twitter_time <- strptime(paste0(twitter$date, twitter$time),
  115. format = "%Y%m%d%H%M%S")
  116. mastodon_time <- strptime(paste0(mastodon$date, mastodon$time),
  117. format = "%Y%m%d%H%M%S")
  118. #### Participation by Time on Twitter
  119. twitter_plot <- ggplot(data = twitter, aes(x=twitter_time)) +
  120. geom_histogram(aes(fill=..count..), binwidth=60*180) +
  121. scale_x_datetime("Date") +
  122. scale_y_continuous("Frequency") +
  123. ggtitle("Participation on Twitter") +
  124. scale_fill_gradient("Count", low="#002864", high="#329cc3")
  125. #### Participation by Time in the Fediverse
  126. mastodon_plot <- ggplot(data = mastodon, aes(x=mastodon_time)) +
  127. geom_histogram(aes(fill=..count..), binwidth=60*180) +
  128. scale_x_datetime("Date") +
  129. scale_y_continuous("Frequency") +
  130. ggtitle("Participation in the Fediverse") +
  131. scale_fill_gradient("Count", low="#640000", high="#FF0000")
  132. #### Export / Save plots as PDF
  133. pdf(file="./plots/ilfs-participation-by-date.pdf", width=14, height=7)
  134. grid.arrange(twitter_plot, mastodon_plot, ncol = 2)
  135. dev.off()
  136. # }}}