-
Notifications
You must be signed in to change notification settings - Fork 27
added chapter (provisionally 23) on time patterns and author note #8
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
Open
thoughtfulbloke
wants to merge
1
commit into
hrbrmstr:master
Choose a base branch
from
thoughtfulbloke:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| --- | ||
| output: | ||
| word_document: default | ||
| html_document: default | ||
| --- | ||
| # Visualizing a Graph of Retweet Relationships | ||
|
|
||
| ## Problem | ||
| You want to visualize the daily and weekly activity pattern of a set of tweets. | ||
|
|
||
| ## Solution | ||
|
|
||
| As time is cyclical as well is linear, as solution to display tweets within cyclic periods involves converting the tweet time information from a linear to a cyclic perspective. | ||
|
|
||
| ```{r 23_lib, message=FALSE, warning=FALSE} | ||
| library(rtweet) | ||
| library(dplyr) | ||
| library(lubridate) | ||
| library(ggplot2) | ||
| library(scales) | ||
| ``` | ||
|
|
||
| First, we gather tweets for an account | ||
|
|
||
| ```{r 23_collect, message=FALSE, warning=FALSE, cache=TRUE} | ||
| examplee <- "thoughtfulnz" | ||
| twitterings <- get_timeline(examplee, n = 3200) | ||
| ``` | ||
|
|
||
| If the tweets gathered are from the same timezone, we get a clearer picture of daily patterns by converting to local time. Twitter marks the creation time in UTC, and if a region observes Daylight Savings, then this leads twitter activity suddenly being displaced by one hour on two occasions during the year. When viewed in aggregate this blurs sharp distinctions in the data. | ||
|
|
||
| ```{r 23_localtz, message=FALSE, warning=FALSE, cache=TRUE} | ||
| local_tz = "Pacific/Auckland" | ||
| ``` | ||
|
|
||
| To turn linear time into cyclic time, we assign the hour, minute, and second the tweet took place to the same (arbitrary) day | ||
|
|
||
| ```{r 23_cycles, message=FALSE, warning=FALSE, cache=TRUE} | ||
| cyclic <- twitterings %>% | ||
| mutate(local_at = with_tz(created_at, local_tz), | ||
| single_day = ISOdatetime(2018, 5, 12, | ||
| hour(local_at), minute(local_at), | ||
| second(local_at), tz=local_tz), | ||
| week_day = wday(local_at, label=TRUE, abbr=TRUE, week_start = 1)) | ||
| ``` | ||
|
|
||
| Note: setting the start date of the week is available in lubridate 1.7.1 (which I used) and more recent, in older versions you may need to to make sure week_day is a factor (ordered category) and set the desired order. | ||
|
|
||
| For a graph, we are representing the patterns as a simple cloud of points through the day, each day rising as a vertical column beginning with (local) midnight. | ||
|
|
||
| ```{r 23_graph, message=FALSE, warning=FALSE, cache=TRUE, fig.width=8, fig.height=6} | ||
| ggplot(cyclic, aes(x=week_day, y= single_day)) + | ||
| geom_jitter(width = 0.1, alpha=0.3, size=0.3) + theme_minimal() + | ||
| scale_y_datetime(labels = date_format("%H:%M", tz=local_tz)) + | ||
| ylab("Time of Day") + xlab("Day of Week") + | ||
| ggtitle(paste("Twitter activity,", local_tz, "timezone")) + | ||
| theme(legend.position="none") | ||
| ``` | ||
|
|
||
| Used for good, cyclic time can provide evidence of bots and fraudulent accounts. But, should you apply it to your own account, you will get a graphical demonstration of the amount of information you leak when small pieces are viewed in aggregate. Peaks, lulls, gaps when others do not have them, and lack of gaps when other accounts are silent are all records of how you go about your daily activities. Tiny records, but they add up. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you be amenable to adding a
library(ggbeeswarm)and adding another graph plot section showing it? (i.e. sameggplot()chain but usinggeom_quasirandom(width = 0.1, alpha=0.3, size=0.3)?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perhaps even a
width = 0.3for that quasirandom suggestionThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That visualisation looks great. Not one I had come across before, but that is part of the awesomeness of the sharing of the community & the way new perspectives show what is possible.