Treemap is a hierarchical chart in which higher values are represented by bigger rectangles. Such a chart can be created in R by using treemap package.
For instance, we have the COVID-19 cases in Nepal by province and gender . The data looks like :
| SN | province | gender | cases | 
| 1 | Province_I | Male | 15614 | 
| 2 | Province_II | Male | 16066 | 
| 3 | Province_III | Male | 70088 | 
| 4 | Province_IV_ | Male | 8705 | 
| 5 | Province_V | Male | 16597 | 
| 6 | Province_VI | Male | 4480 | 
| 7 | Province_VII | Male | 8314 | 
| 8 | Province_I | Female | 8741 | 
| 9 | Province_II | Female | 3326 | 
| 10 | Province_III | Female | 44503 | 
| 11 | Province_IV_ | Female | 3732 | 
| 12 | Province_V | Female | 6997 | 
| 13 | Province_VI | Female | 1470 | 
| 14 | Province_VII | Female | 2842 | 
We can make a simple treemap by creating this data in R and saving it as 'datafile' object and following the commands below .
library(treemap) 
treemap(datafile,
        index="province",
        vSize="cases",
       title="Distribution of CoviD-19 Cases in Nepal",
        type="index"
)
It will produce a chart like below :
To plot the treemap with labels, we can do a simple trick : find totals by province and the plot the treemap.
 library(dplyr)
new<-datafile%>%
  group_by(province)%>%
  summarise(p_total=sum(cases))%>%
  mutate(newlab=paste(province, p_total, sep ="\n"))%>%
  treemap(index="newlab", vSize="p_total", 
          title="Disribution of COVID-19 Cases in Nepal",
          palette = "Reds",      
            fontsize.title=12                                 
           )
It will produce the map as shown below : 
Finally, we can group the chart with gender and show the map with province and gender. This can be done by :
treemap(datafile, index=c("province","gender"),    
        vSize="cases", type="index",
        fontsize.labels=c(15,12),                # 
        fontcolor.labels=c("white","blue"),    # 
        fontface.labels=c(2,1),                  #
        title="Disribution of COVID-19 Cases in Nepal",
        bg.labels=c("transparent"),              #
        align.labels=list(
          c("center", "center")), 
          c("right", "bottom"),
                                       # place of labels in the rectangle
        overlap.labels=0.5,                      
        inflate.labels=F  )                    # If true, labels are bigger when rectangle is bigger.
     
It will produce the following map in R .
The R script file is available here. 
 
No comments:
Post a Comment