Skip to content

Instantly share code, notes, and snippets.

@AmritPatel
Created May 23, 2014 01:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AmritPatel/1458550da41f73de856e to your computer and use it in GitHub Desktop.
Save AmritPatel/1458550da41f73de856e to your computer and use it in GitHub Desktop.
Baltimore Nursing Homes
library(dplyr)
library(rCharts)
library(RDSTK)
# read data
nursing_homes = read.csv(
'http://data.baltimorecity.gov/api/views/53js-3bkd/rows.csv',
stringsAsFactors = F
)
nursing_homes_df <- tbl_df(nursing_homes)
# initialize plot
map <- Leaflet$new()
# set the initial view
map$setView(c(39.33351, -76.64981), zoom = 13)
# iterate over the data
for(i in 1:32) {
# format the beginning of the name string for the tooltip
name <- gsub("^", "<p>", nursing_homes_df[i, "name"])
# format the end of the name string for the tooltip
name <- gsub("$", "<p>", name)
# remove the newlines from the location string to pass to s2c function
loc <- gsub("\n", " ", nursing_homes_df[i, "Location.1"])
# run the s2c function to obtain lat, long for adding tooltip
test <- street2coordinates(loc)
# add tooltip given lat, long and use the name of the nursing home for the desc
map$marker(c(test$latitude, test$longitude), bindPopup = name)
}
# show the map
map
<!doctype HTML>
<meta charset = 'utf-8'>
<html>
<head>
<link rel='stylesheet' href="http://netdna.bootstrapcdn.com/bootswatch/2.3.1/cosmo/bootstrap.min.css">
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-responsive.min.css" >
<link rel='stylesheet' href="http://getbootstrap.com/2.3.2/assets/js/google-code-prettify/prettify.css">
<link rel='stylesheet' href="http://aozora.github.io/bootplus/assets/css/docs.css">
<link rel='stylesheet' href='http://cdn.leafletjs.com/leaflet-0.5.1/leaflet.css'>
<script src='http://cdn.leafletjs.com/leaflet-0.5.1/leaflet.js' type='text/javascript'></script>
<script src='https://rawgithub.com/leaflet-extras/leaflet-providers/gh-pages/leaflet-providers.js' type='text/javascript'></script>
<script src='http://harrywood.co.uk/maps/examples/leaflet/leaflet-plugins/layer/vector/KML.js' type='text/javascript'></script>
<style>
.rChart {
display: block
margin: auto auto;
width: 100%;
height: 400px;
}
/*
body {
margin-top: 60px;
}
*/
</style>
</head>
<body>
<div class='container'>
<div class='row'>
<div class='span8'>
<div class="bs-docs-example">
<div id='chart3d5916b08f0e' class='rChart leaflet'>
</div>
<br/>
<pre><code class='r'>library(dplyr)
library(rCharts)
library(RDSTK)
# read data
nursing_homes = read.csv(
'http://data.baltimorecity.gov/api/views/53js-3bkd/rows.csv',
stringsAsFactors = F
)
nursing_homes_df &lt;- tbl_df(nursing_homes)
# initialize plot
map &lt;- Leaflet$new()
# set the initial view
map$setView(c(39.33351, -76.64981), zoom = 13)
# iterate over the data
for(i in 1:32) {
# format the beginning of the name string for the tooltip
name &lt;- gsub(&quot;^&quot;, &quot;&lt;p&gt;&quot;, nursing_homes_df[i, &quot;name&quot;])
# format the end of the name string for the tooltip
name &lt;- gsub(&quot;$&quot;, &quot;&lt;p&gt;&quot;, name)
# remove the newlines from the location string to pass to s2c function
loc &lt;- gsub(&quot;\n&quot;, &quot; &quot;, nursing_homes_df[i, &quot;Location.1&quot;])
# run the s2c function to obtain lat, long for adding tooltip
test &lt;- street2coordinates(loc)
# add tooltip given lat, long and use the name of the nursing home for the desc
map$marker(c(test$latitude, test$longitude), bindPopup = name)
}
# show the map
map
</code></pre>
</div>
</div>
</div>
</div>
<script>
var spec = {
"dom": "chart3d5916b08f0e",
"width": 700,
"height": 400,
"urlTemplate": "http://{s}.tile.osm.org/{z}/{x}/{y}.png",
"layerOpts": {
"attribution": "Map data<a href=\"http://openstreetmap.org\">OpenStreetMap</a>\n contributors, Imagery<a href=\"http://mapbox.com\">MapBox</a>"
},
"center": [ 39.33351, -76.64981 ],
"zoom": 13,
"id": "chart3d5916b08f0e"
}
var map = L.map(spec.dom, spec.mapOpts)
map.setView(spec.center, spec.zoom);
if (spec.provider){
L.tileLayer.provider(spec.provider).addTo(map)
} else {
L.tileLayer(spec.urlTemplate, spec.layerOpts).addTo(map)
}
L
.marker([
39.334,
-76.65
])
.addTo( map )
.bindPopup("<p>ALICE MANOR NURSING HOME<p>")
L
.marker([
39.169,
-76.525
])
.addTo( map )
.bindPopup("<p>BLUE POINT NURSING CENTER<p>")
L
.marker([
39.274,
-76.673
])
.addTo( map )
.bindPopup("<p>CATON MANOR<p>")
L
.marker([
39.311,
-76.674
])
.addTo( map )
.bindPopup("<p>CRAWFORD RETREAT<p>")
L
.marker([
39.292,
-76.601
])
.addTo( map )
.bindPopup("<p>FAYETTE HEALTH AND REHABILITATION CENTER<p>")
L
.marker([
39.329,
-76.542
])
.addTo( map )
.bindPopup("<p>FRANKFORD NURSING AND REHABILITATION CENTER<p>")
L
.marker([
39.306,
-76.574
])
.addTo( map )
.bindPopup("<p>FUTURE CARE CANTON HARBOR<p>")
L
.marker([
39.298,
-76.724
])
.addTo( map )
.bindPopup("<p>FUTURE CARE CHARLES VILLAGE, LLC<p>")
L
.marker([
39.298,
-76.724
])
.addTo( map )
.bindPopup("<p>FUTURE CARE HOMEWOOD<p>")
L
.marker([
39.285,
-76.689
])
.addTo( map )
.bindPopup("<p>FUTURE CARE IRVINGTON, LLC<p>")
L
.marker([
39.318,
-76.728
])
.addTo( map )
.bindPopup("<p>FUTURE CARE SANDTOWN-WINCHESTER<p>")
L
.marker([
39.358,
-76.584
])
.addTo( map )
.bindPopup("<p>GOOD SAMARITAN NURSING CENTER<p>")
L
.marker([
39.363,
-76.552
])
.addTo( map )
.bindPopup("<p>HAMILTON CENTER<p>")
L
.marker([
39.363,
-76.552
])
.addTo( map )
.bindPopup("<p>HARBORSIDE HEALTHCARE HARFORD<p>")
L
.marker([
39.339,
-76.683
])
.addTo( map )
.bindPopup("<p>HAVEN NURSING HOME<p>")
L
.marker([
39.364,
-76.615
])
.addTo( map )
.bindPopup("<p>HOMEWOOD CENTER<p>")
L
.marker([
39.29,
-76.55
])
.addTo( map )
.bindPopup("<p>JOHNS HOPKINS BAYVIEW CARE CENTER<p>")
L
.marker([
39.337,
-76.628
])
.addTo( map )
.bindPopup("<p>KESWICK MULTI-CARE CENTER<p>")
L
.marker([
39.354,
-76.663
])
.addTo( map )
.bindPopup("<p>LEVINDALE HEBREW GER CTR & HSP<p>")
L
.marker([
39.344,
-76.707
])
.addTo( map )
.bindPopup("<p>LOCHEARN NURSING HOME, LLC<p>")
L
.marker([
39.367,
-76.618
])
.addTo( map )
.bindPopup("<p>LONG GREEN CENTER<p>")
L
.marker([
39.346,
-76.641
])
.addTo( map )
.bindPopup("<p>MANOR CARE OF ROLAND PARK<p>")
L
.marker([
39.296,
-76.663
])
.addTo( map )
.bindPopup("<p>MARYLAND BAPTIST AGED HOME<p>")
L
.marker([
39.342,
-76.665
])
.addTo( map )
.bindPopup("<p>NORTHWEST HEALTH AND REHAB<p>")
L
.marker([
39.349,
-76.537
])
.addTo( map )
.bindPopup("<p>OVERLEA HEALTH AND REHABILITATION CENTER<p>")
L
.marker([
39.295,
-76.623
])
.addTo( map )
.bindPopup("<p>RAVENWOOD NSG. & REHAB. CTR.<p>")
L
.marker([
39.284,
-76.701
])
.addTo( map )
.bindPopup("<p>ROCK GLEN NSG & REHAB CTR<p>")
L
.marker([
39.337,
-76.632
])
.addTo( map )
.bindPopup("<p>ROLAND PARK PLACE<p>")
L
.marker([
39.385,
-76.577
])
.addTo( map )
.bindPopup("<p>TRANSITIONAL CARE AT GOOD SAMARITAN<p>")
L
.marker([
39.229,
-76.696
])
.addTo( map )
.bindPopup("<p>TRANSITIONAL CARE SERVICES AT MERCY MEDICAL CENTER<p>")
L
.marker([
39.303,
-76.731
])
.addTo( map )
.bindPopup("<p>MARIA HEALTH CARE CENTER, INC.<p>")
L
.marker([
39.377,
-76.621
])
.addTo( map )
.bindPopup("<p>THE VILLA<p>")
if (spec.circle2){
for (var c in spec.circle2){
var circle = L.circle(c.center, c.radius, c.opts)
.addTo(map);
}
}
</script>
</body>
<!-- Google Prettify -->
<script src="http://cdnjs.cloudflare.com/ajax/libs/prettify/188.0.0/prettify.js"></script>
<script
src='https://google-code-prettify.googlecode.com/svn-history/r232/trunk/src/lang-r.js'>
</script>
<script>
var pres = document.getElementsByTagName("pre");
for (var i=0; i < pres.length; ++i) {
pres[i].className = "prettyprint linenums";
}
prettyPrint();
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment