<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/the-cycling-accident-map-of-madrid-city/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://fronkonstin.com/2017/08/29/the-cycling-accident-map-of-madrid-city/"> R – Fronkonstin</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div class="twitter-share"><a rel="nofollow" href="https://twitter.com/intent/tweet?url=https%3A%2F%2Ffronkonstin.com%2F2017%2F08%2F29%2Fthe-cycling-accident-map-of-madrid-city%2F&via=aschinchon" class="twitter-share-button" target="_blank">Tweet</a></div>
<blockquote><p>Far away, this ship has taken me far away (Starlight, Muse)</p>
</blockquote>
<p>Madrid City has <a rel="nofollow" href="http://datos.madrid.es/portal/site/egob/" target="_blank">an Open Data platform</a> where can be found <strong>around 300 data sets</strong> about a number of topics. One of these sets is the one I used for this experiment. It contains information about <strong>cycling accidents  happened in the city from January to July 2017</strong>. I have done a map to locate where the accidents took place. This experiment shows how R <strong>makes very easy to create <em>professional</em> maps</strong> with <a rel="nofollow" href="http://leafletjs.com/" target="_blank">Leaflet</a> (in this case I use <a rel="nofollow" href="https://carto.com/" target="_blank">Carto</a> basemaps).</p>
<p>To locate accidents the data set <strong>only contains the address</strong> where they happened so the first thing I did is to <strong>obtain their geographical coordinates</strong> using <code>geocode</code> function from <code>ggmap</code> package. There were 431 accidents during the first 7 months of 2017 (such a big number!) and I got coordinates of 407 so I can locate 94% of the accidents.</p>
<p>Obviously, the amount of accidents in some place <strong>depend</strong> on <strong>how many bikers</strong> circulate there as well as on its<strong> infrastructure</strong>. None of these things can be seen in the map: <strong>It only shows number of accidents</strong>.</p>
<p>The categorization of accidents is:</p>
<ul>
<li><strong>Double collision (<em>Colisión doble</em>):</strong> Traffic accident occurred between two moving vehicles.</li>
<li><strong>Multiple collision (<em>Colisión múltiple</em>):</strong> Traffic accident occurred between more than two moving vehicles.</li>
<li><strong>Fixed object collision (<em>Choque con objeto fijo</em>):</strong> Accident occurred between a moving vehicle with a driver and an immovable object that occupies the road or separated area of ​​the same, whether parked vehicle, tree, street lamp, etc.</li>
<li><strong>Accident (<em>Atropello</em>):</strong> Accident occurred between a vehicle and a pedestrian that occupies the road or travels by sidewalks, refuges, walks or zones of the public road not destined to the circulation of vehicles.</li>
<li><strong>Overturn (<em>Vuelco</em>):</strong> Accident suffered by a vehicle with more than two wheels which by some circumstance loses contact with the road and ends supported on one side or on its roof.</li>
<li><strong>Motorcycle fall (<em>Caída motocicleta</em>):</strong> Accident suffered by a motorcycle, which at some moment loses balance, because of the driver or due to the conditions of the road.</li>
<li><strong>Moped fall (<em>Caída ciclomotor</em>):</strong> Accident suffered by a moped, which at some moment loses balance, because of the driver or due to the conditions of the road.</li>
<li><strong>Bicycle fall (<em>Caída bicicleta</em>):</strong> Accident suffered by a bicycle, which at some moment loses balance, because of the driver or due to the conditions of the road.</li>
</ul>
<p>These categories are <strong>redundant</strong> (e.g. <em>Double</em> and <em>Multiple</em> collision), <strong>difficult to understand</strong> (e.g. <em>Overturn</em>) or <strong>both things at the same time</strong> (e.g. <em>Motorcycle fall</em> and <em>Moped fall</em>). This categorization also <strong>forgets human damages incurred by the accident</strong>.</p>
<p>Taking all these things in mind, <strong>this is the map</strong>:</p>
<p><br>
<iframe src="https://fronkonstin.com/wp-content/uploads/2017/08/bike_accidents_0107_2017.html" width="100%" height="500"></iframe></p>
<p><a rel="nofollow" href="https://fronkonstin.com/wp-content/uploads/2017/08/bike_accidents_0107_2017.html" target="_blank">Here </a>is a full-screen version of the map.</p>
<p>My <strong>suggestions</strong> to the city council of Madrid are:</p>
<ol>
<li><strong>Add geographical coordinates to data</strong> (I guess many of the analysis will need them)</li>
<li><strong><em>Rethink</em> the categorization</strong> to make it clearer and more informative</li>
<li><strong>Add more <em>cycling data sets</em> to the platform</strong> (detail of bikeways, traffic …) to understand accidents better</li>
<li>Attending just to the number of accidents , <strong>put the focus around Parque del Retiro, specially on its west surroundings, from Plaza de Cibeles to Plaza de Carlos V</strong>: more warning signals, more  (or better) bikeways …</li>
</ol>
<p>I add the code below to update the map (If someone ask it to me, I can do it myself regularly):</p>
<pre>
library(dplyr)
library(stringr)
library(ggmap)
library(leaflet)
# First, getting the data
download.file(paste0(&quot;http://datos.madrid.es/egob/catalogo/&quot;, file), 
              destfile=&quot;300110-0-accidentes-bicicleta.csv&quot;)

data=read.csv(&quot;300110-0-accidentes-bicicleta.csv&quot;, sep=&quot;;&quot;, skip=1)

# Prepare data for geolocation
data %>% 
  mutate(direccion=paste(str_trim(Lugar), str_trim(Numero), &quot;MADRID, SPAIN&quot;, sep=&quot;, &quot;) %>% 
           str_replace(&quot;NA, &quot;, &quot;&quot;) %>% 
           str_replace(&quot; - &quot;, &quot; CON &quot;)) -> data

# Geolocation (takes some time ...)
coords=c()
for (i in 1:nrow(data)) 
{
  coords %>% rbind(geocode(data[i,&quot;direccion&quot;])) -> coords
  Sys.sleep(0.5)
}
  
# Save data, just in case
data %>% cbind(coords) %>% saveRDS(file=&quot;bicicletas.RDS&quot;)

data=readRDS(file=&quot;bicicletas.RDS&quot;)

# Remove non-successfull geolocations
data %>% 
  filter(!is.na(lon)) %>% 
  droplevels()-> data

# Remove non-successfull geolocations
data %>% mutate(Fecha=paste0(as.Date(data$Fecha, &quot;%d/%m/%Y&quot;), &quot; &quot;, TRAMO.HORARIO),
                popup=paste0(&quot;<b>Dónde:</b>&quot;,
                             direccion,
                             &quot;<b>Cuándo:</b>&quot;,
                             Fecha,
                             &quot;<b>Qué pasó:</b>&quot;,
                             Tipo.Accidente)) -> data

# Do the map
data %>% split(data$Tipo.Accidente) -> data.df

l &lt;- leaflet() %>% addProviderTiles(providers$CartoDB.Positron)

names(data.df) %>%
  purrr::walk( function(df) {
    l &lt;- l %>%
      addCircleMarkers(data=data.df[[df]],
                 lng=~lon, lat=~lat,
                 popup=~popup,
                 color=&quot;red&quot;,
                 stroke=FALSE,
                 fillOpacity = 0.8,
                 group = df,
                 clusterOptions = markerClusterOptions(removeOutsideVisibleBounds = F))
  })

l %>%
  addLayersControl(
    overlayGroups = names(data.df),
    options = layersControlOptions(collapsed = FALSE)
  )
</pre>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://fronkonstin.com/2017/08/29/the-cycling-accident-map-of-madrid-city/"> R – Fronkonstin</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/-urwdBjpIdg" height="1"></a></div>