{"id":129,"date":"2012-06-29T01:37:37","date_gmt":"2012-06-28T22:37:37","guid":{"rendered":"http:\/\/ekqvist.goeuropeinfo.com\/rbloggerqvist\/?p=129"},"modified":"2012-06-29T01:37:37","modified_gmt":"2012-06-28T22:37:37","slug":"r-and-population-maps-in-finland-31-5-2012","status":"publish","type":"post","link":"https:\/\/science.ekqvist.fi\/blogi\/r-and-data-visualization\/r-and-population-maps-in-finland-31-5-2012\/","title":{"rendered":"R and population maps in Finland 31.5.2012"},"content":{"rendered":"<p>This example source is almost same like<a href=\"http:\/\/louhos.wordpress.com\/2011\/10\/09\/r-paketti-avoimen-suomi-datan-louhintaan\/\" target=\"_blank\"> this<\/a>. I used at first time sorvi-package. In this example I modified little bit original and try to compare MML and GADM shape &#8211; files. As we know there is some need to updates into GADM shapefiles (as you will see). Datafiles source date is 31.05.2012 from <a href=\"http:\/\/www.vrk.fi\/default.aspx?id=278\" target=\"_blank\">V\u00e4est\u00f6rekisterikeskus<\/a>. Thanks to Louhos -people and their invaluable work within open -data project.<\/p>\n<p># library sorvi You will find <a href=\"http:\/\/louhos.github.com\/sorvi\/\" target=\"_blank\">there<\/a><\/p>\n<p>library(sorvi)<br \/>\nlibrary(rgeos)<br \/>\nlibrary(rgdal)<br \/>\nif (!gpclibPermit()) { gpclibPermit() }<\/p>\n<p># Finland map and municipility data at gadm-format<br \/>\ngadm &lt;- GetGADM(&#8220;FIN_adm&#8221;, &#8220;Kunta&#8221;)<\/p>\n<p>##now we use also MML as data source (shape) because of compare shape files<br \/>\n# (C) MML 2011<br \/>\ndata(MML)<br \/>\nsp &lt;- MML[[&#8220;1_milj_Shape_etrs_shape&#8221;]][[&#8220;kunta1_p&#8221;]]<\/p>\n<p># vaestorekisteri population data from data source vrk.fi\u00a0 31.05.2012<br \/>\nvrek &lt;- GetPopulationRegister(&#8220;http:\/\/vrk.fi\/default.aspx?docid=6706&amp;site=3&amp;id=0&#8221;)<\/p>\n<p># Attach vrk data into map object and<br \/>\n# set population as zero where na<br \/>\ngadm$asukkaita &lt;- log10(rowSums(vrek[gadm$Kunta, c(&#8220;Miehet&#8221;, &#8220;Naiset&#8221;)]))<br \/>\ngadm$asukkaita[is.na(gadm$asukkaita)] &lt;- 0<\/p>\n<p>sp$asukkaita &lt;- log10(rowSums(vrek[sp$Kunta, c(&#8220;Miehet&#8221;, &#8220;Naiset&#8221;)]))<br \/>\nsp$asukkaita[is.na(sp$asukkaita)] &lt;- 0<\/p>\n<p># male and female share<br \/>\ngadm$miehet.osuus &lt;- vrek[gadm$Kunta, &#8220;Miehet&#8221;]\/vrek[gadm$Kunta, &#8220;Yhteensa&#8221;]<br \/>\ngadm$naiset.osuus &lt;- vrek[gadm$Kunta, &#8220;Naiset&#8221;]\/vrek[gadm$Kunta, &#8220;Yhteensa&#8221;]<\/p>\n<p>gadm_summary_male &lt;- summary(vrek[gadm$Kunta, &#8220;Miehet&#8221;]\/vrek[gadm$Kunta, &#8220;Yhteensa&#8221;])<br \/>\ngadm_summary_female &lt;- summary(vrek[gadm$Kunta, &#8220;Naiset&#8221;]\/vrek[gadm$Kunta, &#8220;Yhteensa&#8221;])<\/p>\n<p>sp$miehet.osuus &lt;- vrek[sp$Kunta, &#8220;Miehet&#8221;]\/vrek[sp$Kunta, &#8220;Yhteensa&#8221;]<br \/>\nsp$naiset.osuus &lt;- vrek[sp$Kunta, &#8220;Naiset&#8221;]\/vrek[sp$Kunta, &#8220;Yhteensa&#8221;]<\/p>\n<p>sp_summary_male &lt;- summary(vrek[sp$Kunta, &#8220;Miehet&#8221;]\/vrek[sp$Kunta, &#8220;Yhteensa&#8221;])<br \/>\nsp_summary_female &lt;- summary(vrek[sp$Kunta, &#8220;Naiset&#8221;]\/vrek[sp$Kunta, &#8220;Yhteensa&#8221;])<\/p>\n<p>#some summary stat<\/p>\n<p>gadm_summary_male<br \/>\ngadm_summary_female<br \/>\nsp_summary_male<br \/>\nsp_summary_female<\/p>\n<p>hist_gadm_male &lt;- hist(gadm$miehet.osuus)<br \/>\nhist_gadm_female &lt;- hist(gadm$naiset.osuus)<br \/>\nhist_sp_male &lt;- hist(sp$miehet.osuus)<br \/>\nhist_sp_female &lt;- hist(sp$naiset.osuus)<\/p>\n<p>plot(density(gadm$miehet.osuus,na.rm=TRUE))<br \/>\nplot(density(gadm$naiset.osuus,na.rm=TRUE))<br \/>\nplot(density(sp$miehet.osuus,na.rm=TRUE))<br \/>\nplot(density(sp$naiset.osuus,na.rm=TRUE))<\/p>\n<p>#saving hist figure into file &#8230;\\r_maps<br \/>\njpeg(&#8220;&#8230;\/hist.jpg&#8221;)<br \/>\nlayout(matrix(1:4,2,2))<br \/>\nhist_gadm_male &lt;- hist(gadm$miehet.osuus)<br \/>\nhist_gadm_female &lt;- hist(gadm$naiset.osuus)<br \/>\nhist_sp_male &lt;- hist(sp$miehet.osuus)<br \/>\nhist_sp_female &lt;- hist(sp$naiset.osuus)<br \/>\ndev.off()<\/p>\n<p><a href=\"http:\/\/science.ekqvist.fi\/blogi\/wp-content\/uploads\/2012\/06\/hist.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-medium wp-image-132\" title=\"hist\" src=\"http:\/\/science.ekqvist.fi\/blogi\/wp-content\/uploads\/2012\/06\/hist-300x300.jpg\" alt=\"\" width=\"300\" height=\"300\" srcset=\"https:\/\/science.ekqvist.fi\/blogi\/wp-content\/uploads\/2012\/06\/hist-300x300.jpg 300w, https:\/\/science.ekqvist.fi\/blogi\/wp-content\/uploads\/2012\/06\/hist-150x150.jpg 150w, https:\/\/science.ekqvist.fi\/blogi\/wp-content\/uploads\/2012\/06\/hist.jpg 480w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>#saving density figre into file &#8230;\\r_maps (replace &#8230; as your fav. folder)<br \/>\njpeg(&#8220;&#8230;\/density.jpg&#8221;)<br \/>\nlayout(matrix(1:4,2,2))<br \/>\nplot(density(gadm$miehet.osuus,na.rm=TRUE))<br \/>\nplot(density(gadm$naiset.osuus,na.rm=TRUE))<br \/>\nplot(density(sp$miehet.osuus,na.rm=TRUE))<br \/>\nplot(density(sp$naiset.osuus,na.rm=TRUE))<br \/>\ndev.off()<\/p>\n<p><a href=\"http:\/\/science.ekqvist.fi\/blogi\/wp-content\/uploads\/2012\/06\/density.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-medium wp-image-133\" title=\"density\" src=\"http:\/\/science.ekqvist.fi\/blogi\/wp-content\/uploads\/2012\/06\/density-300x300.jpg\" alt=\"\" width=\"300\" height=\"300\" srcset=\"https:\/\/science.ekqvist.fi\/blogi\/wp-content\/uploads\/2012\/06\/density-300x300.jpg 300w, https:\/\/science.ekqvist.fi\/blogi\/wp-content\/uploads\/2012\/06\/density-150x150.jpg 150w, https:\/\/science.ekqvist.fi\/blogi\/wp-content\/uploads\/2012\/06\/density.jpg 480w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p># set share\u00a0 50% male\/female<br \/>\n# where na<br \/>\ngadm$miehet.osuus[is.na(gadm$miehet.osuus)] &lt;- 0.5<br \/>\ngadm$naiset.osuus[is.na(gadm$naiset.osuus)] &lt;- 0.5<\/p>\n<p>#same with MML<\/p>\n<p>sp$miehet.osuus[is.na(sp$miehet.osuus)] &lt;- 0.5<br \/>\nsp$naiset.osuus[is.na(sp$naiset.osuus)] &lt;- 0.5<\/p>\n<p># border of interval GADM<br \/>\nvarname1 &lt;- &#8220;naiset.osuus&#8221;<br \/>\ninterval1 &lt;- max(abs(gadm[[varname1]] &#8211; 0.5))<br \/>\nat1 &lt;- seq(0.5 &#8211; interval1, 0.5 + interval1, length = 100)<\/p>\n<p>#border of interval MML<\/p>\n<p>varname2 &lt;- &#8220;naiset.osuus&#8221;<br \/>\ninterval2 &lt;- max(abs(sp[[varname2]] &#8211; 0.5))<br \/>\nat2 &lt;- seq(0.5 &#8211; interval2, 0.5 + interval2, length = 100)<\/p>\n<p>#PLOTTING INTO FILE<\/p>\n<p>jpeg(&#8220;j:\/todo\/r_maps\/gadm_280612.jpg&#8221;)<br \/>\nq1 &lt;- PlotShape(gadm, varname1, type = &#8220;twoway&#8221;, at = at1, main = &#8220;Share of women (red) and men (blue) 31.05.2012 n=437 (GADM)&#8221;)<br \/>\ndev.off()<\/p>\n<p>jpeg(&#8220;j:\/todo\/r_maps\/mml_280612.jpg&#8221;)<br \/>\nq2 &lt;- PlotShape(sp, varname2, type = &#8220;twoway&#8221;, at = at2, main = &#8220;Share of women (red) and men (blue) 31.05.2012 n=337 (MML)&#8221;)<br \/>\ndev.off()<\/p>\n<p>#Which one is best?<\/p>\n<p><a href=\"http:\/\/science.ekqvist.fi\/blogi\/wp-content\/uploads\/2012\/06\/gadm_mml_2806122.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter  wp-image-136\" title=\"gadm_mml_280612\" src=\"http:\/\/science.ekqvist.fi\/blogi\/wp-content\/uploads\/2012\/06\/gadm_mml_2806122.jpg\" alt=\"\" width=\"614\" height=\"337\" srcset=\"https:\/\/science.ekqvist.fi\/blogi\/wp-content\/uploads\/2012\/06\/gadm_mml_2806122.jpg 876w, https:\/\/science.ekqvist.fi\/blogi\/wp-content\/uploads\/2012\/06\/gadm_mml_2806122-300x165.jpg 300w, https:\/\/science.ekqvist.fi\/blogi\/wp-content\/uploads\/2012\/06\/gadm_mml_2806122-768x423.jpg 768w, https:\/\/science.ekqvist.fi\/blogi\/wp-content\/uploads\/2012\/06\/gadm_mml_2806122-795x437.jpg 795w\" sizes=\"(max-width: 614px) 100vw, 614px\" \/><\/a><\/p>\n<p>GADM projection is not like &#8220;normal&#8221; and MML coastline is little bit odd, but I am patiently looking forward to improvement\u00a0 into this issue&#8230;.<\/p>\n<p>ME\/290612<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This example source is almost same like this. I used at first time sorvi-package. In this example I modified little bit original and try to compare MML and GADM shape &#8211; files. As we know there is some need to updates into GADM shapefiles (as you will see). Datafiles source date is 31.05.2012 from V\u00e4est\u00f6rekisterikeskus. <a class=\"read-more-excerpt\" href=\"https:\/\/science.ekqvist.fi\/blogi\/r-and-data-visualization\/r-and-population-maps-in-finland-31-5-2012\/\">[&#8230;] Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"gallery","meta":[],"categories":[19,22,23],"tags":[34,47,48],"_links":{"self":[{"href":"https:\/\/science.ekqvist.fi\/blogi\/wp-json\/wp\/v2\/posts\/129"}],"collection":[{"href":"https:\/\/science.ekqvist.fi\/blogi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/science.ekqvist.fi\/blogi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/science.ekqvist.fi\/blogi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/science.ekqvist.fi\/blogi\/wp-json\/wp\/v2\/comments?post=129"}],"version-history":[{"count":0,"href":"https:\/\/science.ekqvist.fi\/blogi\/wp-json\/wp\/v2\/posts\/129\/revisions"}],"wp:attachment":[{"href":"https:\/\/science.ekqvist.fi\/blogi\/wp-json\/wp\/v2\/media?parent=129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/science.ekqvist.fi\/blogi\/wp-json\/wp\/v2\/categories?post=129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/science.ekqvist.fi\/blogi\/wp-json\/wp\/v2\/tags?post=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}