{"id":2224,"date":"2024-07-21T15:12:28","date_gmt":"2024-07-21T06:12:28","guid":{"rendered":"https:\/\/secondlife.lol\/?p=2224"},"modified":"2024-07-21T16:35:52","modified_gmt":"2024-07-21T07:35:52","slug":"creating-treemap-with-gni2014-dataset-in-r","status":"publish","type":"post","link":"https:\/\/secondlife.lol\/en\/creating-treemap-with-gni2014-dataset-in-r\/","title":{"rendered":"Create treemaps with GNI2014 datasets (feat. treemap library)"},"content":{"rendered":"<p class=\"wp-block-paragraph\">When visualizing data, have you ever found it difficult to grasp a huge amount of information at a glance, especially when dealing with country-specific economic data? To solve this problem, why not try creating a treemap?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this post, we'll take a look at R's <code>treemap<\/code> library to visualize the GNI2014 dataset. We'll walk you through the process of creating a treemap so you can easily understand the data visually, and you'll learn how to compare the GNI of each country by region. Make sure to read all the way through so you don't miss any important information!<\/p>\n\n\n<style>.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-content-wrap{padding-top:var(--global-kb-spacing-sm, 1.5rem);padding-right:var(--global-kb-spacing-sm, 1.5rem);padding-bottom:var(--global-kb-spacing-sm, 1.5rem);padding-left:var(--global-kb-spacing-sm, 1.5rem);border-top:3px solid var(--global-palette2, #2B6CB0);border-right:3px solid var(--global-palette2, #2B6CB0);border-bottom:3px solid var(--global-palette2, #2B6CB0);border-left:3px solid var(--global-palette2, #2B6CB0);border-top-left-radius:5px;border-top-right-radius:5px;border-bottom-right-radius:5px;border-bottom-left-radius:5px;box-shadow:0px 0px 14px 0px rgba(0, 0, 0, 0.2);}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-contents-title-wrap{padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-contents-title-wrap{color:var(--global-palette2, #2B6CB0);}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-contents-title{color:var(--global-palette2, #2B6CB0);font-size:28px;font-weight:regular;font-style:normal;}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-content-wrap .kb-table-of-content-list{color:var(--global-palette1, #3182CE);line-height:2em;font-weight:regular;font-style:normal;margin-top:var(--global-kb-spacing-sm, 1.5rem);margin-right:0px;margin-bottom:0px;margin-left:0px;}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-content-wrap .kb-table-of-content-list .kb-table-of-contents__entry:hover{color:var(--global-palette6, #718096);}@media all and (max-width: 1024px){.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-content-wrap{border-top:3px solid var(--global-palette2, #2B6CB0);border-right:3px solid var(--global-palette2, #2B6CB0);border-bottom:3px solid var(--global-palette2, #2B6CB0);border-left:3px solid var(--global-palette2, #2B6CB0);}}@media all and (max-width: 1024px){.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-contents-title{font-size:28px;}}@media all and (max-width: 767px){.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-content-wrap{border-top:3px solid var(--global-palette2, #2B6CB0);border-right:3px solid var(--global-palette2, #2B6CB0);border-bottom:3px solid var(--global-palette2, #2B6CB0);border-left:3px solid var(--global-palette2, #2B6CB0);}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-contents-title{font-size:28px;}}<\/style>\n\n\n<h2 class=\"wp-block-heading\">Installing and loading libraries<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To visualize a treemap, first use the <code>treemap<\/code> Install and load the library.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Install the # Treemap Library<br>install.packages(\"treemap\")<br><br>Loading the # Treemap Library<br>library(treemap)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Preparing the GNI2014 dataset<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The GNI2014 dataset is a built-in dataset in the treemap library that contains gross national income (GNI) and regional information for each country. The dataset contains the following variables<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Country<\/code>: Country name<\/li>\n\n\n\n<li><code>Region<\/code>: region name<\/li>\n\n\n\n<li><code>Population<\/code>: Population count<\/li>\n\n\n\n<li><code>GNI<\/code>: Gross National Income (GNI)<\/li>\n\n\n\n<li><code>GNI_perCapita<\/code>Gross National Income per capita<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">We'll use this dataset to generate a treemap.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Load the # GNI2014 dataset<br>data(GNI2014)<br><br>Verify # data<br>head(GNI2014)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Create a treemap<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now let's create a treemap using the GNI2014 dataset, visualizing the GNI of each country and grouping them by region.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Create a # Treemap<br>treemap(GNI2014,<br>        index = c(\"continent\", \"country\"),<br>        vSize = \"GNI\",<br>        vColor = \"GNI\",<br>        type = \"value\",<br>        title = \"GNI2014 Dataset Treemap\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the code above, the <code>index<\/code> parameter defines the hierarchy, <code>vSize<\/code>sets the size of each tile, <code>vColor<\/code>specifies the color of each tile. <code>type<\/code>means color mapping by value. <code>title<\/code>specifies the title of the treemap.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Describe the output of creating a treemap<\/h2>\n\n\n<style>.kb-image2224_a9289d-da .kb-image-has-overlay:after{opacity:0.3;}<\/style>\n<div class=\"wp-block-kadence-image kb-image2224_a9289d-da\"><figure class=\"aligncenter size-medium_large\"><a href=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/07\/image-3.jpg\" class=\"kb-advanced-image-link\"><img decoding=\"async\" width=\"768\" height=\"436\" src=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/07\/image-3-768x436.jpg\" alt=\"\ud2b8\ub9ac\ub9f5 \ub9cc\ub4e4\uae30\" class=\"kb-img wp-image-2225\" srcset=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/07\/image-3-768x436.jpg 768w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/07\/image-3-300x170.jpg 300w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/07\/image-3-600x340.jpg 600w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/07\/image-3.jpg 897w\" sizes=\"(max-width: 768px) 100vw, 768px\" \/><\/a><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">When you run the source code, you'll end up with the same '<strong>Treemap showing Gross National Income (GNI) by country for 2014<\/strong>' appears, with the following key characteristics<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Size<\/strong>: The size of the squares representing each country is <strong>GNI size of the country<\/strong>of the system. The larger the square, the higher the GNI.<\/li>\n\n\n\n<li><strong>Color<\/strong>A color spectrum that changes from dark green to light green to beige indicates the GNI level. <strong>Darker green colors have higher GNI, lighter colors have lower GNI<\/strong>.<\/li>\n\n\n\n<li><strong>Regionalization<\/strong>: Countries are <strong>Group by continent or region<\/strong>. For example, Europe, Asia, North America, etc. are clearly demarcated.<\/li>\n\n\n\n<li><strong>Scale<\/strong>The colored bar at the bottom shows the range of GNI values, from 0 to over 1e+05 (100,000).<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">This visualization allows you to compare the size and relative economic strength of each country's economy at a glance, as well as understand the economic situation across continents. For example, you can see that countries in Europe and North America are generally dark green in color, indicating a high GNI, while countries in Africa and South America are relatively light in color, indicating a low GNI.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Additional explanation<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The reason Bermuda and Norway are colored dark green is because they have very high GNI per capita, which we'll talk about in a bit more detail.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Bermuda\n<ul class=\"wp-block-list\">\n<li>Bermuda is a British Overseas Territory, a small island nation with a population of about 60,000 people.<\/li>\n\n\n\n<li>Its main industries are international finance and tourism, and it is particularly well known as a tax haven.<\/li>\n\n\n\n<li>It has a highly developed financial services industry and a high per capita income.<\/li>\n\n\n\n<li>It has a very high GNI per capita due to its small population and focus on high-value industries.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Norway\n<ul class=\"wp-block-list\">\n<li>Norway is a developed country in Northern Europe with an abundance of natural resources.<\/li>\n\n\n\n<li>In particular, the oil and natural gas resources of the North Sea contribute significantly to the country's economy.<\/li>\n\n\n\n<li>Economic prosperity has been achieved through efficient resource management with a high level of social welfare.<\/li>\n\n\n\n<li>It has a relatively small population of about 5.4 million people and a very high GNI per capita due to high-value industries such as the oil industry.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">The fact that these two countries are colored dark green doesn't simply mean that their total GNI is large, but that they have a very high GNI per capita, or GNI per capita, relative to their population. This is a reflection of a country's economic prosperity and the high standard of living of its people.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It's also important to note, however, that this high GNI per capita doesn't necessarily mean equal wealth for all citizens, and that each country's particular economic structure and how resources are distributed must be taken into account.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Organize<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Treemaps are a powerful tool for visually representing the hierarchical structure of your data. <code>treemap<\/code> library allows you to effectively visualize the GNI2014 dataset. This allows you to intuitively compare the GNI of different countries and regions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use the full source code below to create a treemap using your own data and get a taste of what visualization in R can be like.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Full source code<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Install the # Treemap Library\ninstall.packages(\"treemap\")\n\nLoad the # treemap library\nlibrary(treemap)\n\nLoad the # GNI2014 dataset\ndata(GNI2014)\n\nView # data\nhead(GNI2014)\n\nGenerate a # treemap\ntreemap(GNI2014,\n        index = c(\"continent\", \"country\"),\n        vSize = \"GNI\",\n        vColor = \"GNI\",\n        type = \"value\",\n        title = \"GNI2014 dataset treemap\")<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Take a step forward<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">But the generated graph looks a bit dated. Wouldn't it be possible to draw a more trendy treemap like the one below? How can we make the graph more visually appealing? If you're curious <a href=\"https:\/\/secondlife.lol\/en\/r-treemap-visualization-gni2014\/\">Here<\/a>for more information.<\/p>\n\n\n<style>.kb-image2224_2e990d-08 .kb-image-has-overlay:after{opacity:0.3;}<\/style>\n<figure class=\"wp-block-kadence-image kb-image2224_2e990d-08 size-medium_large\"><img decoding=\"async\" width=\"768\" height=\"421\" src=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/07\/image-4-768x421.jpg\" alt=\"\ub354 \uba4b\uc9c4 \ud2b8\ub9ac\ub9f5 \ub9cc\ub4e4\uae30\" class=\"kb-img wp-image-2231\" srcset=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/07\/image-4-768x421.jpg 768w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/07\/image-4-300x165.jpg 300w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/07\/image-4-600x329.jpg 600w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/07\/image-4.jpg 1200w\" sizes=\"(max-width: 768px) 100vw, 768px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Additional resources<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For additional information about the treemap library, please use the links below.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/cran.r-project.org\/package=treemap\" target=\"_blank\" rel=\"noopener\">treemap library<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>When it comes to visualizing data, it can be difficult to make sense of huge amounts of information at a glance...<\/p>","protected":false},"author":3,"featured_media":2229,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_blocks_custom_css":"","_kad_blocks_head_custom_js":"","_kad_blocks_body_custom_js":"","_kad_blocks_footer_custom_js":"","_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[6],"tags":[169,168,171,170,167,173,172],"class_list":["post-2224","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-r-coding","tag-datascience","tag-datavisualization","tag-gni2014","tag-rprogramming","tag-treemap","tag-173","tag-172"],"taxonomy_info":{"category":[{"value":6,"label":"\uc54c(R)"}],"post_tag":[{"value":169,"label":"DataScience"},{"value":168,"label":"DataVisualization"},{"value":171,"label":"GNI2014"},{"value":170,"label":"RProgramming"},{"value":167,"label":"Treemap"},{"value":173,"label":"\ub370\uc774\ud130\ubd84\uc11d"},{"value":172,"label":"\uc2dc\uac01\ud654"}]},"featured_image_src_large":["https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/07\/treemap-thumbnail-600x600.jpg",600,600,true],"author_info":{"display_name":"TERE","author_link":"https:\/\/secondlife.lol\/en\/author\/tere\/"},"comment_info":0,"category_info":[{"term_id":6,"name":"\uc54c(R)","slug":"r-coding","term_group":0,"term_taxonomy_id":6,"taxonomy":"category","description":"","parent":20,"count":61,"filter":"raw","cat_ID":6,"category_count":61,"category_description":"","cat_name":"\uc54c(R)","category_nicename":"r-coding","category_parent":20}],"tag_info":[{"term_id":169,"name":"DataScience","slug":"datascience","term_group":0,"term_taxonomy_id":169,"taxonomy":"post_tag","description":"","parent":0,"count":1,"filter":"raw"},{"term_id":168,"name":"DataVisualization","slug":"datavisualization","term_group":0,"term_taxonomy_id":168,"taxonomy":"post_tag","description":"","parent":0,"count":1,"filter":"raw"},{"term_id":171,"name":"GNI2014","slug":"gni2014","term_group":0,"term_taxonomy_id":171,"taxonomy":"post_tag","description":"","parent":0,"count":1,"filter":"raw"},{"term_id":170,"name":"RProgramming","slug":"rprogramming","term_group":0,"term_taxonomy_id":170,"taxonomy":"post_tag","description":"","parent":0,"count":1,"filter":"raw"},{"term_id":167,"name":"Treemap","slug":"treemap","term_group":0,"term_taxonomy_id":167,"taxonomy":"post_tag","description":"","parent":0,"count":1,"filter":"raw"},{"term_id":173,"name":"\ub370\uc774\ud130\ubd84\uc11d","slug":"%eb%8d%b0%ec%9d%b4%ed%84%b0%eb%b6%84%ec%84%9d","term_group":0,"term_taxonomy_id":173,"taxonomy":"post_tag","description":"","parent":0,"count":5,"filter":"raw"},{"term_id":172,"name":"\uc2dc\uac01\ud654","slug":"%ec%8b%9c%ea%b0%81%ed%99%94","term_group":0,"term_taxonomy_id":172,"taxonomy":"post_tag","description":"","parent":0,"count":3,"filter":"raw"}],"_links":{"self":[{"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/posts\/2224","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/comments?post=2224"}],"version-history":[{"count":10,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/posts\/2224\/revisions"}],"predecessor-version":[{"id":2256,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/posts\/2224\/revisions\/2256"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/media\/2229"}],"wp:attachment":[{"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/media?parent=2224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/categories?post=2224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/tags?post=2224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}