Sunday, September 1, 2024

TreeMap using R

Tree Map Using R

Introduction

Tree Maps are important visualization and helps us get a sense of relative size of various entities in the data. For example, we can analyse the total sales using tree map and it would help us get a sense of relative sales amount for different countries.Tableau is known to create beautiful visualization and tree map can be created with ease in tableau. But lets say if we dont have access to tableau then we can leverage R and create something similar.

Step 1: Importing the libraries

package.name<-c("tidyverse","plotly")

for(i in package.name){

  if(!require(i,character.only = T)){

    install.packages(i)
  }
  library(i,character.only = T)

}
Loading required package: tidyverse
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.0     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Loading required package: plotly


Attaching package: 'plotly'


The following object is masked from 'package:ggplot2':

    last_plot


The following object is masked from 'package:stats':

    filter


The following object is masked from 'package:graphics':

    layout


Step 2:Creating sample data


df<-data.frame(Item = str_c("Item_",1:10),
               sales=round(rnorm(10,50,20),2))%>%
  arrange(desc(sales))
df
      Item sales
1   Item_9 90.92
2   Item_4 74.38
3   Item_2 54.74
4   Item_5 53.48
5   Item_1 39.77
6   Item_3 39.17
7  Item_10 38.78
8   Item_6 37.69
9   Item_8 37.13
10  Item_7 13.86


Step 3: Adding color variable

df1<-df%>%
  mutate(clr=c("#B3B3B9","#C00000","#E66C37",
               "#666699","#330099","#C00000",
                "#66CC66","#FFC000","#003333",
                "#E66C37"))

df1
      Item sales     clr
1   Item_9 90.92 #B3B3B9
2   Item_4 74.38 #C00000
3   Item_2 54.74 #E66C37
4   Item_5 53.48 #666699
5   Item_1 39.77 #330099
6   Item_3 39.17 #C00000
7  Item_10 38.78 #66CC66
8   Item_6 37.69 #FFC000
9   Item_8 37.13 #003333
10  Item_7 13.86 #E66C37

I am adding random colors. These colors would mirror the item number they are assigned in the table.FOr instance, item_8 would appear as grey(#B3B3B9) and so on.

Step 4: Creating the tree map

plot_ly(
  df1,
  type="treemap",
  labels=~Item,
  parents=~NA,
  values=~sales ,
  textinfo="label+value+percent",
  texttemplate = '<b>%{label}</br></br>%{value}</b>', 
  marker=list(colors=~clr))


Step 5: Grouping items within the Map

Lets say we want certain items to be resepresnted with the same color. Then we define the colors in the data frame accordingly. Let see it through an example here

df1<-df%>%
  mutate(clr=c("#B3B3B9","#B3B3B9","#B3B3B9",
               "#C00000","#C00000","#C00000",
                "#66CC66","#66CC66","#66CC66",
                "#66CC66"))

df1
      Item sales     clr
1   Item_9 90.92 #B3B3B9
2   Item_4 74.38 #B3B3B9
3   Item_2 54.74 #B3B3B9
4   Item_5 53.48 #C00000
5   Item_1 39.77 #C00000
6   Item_3 39.17 #C00000
7  Item_10 38.78 #66CC66
8   Item_6 37.69 #66CC66
9   Item_8 37.13 #66CC66
10  Item_7 13.86 #66CC66


plot_tmap<-plot_ly(
  df1,
  type="treemap",
  labels=~Item,
  parents=~NA,
  values=~sales ,
  textinfo="label+value+percent",
  texttemplate = '<b>%{label}</br></br>%{value}</b>', 
  marker=list(colors=~clr))

plot_tmap


Step 6: Changing the font

plot_tmap%>%
   layout(uniformtext=list(minsize=12, 
                          mode='hide',
                          font_en="Arial"
                            ))


The aim of this short blog was to give a quick introduction to tree map and how we can create beautiful tableau type visualization in R.

No comments:

Post a Comment

Web Scraping Tutorial 4- Getting the busy information data from Popular time page from Google

Popular Times Popular Times In this blog we will try to scrape the ...