<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/when-trump-visits-tweets-from-his-trip-to-mexico/"></div></div><div><a data-url="https://www.r-bloggers.com/when-trump-visits-tweets-from-his-trip-to-mexico/" data-counturl="https://www.r-bloggers.com/when-trump-visits-tweets-from-his-trip-to-mexico/" data-count="horizontal" data-text="When Trump visits… tweets from his trip to Mexico" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.enelmargen.org/datascience/trump-mextweets"> En El Margen - R-English</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<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 target="_blank" 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 data-lang="r"><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 data-lang="r">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><img alt="Trump tweets by mexican officials, percent" src="https://i2.wp.com/www.enelmargen.org/images/posts/trump_mex_plot1.png?w=456" data-recalc-dims="1"></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 data-lang="r">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><img alt="Trump tweets by mexican officials, total" src="https://i0.wp.com/www.enelmargen.org/images/posts/trump_mex_plot2.png?w=456" data-recalc-dims="1"></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 data-lang="r">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><img alt="Total audience of trump tweets" src="https://i1.wp.com/www.enelmargen.org/images/posts/trump_mex_plot4.png?w=456" data-recalc-dims="1"></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 data-lang="r">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>
<thead>
<tr>
<th>T_ISRT</th>
<th>count</th>
</tr>
</thead>
<tbody>
<tr>
<td>FALSE</td>
<td>313</td>
</tr>
<tr>
<td>TRUE</td>
<td>164</td>
</tr>
</tbody>
</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 data-lang="r">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><img alt="Mentions" src="https://i2.wp.com/www.enelmargen.org/images/posts/trump_mex_plot3.png?w=456" data-recalc-dims="1"></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 data-lang="r">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><img alt="Mentions" src="https://i1.wp.com/www.enelmargen.org/images/posts/trump_mex_plot5.png?w=456" data-recalc-dims="1"></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 data-lang="r">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><img alt="Mentions" src="https://i1.wp.com/www.enelmargen.org/images/posts/trump_mex_plot6.png?w=456" data-recalc-dims="1"></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>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/when-trump-visits-tweets-from-his-trip-to-mexico/"></div></div><div><a data-url="https://www.r-bloggers.com/when-trump-visits-tweets-from-his-trip-to-mexico/" data-counturl="https://www.r-bloggers.com/when-trump-visits-tweets-from-his-trip-to-mexico/" data-count="vertical" data-text="When Trump visits… tweets from his trip to Mexico" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.enelmargen.org/datascience/trump-mextweets"> En El Margen - R-English</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/ETVHrX7Bp4c"></a></div>