<p>I’m sure many of my fellow Mexicans will remember the historically ill-advised (to say the least) decision by our President to invite Donald Trump for a meeting.</p>
<p>Talking to some fellow colleagues, we couldn’t help but notice that <strong>maybe</strong> in another era this decision would have been good policy. The problem, some concluded, was the influence of social media today. In fact, the Trump debacle did cause outcry among leading politica voices online.</p>
<p>I wanted to investigate this further, and thankfully for me, I’ve been using <strong>R</strong> to collect tweets from a catalog of leading political personalities in Mexico for a <a href="http://numbersmart.mx/">personal business project</a>.</p>
<p>Here is a short descriptive look at what the 65 twitter accounts I’m following tweeted between August 27th and September 5th (the Donald announced his visit on August the 30th). I’m sorry I can’t share the dataset, but you get the idea with the code…</p>
<div><pre><code><span>library</span><span>(</span>dplyr<span>)</span>
<span>library</span><span>(</span>stringr<span>)</span>

<span># 42 of the 65 accounts tweeted between those dates.</span>
d <span>%>%</span> 
  summarise<span>(</span><span>&quot;n&quot;</span> <span>=</span> n_distinct<span>(</span>NOMBRE<span>))</span>
<span># n</span>
<span># 42</span></code></pre></div>
<p>We can see how mentions of trump spike just about the time it was announced…</p>
<div><pre><code>byhour <span>&lt;-</span> d <span>%>%</span> 
  mutate<span>(</span><span>&quot;MONTH&quot;</span> <span>=</span> <span>as.numeric</span><span>(</span>month<span>(</span>T_CREATED<span>)),</span> 
         <span>&quot;DAY&quot;</span> <span>=</span> <span>as.numeric</span><span>(</span>day<span>(</span>T_CREATED<span>)),</span> 
         <span>&quot;HOUR&quot;</span> <span>=</span> <span>as.numeric</span><span>(</span>hour<span>(</span>T_CREATED<span>)),</span> 
         <span>&quot;TRUMP_MENTION&quot;</span> <span>=</span> str_count<span>(</span>TXT<span>,</span> pattern <span>=</span> <span>&quot;Trump|TRUMP|trump&quot;</span><span>))</span> <span>%>%</span> 
  group_by<span>(</span>MONTH<span>,</span> DAY<span>,</span> HOUR<span>)</span> <span>%>%</span> 
  summarise<span>(</span><span>&quot;N&quot;</span> <span>=</span> n<span>(),</span> 
            <span>&quot;TRUMP_MENTIONS&quot;</span> <span>=</span> <span>sum</span><span>(</span>TRUMP_MENTION<span>))</span> <span>%>%</span>
  mutate<span>(</span><span>&quot;PCT_MENTIONS&quot;</span> <span>=</span> TRUMP_MENTIONS<span>/</span>N<span>*</span><span>100</span><span>)</span> <span>%>%</span>
  arrange<span>(</span>desc<span>(</span>MONTH<span>),</span> desc<span>(</span>DAY<span>),</span> HOUR<span>)</span> <span>%>%</span>
  mutate<span>(</span><span>&quot;CHART_DATE&quot;</span> <span>=</span> <span>as.POSIXct</span><span>(</span><span>paste0</span><span>(</span><span>&quot;2016-&quot;</span><span>,</span>MONTH<span>,</span><span>&quot;-&quot;</span><span>,</span>DAY<span>,</span><span>&quot; &quot;</span><span>,</span> HOUR<span>,</span> <span>&quot;:00&quot;</span><span>)))</span>

<span>library</span><span>(</span>ggplot2<span>)</span>  
<span>library</span><span>(</span>eem<span>)</span>
ggplot<span>(</span>byhour<span>,</span> 
       aes<span>(</span>x <span>=</span> CHART_DATE<span>,</span> 
           y <span>=</span> PCT_MENTIONS<span>))</span> <span>+</span> 
        geom_line<span>(</span>colour<span>=</span>eem_colors<span>[</span><span>1</span><span>])</span> <span>+</span> 
        theme_eem<span>()</span><span>+</span>
        labs<span>(</span>x <span>=</span> <span>&quot;Time&quot;</span><span>,</span> 
             y <span>=</span> <span>&quot;Trump mentions \n (% of Tweets)&quot;</span><span>)</span></code></pre></div>
<p></p>
<p>The peak of mentions (as a percentage of tweets) was September 1st at 6 am (75%). But it terms of amount of tweets, it is much more obvious the outcry was following the anouncement and later visit of the candidate:</p>
<div><pre><code>ggplot<span>(</span>byhour<span>,</span> 
       aes<span>(</span>x <span>=</span> CHART_DATE<span>,</span> 
           y <span>=</span> TRUMP_MENTIONS<span>))</span> <span>+</span> 
        geom_line<span>(</span>colour<span>=</span>eem_colors<span>[</span><span>1</span><span>])</span> <span>+</span> 
        theme_eem<span>()</span><span>+</span>
        labs<span>(</span>x <span>=</span> <span>&quot;Time&quot;</span><span>,</span> 
             y <span>=</span> <span>&quot;Trump mentions \n (# of Tweets)&quot;</span><span>)</span></code></pre></div>
<p></p>
<p>We can also (sort-of) identify the effect of these influencers tweeting. I’m going to add the followers, which are potential viewers, of each tweet mentioning Trump, by hour.</p>
<div><pre><code>byaudience <span>&lt;-</span> d <span>%>%</span> 
  mutate<span>(</span><span>&quot;MONTH&quot;</span> <span>=</span> <span>as.numeric</span><span>(</span>month<span>(</span>T_CREATED<span>)),</span> 
         <span>&quot;DAY&quot;</span> <span>=</span> <span>as.numeric</span><span>(</span>day<span>(</span>T_CREATED<span>)),</span> 
         <span>&quot;HOUR&quot;</span> <span>=</span> <span>as.numeric</span><span>(</span>hour<span>(</span>T_CREATED<span>)),</span> 
         <span>&quot;TRUMP_MENTION&quot;</span> <span>=</span> str_count<span>(</span>TXT<span>,</span> pattern <span>=</span> <span>&quot;Trump|TRUMP|trump&quot;</span><span>))</span> <span>%>%</span> 
  filter<span>(</span>TRUMP_MENTION <span>></span> <span>0</span><span>)</span> <span>%>%</span>
  group_by<span>(</span>MONTH<span>,</span> DAY<span>,</span> HOUR<span>)</span> <span>%>%</span> 
  summarise<span>(</span><span>&quot;TWEETS&quot;</span> <span>=</span> n<span>(),</span> 
            <span>&quot;AUDIENCE&quot;</span> <span>=</span> <span>sum</span><span>(</span>U_FOLLOWERS<span>))</span> <span>%>%</span>
  arrange<span>(</span>desc<span>(</span>MONTH<span>),</span> desc<span>(</span>DAY<span>),</span> HOUR<span>)</span> <span>%>%</span>
  mutate<span>(</span><span>&quot;CHART_DATE&quot;</span> <span>=</span> <span>as.POSIXct</span><span>(</span><span>paste0</span><span>(</span><span>&quot;2016-&quot;</span><span>,</span>MONTH<span>,</span><span>&quot;-&quot;</span><span>,</span>DAY<span>,</span><span>&quot; &quot;</span><span>,</span> HOUR<span>,</span> <span>&quot;:00&quot;</span><span>)))</span>


ggplot<span>(</span>byaudience<span>,</span> 
       aes<span>(</span>x <span>=</span> CHART_DATE<span>,</span> 
           y <span>=</span> AUDIENCE<span>))</span> <span>+</span> 
        geom_line<span>(</span>colour<span>=</span>eem_colors<span>[</span><span>1</span><span>])</span> <span>+</span> 
        theme_eem<span>()</span><span>+</span>
        labs<span>(</span>x <span>=</span> <span>&quot;Time&quot;</span><span>,</span> 
             y <span>=</span> <span>&quot;Potential audience \n (# of followers)&quot;</span><span>)</span></code></pre></div>
<p></p>
<p>So clearly, I’m stating the obvious. People were talking. But how was the conversation being developed? Let’s first see the type of tweets (RT’s vs drafted individually):</p>
<div><pre><code>bytype <span>&lt;-</span> d <span>%>%</span> 
  mutate<span>(</span><span>&quot;TRUMP_MENTION&quot;</span> <span>=</span> str_count<span>(</span>TXT<span>,</span> pattern <span>=</span> <span>&quot;Trump|TRUMP|trump&quot;</span><span>))</span> <span>%>%</span>
  <span># only the tweets that mention trump</span>
  filter<span>(</span>TRUMP_MENTION<span>></span><span>0</span><span>)</span> <span>%>%</span>
  group_by<span>(</span>T_ISRT<span>)</span> <span>%>%</span> 
  summarise<span>(</span><span>&quot;count&quot;</span> <span>=</span> n<span>())</span>
kable<span>(</span>bytype<span>)</span></code></pre></div>
<table><tr>T_ISRT
count
</tr><tr><td>FALSE</td>
<td>313</td>
</tr><tr><td>TRUE</td>
<td>164</td>
</tr></table><p>About 1 in 3 was a RT. Comparing to the overall tweets, (1389 out of 3833) this seems not too much of a difference, so it wasn’t necesarrily an influencer pushing the discourse. In terms of the most mentioned by tweet it was our President on the spotlight:</p>
<div><pre><code>bymentionchain <span>&lt;-</span> d <span>%>%</span> 
  mutate<span>(</span><span>&quot;TRUMP_MENTION&quot;</span> <span>=</span> str_count<span>(</span>TXT<span>,</span> pattern <span>=</span> <span>&quot;Trump|TRUMP|trump&quot;</span><span>))</span> <span>%>%</span>
  <span># only the tweets that mention trump</span>
  group_by<span>(</span>TRUMP_MENTION<span>,</span> MENTION_CHAIN<span>)</span> <span>%>%</span> 
  summarise<span>(</span><span>&quot;count&quot;</span> <span>=</span> n<span>())</span> <span>%>%</span> 
  ungroup<span>()</span> <span>%>%</span> 
  mutate<span>(</span><span>&quot;GROUPED_CHAIN&quot;</span> <span>=</span> <span>ifelse</span><span>(</span><span>grepl</span><span>(</span>pattern <span>=</span> <span>&quot;EPN&quot;</span><span>,</span> 
                                        x <span>=</span> MENTION_CHAIN<span>),</span> 
                                  <span>&quot;EPN&quot;</span><span>,</span> MENTION_CHAIN<span>))</span> <span>%>%</span> 
  mutate<span>(</span><span>&quot;GROUPED_CHAIN&quot;</span> <span>=</span> <span>ifelse</span><span>(</span><span>grepl</span><span>(</span>pattern <span>=</span> <span>&quot;realDonaldTrump&quot;</span><span>,</span> 
                                        x <span>=</span> MENTION_CHAIN<span>),</span> 
                                  <span>&quot;realDonaldTrump&quot;</span><span>,</span> GROUPED_CHAIN<span>))</span>
                                  
ggplot<span>(</span>order_axis<span>(</span>bymentionchain <span>%>%</span> 
                    filter<span>(</span>count<span>></span><span>10</span> <span>&amp;</span> GROUPED_CHAIN<span>!=</span><span>&quot;ND&quot;</span><span>),</span> 
                  axis <span>=</span> GROUPED_CHAIN<span>,</span> 
                  column <span>=</span> count<span>),</span> 
       aes<span>(</span>x <span>=</span> GROUPED_CHAIN_o<span>,</span> 
           y <span>=</span> count<span>))</span> <span>+</span> 
  geom_bar<span>(</span>stat <span>=</span> <span>&quot;identity&quot;</span><span>)</span> <span>+</span> 
  theme_eem<span>()</span> <span>+</span> 
  labs<span>(</span>x <span>=</span> <span>&quot;Mention chain \n (separated by _|.|_ )&quot;</span><span>,</span> y <span>=</span> <span>&quot;Tweets&quot;</span><span>)</span></code></pre></div>
<p></p>
<p>How about the actual persons who tweeted? It seemed like news anchor Joaquin Lopez-Doriga and security analyst Alejandro Hope were the most vocal about the visit (out of the influencers i’m following).</p>
<div><pre><code>bytweetstar <span>&lt;-</span> d <span>%>%</span> 
  mutate<span>(</span><span>&quot;TRUMP_MENTION&quot;</span> <span>=</span> <span>ifelse</span><span>(</span>str_count<span>(</span>TXT<span>,</span> pattern <span>=</span> <span>&quot;Trump|TRUMP|trump&quot;</span><span>)</span><span>&lt;</span><span>1</span><span>,</span><span>0</span><span>,</span><span>1</span><span>))</span> <span>%>%</span>
  group_by<span>(</span>TRUMP_MENTION<span>,</span> NOMBRE<span>)</span> <span>%>%</span> 
  summarise<span>(</span><span>&quot;count&quot;</span> <span>=</span> n_distinct<span>(</span>TXT<span>))</span>
<span>## plot with ggplot2</span></code></pre></div>
<p></p>
<p>I also grouped each person by his political affiliation and I found it confirms the notion that the conversation on the eve of the visit, at least among this <strong>very small</strong> subset of twitter accounts, was driven by those with no party afiliation or in the “PAN” (opposition party).</p>
<div><pre><code>byafiliation <span>&lt;-</span> d <span>%>%</span> 
  mutate<span>(</span><span>&quot;MONTH&quot;</span> <span>=</span> <span>as.numeric</span><span>(</span>month<span>(</span>T_CREATED<span>)),</span> 
         <span>&quot;DAY&quot;</span> <span>=</span> <span>as.numeric</span><span>(</span>day<span>(</span>T_CREATED<span>)),</span> 
         <span>&quot;HOUR&quot;</span> <span>=</span> <span>as.numeric</span><span>(</span>hour<span>(</span>T_CREATED<span>)),</span> 
         <span>&quot;TRUMP_MENTION&quot;</span> <span>=</span> <span>ifelse</span><span>(</span>str_count<span>(</span>TXT<span>,</span> pattern <span>=</span> <span>&quot;Trump|TRUMP|trump&quot;</span><span>)</span><span>></span><span>0</span><span>,</span><span>1</span><span>,</span><span>0</span><span>))</span> <span>%>%</span> 
  group_by<span>(</span>MONTH<span>,</span> DAY<span>,</span> HOUR<span>,</span> TRUMP_MENTION<span>,</span> AFILIACION<span>)</span> <span>%>%</span> 
  summarise<span>(</span><span>&quot;TWEETS&quot;</span> <span>=</span> n<span>())</span> <span>%>%</span>
  arrange<span>(</span>desc<span>(</span>MONTH<span>),</span> desc<span>(</span>DAY<span>),</span> HOUR<span>)</span> <span>%>%</span>
  mutate<span>(</span><span>&quot;CHART_DATE&quot;</span> <span>=</span> <span>as.POSIXct</span><span>(</span><span>paste0</span><span>(</span><span>&quot;2016-&quot;</span><span>,</span>MONTH<span>,</span><span>&quot;-&quot;</span><span>,</span>DAY<span>,</span><span>&quot; &quot;</span><span>,</span> HOUR<span>,</span> <span>&quot;:00&quot;</span><span>)))</span>
  
 ggplot<span>(</span>byafiliation<span>,</span> 
       aes<span>(</span>x <span>=</span> CHART_DATE<span>,</span> 
           y <span>=</span> TWEETS<span>,</span> 
           group <span>=</span> AFILIACION<span>,</span> 
           fill <span>=</span> AFILIACION<span>))</span> <span>+</span> 
  geom_bar<span>(</span>stat <span>=</span> <span>&quot;identity&quot;</span><span>)</span> <span>+</span> 
  theme_eem<span>()</span> <span>+</span> 
  scale_fill_eem<span>(</span><span>20</span><span>)</span> <span>+</span> 
  facet_grid<span>(</span>TRUMP_MENTION <span>~</span><span>.</span><span>)</span> <span>+</span>
  labs<span>(</span>x <span>=</span> <span>&quot;Time&quot;</span><span>,</span> y <span>=</span> <span>&quot;Tweets \n (By mention of Trump)&quot;</span><span>)</span></code></pre></div>
<p></p>
<p>However, It’s interesting to note how there is a small spike of the accounts afiliated with the PRI (party in power) on the day after his visit (Sept. 1st). Maybe they were trying to drive the conversation to another place?</p>