{"id":6,"date":"2011-10-17T11:25:33","date_gmt":"2011-10-17T11:25:33","guid":{"rendered":"https:\/\/blogs.swarthmore.edu\/wordpress-camp\/ir-sandbox\/?p=6"},"modified":"2023-09-13T15:41:15","modified_gmt":"2023-09-13T15:41:15","slug":"mapping-student-counties","status":"publish","type":"post","link":"https:\/\/blogs.swarthmore.edu\/institutional-research\/?p=6","title":{"rendered":"Mapping Student Counties"},"content":{"rendered":"<figure id=\"attachment_459\" aria-describedby=\"caption-attachment-459\" style=\"width: 199px\" class=\"wp-caption alignleft\"><a href=\"http:\/\/www.flickr.com\/photos\/bartholl\/3384984207\/in\/photostream\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-459\" title=\"pushpin\" src=\"https:\/\/blogs.swarthmore.edu\/institutional-research\/wp-content\/uploads\/2011\/09\/pushpin-199x300.jpg\" alt=\"\" width=\"199\" height=\"300\" srcset=\"https:\/\/blogs.swarthmore.edu\/institutional-research\/wp-content\/uploads\/2011\/09\/pushpin-199x300.jpg 199w, https:\/\/blogs.swarthmore.edu\/institutional-research\/wp-content\/uploads\/2011\/09\/pushpin.jpg 333w\" sizes=\"(max-width: 199px) 100vw, 199px\" \/><\/a><figcaption id=\"caption-attachment-459\" class=\"wp-caption-text\">Photo by Aram Bartholl<\/figcaption><\/figure>\n<p>We thought it might be interesting to create a map of the home counties of our domestic students. \u00a0Since this is something that I have seen done in <a href=\"http:\/\/www.r-project.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">R<\/a> and I am always up for trying to sharpen my R programming skills, I thought I would give it a shot.<\/p>\n<p>My first step was to retrieve zip codes for all current students from Banner. \u00a0I am able to do this using the RODBC package in R. \u00a0This requires downloading Oracle client software and then setting up an ODBC connection to Oracle first. \u00a0Once this is set up, I can connect to banner, enter my username and password, and then pass a SQL statement to Banner. \u00a0Here is the code for this step:<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<div style=\"overflow: auto;\">\n<div class=\"geshifilter\">\n<pre class=\"r geshifilter-R\" style=\"font-family: monospace;\"><a href=\"http:\/\/inside-r.org\/r-doc\/base\/library\"><span style=\"color: #003399; font-weight: bold;\">library<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/packages\/cran\/RODBC\"><span>RODBC<\/span><\/a><span style=\"color: #009900;\">)<\/span>\r\n\r\nprod<span>&lt;-<\/span>odbcConnect<span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">\"proddb\"<\/span><span style=\"color: #009900;\">)<\/span>\r\n\r\nzip<span>&lt;-<\/span>sqlQuery<span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/prod\"><span style=\"color: #003399; font-weight: bold;\">prod<\/span><\/a><span style=\"color: #339933;\">,<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/paste\"><span style=\"color: #003399; font-weight: bold;\">paste<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">\"select ZIP1 from AS_STUDENT_ENROLLMENT_SUMMARY where TERM_CODE_KEY=201102 and STST_CODE='AS' and LEVL_CODE='UG'\"<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span>\r\n\r\nodbcClose<span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/prod\"><span style=\"color: #003399; font-weight: bold;\">prod<\/span><\/a><span style=\"color: #009900;\">)<\/span><\/pre>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>This creates an R dataframe called &#8220;zip&#8221; and\u00a0closes my RODBC connection to Banner. The example that I am following uses FIPS county codes, so I will need to prep these zip codes for use with a FIPS lookup table by first making sure they are only 5 digits. Then I import my FIPS lookup table (making sure to preserve leading zeros) and merge with student zip codes. Once I have done this, I can get the counts of students in each of the FIPS codes.<\/p>\n<div style=\"overflow: auto;\">\n<div class=\"geshifilter\">\n<pre class=\"r geshifilter-R\" style=\"font-family: monospace;\">zip<span>$<\/span>ZIP<span>&lt;-<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/substr\"><span style=\"color: #003399; font-weight: bold;\">substr<\/span><\/a><span style=\"color: #009900;\">(<\/span>zip<span>$<\/span>ZIP1<span style=\"color: #339933;\">,<\/span><span style=\"color: #cc66cc;\">1<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #cc66cc;\">5<\/span><span style=\"color: #009900;\">)<\/span>\r\n\r\nfips<span>&lt;-<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/utils\/read.csv\"><span style=\"color: #003399; font-weight: bold;\">read.csv<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">\"C:\/R\/FIPSlookup.csv\"<\/span><span style=\"color: #339933;\">,<\/span>\r\ncolClasses=<a href=\"http:\/\/inside-r.org\/r-doc\/base\/c\"><span style=\"color: #003399; font-weight: bold;\">c<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">\"character\"<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">\"character\"<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span>\r\n\r\nm<span>&lt;-<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/merge\"><span style=\"color: #003399; font-weight: bold;\">merge<\/span><\/a><span style=\"color: #009900;\">(<\/span>zip<span style=\"color: #339933;\">,<\/span> fips<span style=\"color: #339933;\">,<\/span> <a href=\"http:\/\/inside-r.org\/r-doc\/base\/by\"><span style=\"color: #003399; font-weight: bold;\">by<\/span><\/a>=<span style=\"color: #0000ff;\">\"ZIP\"<\/span><span style=\"color: #009900;\">)<\/span>\r\n\r\nfipstable<span>&lt;-<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/as.data.frame\"><span style=\"color: #003399; font-weight: bold;\">as.data.frame<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/table\"><span style=\"color: #003399; font-weight: bold;\">table<\/span><\/a><span style=\"color: #009900;\">(<\/span>m<span>$<\/span>fips<span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span><\/pre>\n<\/div>\n<\/div>\n<p>Now I can proceed with the example that I am using. \u00a0This example comes from Barry Rowlingson by way of David Smith&#8217;s &#8220;<a href=\"http:\/\/blog.revolutionanalytics.com\/2009\/11\/choropleth-challenge-result.html\" target=\"_blank\" rel=\"noopener noreferrer\">Choropleth Map Challenge<\/a>&#8221; on his excellent, all-things-R blog. \u00a0I chose this method because it does not rely on merging counties by name, but instead uses FIPS codes &#8211; which we now have thanks to the steps above.<\/p>\n<p>Then I use the &#8220;rgdal&#8221; package to read in US Census shapefile (available <a title=\"US Census Shapefiles\" href=\"http:\/\/www.census.gov\/geo\/www\/cob\/co2000.html\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>), prep the FIPS codes in the shapefile and match with our student counts, and assign zeros to counties with no students:<\/p>\n<div style=\"overflow: auto;\">\n<div class=\"geshifilter\">\n<pre class=\"r geshifilter-R\" style=\"font-family: monospace;\"><a href=\"http:\/\/inside-r.org\/r-doc\/base\/library\"><span style=\"color: #003399; font-weight: bold;\">library<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/packages\/cran\/rgdal\"><span>rgdal<\/span><\/a><span style=\"color: #009900;\">)<\/span>\r\n\r\ncounty<span>&lt;-<\/span>readOGR<span style=\"color: #009900;\">(<\/span><span style=\"color: #0000ff;\">\"C:\/Maps\"<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">\"co99_d00\"<\/span><span style=\"color: #009900;\">)<\/span>\r\ncounty<span>$<\/span>fips<span>&lt;-<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/paste\"><span style=\"color: #003399; font-weight: bold;\">paste<\/span><\/a><span style=\"color: #009900;\">(<\/span>county<span>$<\/span>STATE<span style=\"color: #339933;\">,<\/span>county<span>$<\/span>COUNTY<span style=\"color: #339933;\">,<\/span>sep=<span style=\"color: #0000ff;\">\"\"<\/span><span style=\"color: #009900;\">)<\/span>\r\n\r\nm2<span>&lt;-<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/match\"><span style=\"color: #003399; font-weight: bold;\">match<\/span><\/a><span style=\"color: #009900;\">(<\/span>county<span>$<\/span>fips<span style=\"color: #339933;\">,<\/span>fipstable<span>$<\/span>Var1<span style=\"color: #009900;\">)<\/span>\r\ncounty<span>$<\/span>Freq<span>&lt;-<\/span>fipstable<span>$<\/span>Freq<span style=\"color: #009900;\">[<\/span>m2<span style=\"color: #009900;\">]<\/span>\r\ncounty<span>$<\/span>Freq<span style=\"color: #009900;\">[<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/is.na\"><span style=\"color: #003399; font-weight: bold;\">is.na<\/span><\/a><span style=\"color: #009900;\">(<\/span>county<span>$<\/span>Freq<span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">]<\/span>=<span style=\"color: #cc66cc;\">0<\/span><\/pre>\n<\/div>\n<\/div>\n<p>Following Rowlingson, we use the &#8220;RColorBrewer&#8221; package and his own &#8220;colorschemes&#8221; package to get the colors for our map and associate them with counts of students. We then set the plot region with blank axes, add the counties, and then draw the plot:<\/p>\n<div style=\"overflow: auto;\">\n<div class=\"geshifilter\">\n<pre class=\"r geshifilter-R\" style=\"font-family: monospace;\"><a href=\"http:\/\/inside-r.org\/r-doc\/base\/require\"><span style=\"color: #003399; font-weight: bold;\">require<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/packages\/cran\/RColorBrewer\"><span>RColorBrewer<\/span><\/a><span style=\"color: #009900;\">)<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/base\/require\"><span style=\"color: #003399; font-weight: bold;\">require<\/span><\/a><span style=\"color: #009900;\">(<\/span>colourschemes<span style=\"color: #009900;\">)<\/span>\r\n\r\ncol<span>&lt;-<\/span>brewer.pal<span style=\"color: #009900;\">(<\/span><span style=\"color: #cc66cc;\">6<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #0000ff;\">\"Reds\"<\/span><span style=\"color: #009900;\">)<\/span>\r\nsd<span>&lt;-<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/data.frame\"><span style=\"color: #003399; font-weight: bold;\">data.frame<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/col\"><span style=\"color: #003399; font-weight: bold;\">col<\/span><\/a><span style=\"color: #339933;\">,<\/span>values=<a href=\"http:\/\/inside-r.org\/r-doc\/base\/c\"><span style=\"color: #003399; font-weight: bold;\">c<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #cc66cc;\">0<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #cc66cc;\">2<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #cc66cc;\">4<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #cc66cc;\">6<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #cc66cc;\">8<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #cc66cc;\">10<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #009900;\">)<\/span>\r\nsc<span>&lt;-<\/span>nearestScheme<span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/stats\/sd\"><span style=\"color: #003399; font-weight: bold;\">sd<\/span><\/a><span style=\"color: #009900;\">)<\/span>\r\n\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/graphics\/plot\"><span style=\"color: #003399; font-weight: bold;\">plot<\/span><\/a><span style=\"color: #009900;\">(<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/c\"><span style=\"color: #003399; font-weight: bold;\">c<\/span><\/a><span style=\"color: #009900;\">(<\/span><span>-<\/span><span style=\"color: #cc66cc;\">129<\/span><span style=\"color: #339933;\">,<\/span><span>-<\/span><span style=\"color: #cc66cc;\">61<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">,<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/c\"><span style=\"color: #003399; font-weight: bold;\">c<\/span><\/a><span style=\"color: #009900;\">(<\/span><span style=\"color: #cc66cc;\">21<\/span><span style=\"color: #339933;\">,<\/span><span style=\"color: #cc66cc;\">53<\/span><span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">,<\/span>type=<span style=\"color: #0000ff;\">\"n\"<\/span><span style=\"color: #339933;\">,<\/span>axes=F<span style=\"color: #339933;\">,<\/span>xlab=<span style=\"color: #0000ff;\">\"\"<\/span><span style=\"color: #339933;\">,<\/span>ylab=<span style=\"color: #0000ff;\">\"\"<\/span><span style=\"color: #009900;\">)<\/span>\r\n<a href=\"http:\/\/inside-r.org\/r-doc\/graphics\/plot\"><span style=\"color: #003399; font-weight: bold;\">plot<\/span><\/a><span style=\"color: #009900;\">(<\/span>county<span style=\"color: #339933;\">,<\/span><a href=\"http:\/\/inside-r.org\/r-doc\/base\/col\"><span style=\"color: #003399; font-weight: bold;\">col<\/span><\/a>=sc<span style=\"color: #009900;\">(<\/span>county<span>$<\/span>Freq<span style=\"color: #009900;\">)<\/span><span style=\"color: #339933;\">,<\/span>add=<span style=\"color: #000000; font-weight: bold;\">TRUE<\/span><span style=\"color: #339933;\">,<\/span>border=<span style=\"color: #0000ff;\">\"grey\"<\/span><span style=\"color: #339933;\">,<\/span>lwd=<span style=\"color: #cc66cc;\">0.2<\/span><span style=\"color: #009900;\">)<\/span><\/pre>\n<\/div>\n<\/div>\n<p>Click the thumbnail below to see the resulting map:<br \/>\n<a href=\"https:\/\/blogs.swarthmore.edu\/institutional-research\/wp-content\/uploads\/2011\/09\/zipmap_final.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-thumbnail wp-image-373\" style=\"border-width: 1px; border-color: grey; border-style: solid;\" title=\"county_heat_map\" src=\"https:\/\/blogs.swarthmore.edu\/institutional-research\/wp-content\/uploads\/2011\/09\/zipmap_final-150x150.png\" alt=\"\" width=\"150\" height=\"150\" \/><\/a><\/p>\n<p>As you can see, the map is pretty sparse as you might expect with 1531 students from 325 different counties. \u00a0This represents only a first pass at trying this, so there will be more to come, possibly a googleVis version. \u00a0If others have had success with the above approach, we would love to hear about it in the comments!<\/p>\n<p>To get more info about the geographic distribution of our students (both international and domestic), check out the &#8220;enrollments&#8221; section of our Fact Book page <a title=\"Swarthmore College Fact Book\" href=\"http:\/\/www.swarthmore.edu\/factbook.xml\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>.<\/p>\n<h6><em>The R syntax highlighting used in this post was creating using <a title=\"Pretty R Syntax Highlighter\" href=\"http:\/\/www.inside-r.org\/pretty-r\" target=\"_blank\" rel=\"noopener noreferrer\">Pretty R<\/a>, a tool made available by <a href=\"http:\/\/www.revolutionanalytics.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Revolution Analytics<\/a>.<\/em><\/h6>\n","protected":false},"excerpt":{"rendered":"<p>We thought it might be interesting to create a map of the home counties of our domestic students. \u00a0Since this is something that I have seen done in R and I am always up for trying to sharpen my R programming skills, I thought I would give it a shot. My first step was to &hellip; <a href=\"https:\/\/blogs.swarthmore.edu\/institutional-research\/?p=6\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Mapping Student Counties<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[10],"_links":{"self":[{"href":"https:\/\/blogs.swarthmore.edu\/institutional-research\/index.php?rest_route=\/wp\/v2\/posts\/6"}],"collection":[{"href":"https:\/\/blogs.swarthmore.edu\/institutional-research\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.swarthmore.edu\/institutional-research\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.swarthmore.edu\/institutional-research\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.swarthmore.edu\/institutional-research\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6"}],"version-history":[{"count":95,"href":"https:\/\/blogs.swarthmore.edu\/institutional-research\/index.php?rest_route=\/wp\/v2\/posts\/6\/revisions"}],"predecessor-version":[{"id":1575,"href":"https:\/\/blogs.swarthmore.edu\/institutional-research\/index.php?rest_route=\/wp\/v2\/posts\/6\/revisions\/1575"}],"wp:attachment":[{"href":"https:\/\/blogs.swarthmore.edu\/institutional-research\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.swarthmore.edu\/institutional-research\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.swarthmore.edu\/institutional-research\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}