<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/dplyr-and-zika-epilogue/"></div></div><div><a data-url="http://www.r-bloggers.com/dplyr-and-zika-epilogue/" data-counturl="http://www.r-bloggers.com/dplyr-and-zika-epilogue/" data-count="horizontal" data-text="dplyr and Zika – Epilogue" 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="https://rollingyours.wordpress.com/2016/07/19/dplyr-and-zika-epilogue/"> Rolling Your Rs</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<div data-shortcode="caption" class="wp-caption"><a target="_blank" href="http://i0.wp.com/rollingyours.files.wordpress.com/2016/07/screenshot_775.png?ssl=1"><img alt="HexBin Chart of Zika Cases in Brazil" class="wp-image-1651" src="http://i0.wp.com/rollingyours.files.wordpress.com/2016/07/screenshot_775.png?resize=300%2C260&ssl=1" data-recalc-dims="1"></a>
<p class="wp-caption-text">HexBin Chart of Zika Cases in Brazil</p>
</div>
<p>I really thought I was done with the Express dplyr series though on completion of the <a target="_blank" href="https://rollingyours.wordpress.com/2016/07/07/express-dplyr-part-ii/">second part</a> I received many messages requesting more examples of using dplyr with ggplot along with some other types of information such as the Zika virus data which can be <a target="_blank" href="https://github.com/cdcepi/zika">downloaded from Github</a>. These examples are not drastically different from previous examples although they allow me to show how each data set presents its own challenges and also how transformations to the data will be necessary to visualize  the information.  The Zika data is spread across a number of countries but we will focus uniquely on cases in Brazil. Download the data using a Git client or <a target="_blank" href="https://github.com/cdcepi/zika/tree/master/Brazil">straight from the site itself.</a> If you are not using Git then you are missing out. (See my <a target="_blank" href="https://www.youtube.com/playlist?list=PL905DXZOAgww-j4-lJ6hDy1ykfymnHxKX">Youtube playlist</a> for a basic introduction). It’s a great way to manage your R code and larger projects. Also keep in mind that the <a target="_blank" href="https://www.rstudio.com/">RStudio IDE</a> has built in Git capabilities so as you create software it is extremely easy to “push” changes to your reference code and equally as easy for users of your code to “pull” changes down.</p>
<h4>Processing the Data</h4>
<p><a target="_blank" href="http://i0.wp.com/rollingyours.files.wordpress.com/2016/07/brazil_data.png?ssl=1"><img alt="Brazil Zika Data" class="wp-image-1583" src="http://i0.wp.com/rollingyours.files.wordpress.com/2016/07/brazil_data.png?resize=300%2C279&ssl=1" data-recalc-dims="1"></a>Okay we have seven .CSV files each of which represents some Zika information on the given date. (Note that there might be more files that have been added since I wrote this post). The format of the data isn’t particularly complicated although it isn’t standard in the sense that each row in a file represents the same type of information.  In general you never really know what the data contains (or not) until you start working with it. There are a number of ways to approach this scenario. Ideally if there is a code book that explains the format then start there although it turns out that this data is simple enough to figure out. We could read in the data for a specific date, e.g. 2016-04-02.csv, but my inclination is to read in all the files and bind them together into a single data frame so I will have all that I need in one spot. This isn’t necessary but since the data itself is not very large then this is a reasonable approach.</p>
<pre>
library(dplyr)

# You will need to specify your own path to reflect your download location

path &lt;- &quot;/Users/fender/Downloads/zika/Brazil/Epidemiological_Bulletin/data&quot;
setwd(path)

temp_files &lt;- list.files(pattern=&quot;.csv&quot;)
temp_files
[1] &quot;Epidemiological_Bulletin-2016-04-02.csv&quot; &quot;Epidemiological_Bulletin-2016-04-23.csv&quot;
[3] &quot;Epidemiological_Bulletin-2016-04-30.csv&quot; &quot;Epidemiological_Bulletin-2016-05-07.csv&quot;
[5] &quot;Epidemiological_Bulletin-2016-05-14.csv&quot; &quot;Epidemiological_Bulletin-2016-05-21.csv&quot;
[7] &quot;Epidemiological_Bulletin-2016-05-28.csv&quot;

# Next we read all seven files the contents of each will go into a list
# element

myfiles &lt;- lapply(temp_files,read.csv,stringsAsFactors=FALSE) 

str(myfiles,1)
List of 7
 $ :'data.frame':    33 obs. of  9 variables:
 $ :'data.frame':    33 obs. of  9 variables:
 $ :'data.frame':    33 obs. of  9 variables:
 $ :'data.frame':    33 obs. of  9 variables:
 $ :'data.frame':    33 obs. of  9 variables:
 $ :'data.frame':    33 obs. of  9 variables:
 $ :'data.frame':    33 obs. of  9 variables:
</pre>
<p>The result of the <strong>list.files() </strong>function will be a character vector that contains the names of all the .CSV files in the current working folder. We then read all of these files in using the <strong>lapply() </strong>function. We could have used a <strong>for loop</strong> construct to do the same thing although the former approach is a more “R-like” way to do things. If you don’t yet understand <strong>lapply() </strong>or need a review of what it does then please see <a target="_blank" href="https://rollingyours.wordpress.com/2014/10/20/the-lapply-command-101/">my posting</a> which explains it in considerable detail. It is a very cool function in R that is used many places so you definitely want to get to get to know how it works. Each element of the resulting list contains a data frame that in turn contains the contents of one of the seven .CSV files. Let’s inspect the first two lines of the first two list elements to get an idea bout the Zika data.</p>
<pre>
# First let's get the column names for the data frames

lapply(myfiles,names)[1]
[[1]]
[1] &quot;report_date&quot;      &quot;location&quot;         &quot;location_type&quot;    &quot;data_field&quot;      
[5] &quot;data_field_code&quot;  &quot;time_period&quot;      &quot;time_period_type&quot; &quot;value&quot;           
[9] &quot;unit&quot;      

# Now look at the first two lines of the first two list elements
lapply(myfiles,head,2)[1:2]
[[1]]
  report_date        location location_type    data_field data_field_code time_period
1  2016-04-02           Norte        region zika_reported          BR0011          NA
2  2016-04-02 Brazil-Rondonia         state zika_reported          BR0011          NA
  time_period_type value  unit
1               NA  6295 cases
2               NA   618 cases

[[2]]
  report_date    location location_type    data_field data_field_code time_period
1  2016-04-23       Norte        region zika_reported          BR0011          NA
2  2016-04-23 Brazil-Acre         state zika_reported          BR0011          NA
  time_period_type value  unit
1               NA  8545 cases
2               NA   716 cases&lt;/pre>
</pre>
<p>So we could work with each file/data frame  individually although I want to create one large data structure. The <strong>do.call()</strong> function in R is used for a number of things but is frequently used to “<strong>bind” </strong>together individual list elements  to form a single data structure so it’s going to be perfect for this job.</p>
<pre>
# The following will take all the individual list elements each of which are
# individual data frames and bind them into a single data frame

brazil &lt;- do.call(rbind,myfiles)

# Let's turn the report_date variable into a &quot;real&quot; date

brazil &lt;- brazil %>% mutate(report_date = as.Date(report_date))

glimpse(brazil)
Observations: 231
Variables: 9
$ report_date      &lt;date> 2016-04-02, 2016-04-02, 2016-04-02, 2016-04-02, 2016-04-02,...
$ location         &lt;chr> &quot;Norte&quot;, &quot;Brazil-Rondonia&quot;, &quot;Brazil-Acre&quot;, &quot;Brazil-Amazonas&quot;...
$ location_type    &lt;chr> &quot;region&quot;, &quot;state&quot;, &quot;state&quot;, &quot;state&quot;, &quot;state&quot;, &quot;state&quot;, &quot;stat...
$ data_field       &lt;chr> &quot;zika_reported&quot;, &quot;zika_reported&quot;, &quot;zika_reported&quot;, &quot;zika_rep...
$ data_field_code  &lt;chr> &quot;BR0011&quot;, &quot;BR0011&quot;, &quot;BR0011&quot;, &quot;BR0011&quot;, &quot;BR0011&quot;, &quot;BR0011&quot;, ...
$ time_period      &lt;lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, ...
$ time_period_type &lt;lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, ...
$ value            &lt;int> 6295, 618, 375, 1520, 44, 771, 74, 2893, 30286, 1202, 7, 156...
$ unit             &lt;chr> &quot;cases&quot;, &quot;cases&quot;, &quot;cases&quot;, &quot;cases&quot;, &quot;cases&quot;, &quot;cases&quot;, &quot;cases...
9               NA 30286 cases
</pre>
<p>If we use the <strong>glimpse() </strong>function to examine this data frame we see that we have some basic variables represented such as <strong>report_date</strong>, l<strong>ocation</strong>, and <strong>value</strong> which appears to represent the number of cases for a given location. The <strong>location_type</strong> variable tells us whether the location is a state or region with the latter being a container of possibly many states. The way I understand the information be organized is that on a certain date a .CSV file was uploaded that contained specific locations within Brazil and the number of reported Zika cases at that time. For each subsequent date (file) the same thing happened although the new number of cases are a  “delta” or adjusted number to the previously reported number. We now have all of that in a single data frame. Let’s work with this data. We will remove columns 6 and 7 since all they have are missing values anyway as confirmed by the above <strong>glimpse()</strong> function results.</p>
<pre>
# Turn the data frame into a dplyr data table and get rid of
# columns 5 and 7

brazil &lt;- brazil %>% select(-(6:7)) 

# Let's look at the first twenty rows. While we are at it we will
# omit columns 6 and 7 since the have all missing values anyway

brazil %>% slice (1:20) 

# A tibble: 20 x 7
   report_date                   location location_type    data_field data_field_code value  unit
        &lt;date>                      &lt;chr>         &lt;chr>         &lt;chr>           &lt;chr> &lt;int> &lt;chr>
1   2016-04-02                      Norte        region zika_reported          BR0011  6295 cases
2   2016-04-02            Brazil-Rondonia         state zika_reported          BR0011   618 cases
3   2016-04-02                Brazil-Acre         state zika_reported          BR0011   375 cases
4   2016-04-02            Brazil-Amazonas         state zika_reported          BR0011  1520 cases
5   2016-04-02             Brazil-Roraima         state zika_reported          BR0011    44 cases
6   2016-04-02                Brazil-Para         state zika_reported          BR0011   771 cases
7   2016-04-02               Brazil-Amapa         state zika_reported          BR0011    74 cases
8   2016-04-02           Brazil-Tocantins         state zika_reported          BR0011  2893 cases
9   2016-04-02                   Nordeste        region zika_reported          BR0011 30286 cases
10  2016-04-02            Brazil-Maranhao         state zika_reported          BR0011  1202 cases
11  2016-04-02               Brazil-Piaui         state zika_reported          BR0011     7 cases
12  2016-04-02               Brazil-Ceara         state zika_reported          BR0011   156 cases
13  2016-04-02 Brazil-Rio_Grande_do_Norte         state zika_reported          BR0011   640 cases
14  2016-04-02             Brazil-Paraiba         state zika_reported          BR0011  1060 cases
15  2016-04-02          Brazil-Pernambuco         state zika_reported          BR0011   333 cases
16  2016-04-02             Brazil-Alagoas         state zika_reported          BR0011  1479 cases
17  2016-04-02             Brazil-Sergipe         state zika_reported          BR0011   348 cases
18  2016-04-02               Brazil-Bahia         state zika_reported          BR0011 25061 cases
19  2016-04-02                    Sudeste        region zika_reported          BR0011 35505 cases
20  2016-04-02        Brazil-Minas_Gerais         state zika_reported          BR0011  6693 cases 
</pre>
<h4>Understanding the Data</h4>
<p>This data frame, though simple, is a little confusing. For each region a cumulative number of cases is presented which represents the sum of all the constituent states occurring in some number of rows below this record. For example – the first row of the data frame summarizes reported cases for the “<em>Norte</em>” region on 04/02/16 and then rows 2-8 are the constituent states and their respective case number totals. So the sum of the cases of rows  2-8 is the same as the case numbers reported on row 1. And then there are other entries for the “<em>Norte</em>” region for different reporting dates later on in the data table.  If you look at row 19 you see that the same thing is happening for the “<em>Sudeste</em>” region. For someone wanting summary information this is fine but it makes the resulting data frame non-standard. As analysts you would probably want to have the region as a factor – another column in the data frame. Totals can easily be generated from this format so we lose nothing. But even with the current format we can still work with it. For example let’s pull out the totals for all regions and plot them.</p>
<pre>
# For each reporting_date we have a each of the 5 region case numbers
# So we have 7 dates/files so we have 35 rows in this table

brazil %>% filter(location_type==&quot;region&quot;)
# A tibble: 35 x 7
   report_date     location location_type    data_field data_field_code value  unit
        &lt;date>        &lt;chr>         &lt;chr>         &lt;chr>           &lt;chr> &lt;int> &lt;chr>
1   2016-04-02        Norte        region zika_reported          BR0011  6295 cases
2   2016-04-02     Nordeste        region zika_reported          BR0011 30286 cases
3   2016-04-02      Sudeste        region zika_reported          BR0011 35505 cases
4   2016-04-02          Sul        region zika_reported          BR0011  1797 cases
5   2016-04-02 Centro-Oeste        region zika_reported          BR0011 17504 cases
6   2016-04-23        Norte        region zika_reported          BR0011  8545 cases
7   2016-04-23     Nordeste        region zika_reported          BR0011 43000 cases
8   2016-04-23      Sudeste        region zika_reported          BR0011 46318 cases
9   2016-04-23          Sul        region zika_reported          BR0011  2197 cases
10  2016-04-23 Centro-Oeste        region zika_reported          BR0011 20101 cases
# ... with 25 more rows

# Let's plot it
brazil %>% filter(location_type==&quot;region&quot;) %>% 
           ggplot(aes(x=report_date, y=value,
                        group=location, color=location)) + 
           geom_line() +  
           geom_point() +
           ggtitle(&quot;Brazil Zika Cases by Region&quot;)
</pre>
<p> </p>
<p><a target="_blank" href="http://i0.wp.com/rollingyours.files.wordpress.com/2016/07/zika_line.png?ssl=1"><img alt="Zika Line Chart" class="wp-image-1607" src="http://i0.wp.com/rollingyours.files.wordpress.com/2016/07/zika_line.png?resize=300%2C231&ssl=1" data-recalc-dims="1"></a></p>
<p>Looks like the Southeast and Northeast have the most reported cases by far and the highest increase in reported cases over the available date period. This doesn’t necessarily mean that the actual number of cases in those regions changed that much over the given date range.  It is possible that those cases were there all along and maybe the collecting and reporting mechanisms just caught up with the actual number of cases. (Note I have no idea if this is/was true or not). Let’s do a barchart of this information just for fun (and also to demonstrate an eccentricity of ggplot).</p>
<pre>
# Let's just pull out the regions into a table of its own

region &lt;- brazil %>% filter(location_type==&quot;region&quot;)

region %>% 
  ggplot(aes(x=location,y=value)) + geom_bar(stat=&quot;identity&quot;) +
  ylab(&quot;Number of Reported Cases&quot;) + xlab(&quot;Region&quot;) + 
  ggtitle(&quot;Reported Brazil Zika Cases 04/02/16 - 2016-05-28&quot;)
</pre>
<p><a target="_blank" href="http://i2.wp.com/rollingyours.files.wordpress.com/2016/07/zika_bar_unsorted.png?ssl=1"><img alt="Zika Bar Chart" class="wp-image-1613" src="http://i2.wp.com/rollingyours.files.wordpress.com/2016/07/zika_bar_unsorted.png?resize=290%2C300&ssl=1" data-recalc-dims="1"></a></p>
<h4>Dealing with Factors</h4>
<p>You saw something like this in the previous posting on dplyr. It’s a bar plot and it’s a bit unsatisfying since the bars are not in order according to number of cases. This data is easy to understand so it’s not a big deal although I don’t like this. I want it to be in order of most cases to least cases. To accomplish this will require creating a factor out of of the <strong>location </strong>variable based on the total number of reported cases. First here is the solution:</p>
<pre>
region %>% 
  slice(1:length(unique(region$location))) %>% 
  arrange(desc(value)) %>%
  mutate(location=factor(location,levels=location,ordered=TRUE)) %>%
  ggplot(aes(x=location,y=value)) + geom_bar(stat=&quot;identity&quot;) +
  ylab(&quot;Number of Reported Cases&quot;) + xlab(&quot;Region&quot;) + 
  ggtitle(&quot;Reported Brazil Zika Cases 04/02/16 - 2016-05-28&quot;)
</pre>
<div data-shortcode="caption" class="wp-caption"><a target="_blank" href="http://i2.wp.com/rollingyours.files.wordpress.com/2016/07/barsort.png?ssl=1"><img alt="Sorted Zika Cases" class="wp-image-1623" src="http://i2.wp.com/rollingyours.files.wordpress.com/2016/07/barsort.png?resize=300%2C248&ssl=1" data-recalc-dims="1"></a>
<p class="wp-caption-text">Sorted Zika Cases</p>
</div>
<p>So how does this work ? Well check this out:</p>
<pre>

# This gets us the unique locations

region %>% 
    slice(1:length(unique(region$location)))
  report_date     location location_type    data_field data_field_code value  unit
1  2016-04-02        Norte        region zika_reported          BR0011  6295 cases
2  2016-04-02     Nordeste        region zika_reported          BR0011 30286 cases
3  2016-04-02      Sudeste        region zika_reported          BR0011 35505 cases
4  2016-04-02          Sul        region zika_reported          BR0011  1797 cases
5  2016-04-02 Centro-Oeste        region zika_reported          BR0011 17504 cases

# This arranges the unique locations by number of reported cases (descending)

region %>% 
     slice(1:length(unique(region$location))) %>% 
     arrange(desc(value))
  report_date     location location_type    data_field data_field_code value  unit
1  2016-04-02      Sudeste        region zika_reported          BR0011 35505 cases
2  2016-04-02     Nordeste        region zika_reported          BR0011 30286 cases
3  2016-04-02 Centro-Oeste        region zika_reported          BR0011 17504 cases
4  2016-04-02        Norte        region zika_reported          BR0011  6295 cases
5  2016-04-02          Sul        region zika_reported          BR0011  1797 cases&lt;/pre>

# So now we can create a factor out the locations variable using this order
# Notice how location is now an ordered factor

region %>% 
     slice(1:length(unique(region$location))) %>% 
     arrange(desc(value)) %>%
     mutate(location=factor(location,levels=location,ordered=TRUE)) %>% glimpse()
Observations: 5
Variables: 7
$ report_date     &lt;date> 2016-04-02, 2016-04-02, 2016-04-02, 2016-04-02, 2016-04-02
$ location        &lt;ord> Sudeste, Nordeste, Centro-Oeste, Norte, Sul
$ location_type   &lt;chr> &quot;region&quot;, &quot;region&quot;, &quot;region&quot;, &quot;region&quot;, &quot;region&quot;
$ data_field      &lt;chr> &quot;zika_reported&quot;, &quot;zika_reported&quot;, &quot;zika_reported&quot;, &quot;zika_re...
$ data_field_code &lt;chr> &quot;BR0011&quot;, &quot;BR0011&quot;, &quot;BR0011&quot;, &quot;BR0011&quot;, &quot;BR0011&quot;
$ value           &lt;int> 35505, 30286, 17504, 6295, 1797
$ unit            &lt;chr> &quot;cases&quot;, &quot;cases&quot;, &quot;cases&quot;, &quot;cases&quot;, &quot;cases&quot;
</pre>
<h4>Standardizing the Data Format</h4>
<p>We could continue to work with the data this way – with summaries for regions and the country sitting along side entries for states. But this mixture “violates” the standards of a “tidy” data frame. What I propose is to remove the summary info for the regions and country. We will make a factor column that gives us the region for each state. This is great because 1) all rows represent the same types of information and 2) we can easily recompute the totals for the region. There are a number of ways to do this – I’m just going with a quick method here with a loop.</p>
<pre>

# Here we will pull out these total so we can later check out work
brazil_totals &lt;- brazil %>% filter(location==&quot;Brazil&quot;) 
region_totals %>% 
       group_by(report_date,location) %>%  
       summarize(tot=sum(value)) -> region_totals

# Here we standardize the data frame and remove all summary rows because after all we can easily
# (re)compute any summary totals that we want

regvec &lt;- vector()  # A vector to hold the region for a given state
length(regvec) &lt;- nrow(brazil)
for (ii in 1:nrow(brazil)) {
  if (brazil[ii,]$location_type != &quot;region&quot;)  {
       regvec[ii] &lt;- newlab
  } else {
       newlab &lt;- brazil[ii,]$location
       regvec[ii] &lt;- newlab
  }
}

# Bind the region vector to the brazil data frame

statedf &lt;- cbind(brazil,regvec)

# Next we eliminate the summary rows for the country and regions

statedf &lt;- statedf %>% filter(location != &quot;Brazil&quot;) 
statedf &lt;- statedf %>% filter(location_type != &quot;region&quot;) 

# Let's generate the region totals from the newly transformed data to make
# sure it matches what we had before. 
statedf %>% group_by(report_date,regvec) %>% 
            summarize(tot=sum(value)) -> totals

# Do a spot check on the totals - compare this to the original data frame 
# totals. There are differences in the column names but other than it's the
# same

all.equal(totals,region_totals)
[1] &quot;Cols in y but not x: location. Cols in x but not y: regvec. &quot;

# The following will give the same line plot as above just to show you that we
# can easily regenerate the totals with no problem.

statedf %>% group_by(report_date,regvec) %>% summarize(cases=sum(value)) %>% 
   ggplot(aes(x=report_date,y=cases,group=regvec,color=regvec)) + 
   geom_line() + 
   geom_point() + 
   ggtitle(&quot;Brazil Zika Cases by Region&quot;)

</pre>
<h4>Drawing a Cluster Map</h4>
<p>Okay what next ? Well how about generating a Leaflet Cluster Map where we see the number of cases reflected in a given region. And if we zoom in we can then see the sub clusters that make up the larger cluster. This is a cool feature of Leaflet. But to do this we will need to Geo Code the locations given in the data frame. This is easy to do since the <strong>ggmap</strong> package has  geo coding capability built into it courtesy of the <strong>geocode() </strong>function which uses Google to do the work. See how easy this:</p>
<pre>
library(ggmap)
longlat &lt;- geocode(unique(statedf$location)) %>% 
  mutate(loc=unique(statedf$location)) 
&lt;/pre>
&lt;pre># Let's join this geocoded information to the statedf and save it into
# another data frame for mapping

statedf %>% filter(as.character(report_date)==&quot;2016-05-28&quot;) %>% 
  group_by(location) %>% summarize(cases=sum(value)) %>% 
  inner_join(longlat,by=c(&quot;location&quot;=&quot;loc&quot;)) %>% 
  mutate(LatLon=paste(lat,lon,sep=&quot;:&quot;)) -> formapping

head(formapping) 
# A tibble: 6 x 5
         location cases       lon       lat              LatLon
            &lt;chr> &lt;int>     &lt;dbl>     &lt;dbl>               &lt;chr>
1     Brazil-Acre   843 -70.00000  -9.00000              -9:-70
2  Brazil-Alagoas  3577 -36.00000  -9.00000              -9:-36
3    Brazil-Amapa   189 -52.00000   1.00000               1:-52
4 Brazil-Amazonas  2176 -63.00000  -5.00000              -5:-63
5    Brazil-Bahia 45419 -38.51083 -12.97111 -12.97111:-38.51083
6    Brazil-Ceara  2144 -38.54306  -3.71722  -3.71722:-38.54306

</pre>
<div data-shortcode="caption" class="wp-caption"><a target="_blank" href="http://i2.wp.com/rollingyours.files.wordpress.com/2016/07/brazil_leaflet.png?ssl=1"><img alt="Cluster Map of Brazil Zika Cases" class="wp-image-1635" src="http://i2.wp.com/rollingyours.files.wordpress.com/2016/07/brazil_leaflet.png?resize=231%2C300&ssl=1" data-recalc-dims="1"></a>
<p class="wp-caption-text">Cluster Map of Brazil Zika Cases</p>
</div>
<p>Wait. This doesn’t look right because we know we have many more cases in many more regions. What happened ? Well the way the cluster mapping option works is that it looks at each row and the corresponding latitude and longitude and clusters together similar rows. The data we mapped has the states listed once and their location so no wonder we get this limited output. If you look at the <strong>cases</strong> column it tells us how many times each cases occurred in that region. We need to find a hack to generate a data frame to get this number of <strong>cases</strong> for each location so the Leaflet Clustermap will look reasonable. There is a way to do this – a number of ways in fact. Here were will generate the row numbers of the data frame and for each row number we’ll replicate that row <strong>case </strong>number of times.</p>
<pre>
num_of_times_to_repeat &lt;- formapping$cases
long_formapping &lt;- formapping[rep(seq_len(nrow(formapping)),
                                  num_of_times_to_repeat),]

head(long_formapping)
# A tibble: 6 x 5
     location cases   lon   lat LatLon
        &lt;chr> &lt;int> &lt;dbl> &lt;dbl>  &lt;chr>
1 Brazil-Acre   843   -70    -9 -9:-70
2 Brazil-Acre   843   -70    -9 -9:-70
3 Brazil-Acre   843   -70    -9 -9:-70
4 Brazil-Acre   843   -70    -9 -9:-70
5 Brazil-Acre   843   -70    -9 -9:-70
6 Brazil-Acre   843   -70    -9 -9:-70

# ah so this is the format we need

leaflet(long_formapping) %>% addTiles() %>% 
  addMarkers(clusterOptions=markerClusterOptions()
</pre>
<div data-shortcode="caption" class="wp-caption"><a target="_blank" href="http://i2.wp.com/rollingyours.files.wordpress.com/2016/07/screenshot_773.png?ssl=1"><img alt="Accurate Cluster Map" class="wp-image-1637" src="http://i2.wp.com/rollingyours.files.wordpress.com/2016/07/screenshot_773.png?resize=300%2C277&ssl=1" data-recalc-dims="1"></a>
<p class="wp-caption-text">Accurate Cluster Map</p>
</div>
<p>So this still isn’t ideal because if we click on one of the clusters to see the constituent sub clusters the performance can be slow especially in regions where there are lots of cases (like the Southest and Northeast).  Why don’t we make a hexbin chart that might be more useful in representing the distribution of reported Zika cases in Brazil ? Hexbin charts are an extension of the heat map concept where squares are used to capture density or count in a region. The hexagonal shape is closer to a circle shape. What we will see is that the hex shapes on the map will be colored by count of Zika cases in that area. You will need to install the <strong>hexbin</strong> package prior to use.</p>
<pre>
library(hexbin)
mymap &lt;- qmap(c(lon=-56.09667,lat=-15.59611),zoom=4)
mymap + 
  geom_hex(bins=bins,data = long_formapping, aes(x = lon, y = lat),alpha=.6,
           inherit.aes=FALSE) + 
  geom_point(data = long_formapping, aes(x = lon, y=lat),
             inherit.aes=FALSE,position = &quot;jitter&quot;)+
  scale_fill_gradient(low = &quot;blue&quot;, high = &quot;red&quot;)
</pre>
<div data-shortcode="caption" class="wp-caption"><a target="_blank" href="http://i0.wp.com/rollingyours.files.wordpress.com/2016/07/screenshot_775.png?ssl=1"><img alt="HexBin Chart of Zika Cases in Brazil" class="wp-image-1651" src="http://i0.wp.com/rollingyours.files.wordpress.com/2016/07/screenshot_775.png?resize=300%2C260&ssl=1" data-recalc-dims="1"></a>
<p class="wp-caption-text">HexBin Chart of Zika Cases in Brazil</p>
</div>
<p> </p>
<p>As usual there are many other ways to kick this data round but I hope I have given you a realistic introduction to working with data that isn’t custom made for summary. Most of the data you will encounter will involve cleaning and manipulation prior to getting the best format for visualization and analysis. Speaking of analysis it can also be the case that you will need to juggle data formats around a few times to accommodate your interests. So many times you will never have a single ideal data format that will serve all your interests. This is why it is essential for you to become experienced with tools like dplyr, the lapply command, and some of the R programming constructs to glue it all together for you. Thanks for reading Steve Pittard © 2016  See <a target="_blank" href="https://pconsdec.com/">my website</a> for information on consulting and R training services.</p>
<p>Filed under: <a target="_blank" href="https://rollingyours.wordpress.com/category/data-mining/">Data Mining</a>, <a target="_blank" href="https://rollingyours.wordpress.com/category/dplyr/">dplyr</a>, <a target="_blank" href="https://rollingyours.wordpress.com/category/zika/">Zika</a> Tagged: <a target="_blank" href="https://rollingyours.wordpress.com/tag/brazil/">Brazil</a>, <a target="_blank" href="https://rollingyours.wordpress.com/tag/dplyr/">dplyr</a>, <a target="_blank" href="https://rollingyours.wordpress.com/tag/ggplot2/">ggplot2</a>, <a target="_blank" href="https://rollingyours.wordpress.com/tag/r/">R</a>, <a target="_blank" href="https://rollingyours.wordpress.com/tag/zika/">Zika</a> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rollingyours.wordpress.com/1580/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/rollingyours.wordpress.com/1580/" border="0"></a> <img alt="" src="http://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></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="http://www.r-bloggers.com/dplyr-and-zika-epilogue/"></div></div><div><a data-url="http://www.r-bloggers.com/dplyr-and-zika-epilogue/" data-counturl="http://www.r-bloggers.com/dplyr-and-zika-epilogue/" data-count="vertical" data-text="dplyr and Zika – Epilogue" 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="https://rollingyours.wordpress.com/2016/07/19/dplyr-and-zika-epilogue/"> Rolling Your Rs</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://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="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/rXppHLcqCXE"></a></div>