Introduction

Creating a Circle packing chart in R. This chart is also called as Circular Treemap A circle packing chart is a nice way to show the quantitative values for example the population of various countries.

Video link https://youtu.be/jKIR2-QpaI4

Packages used

#How to plot  a Circular Packing or Circular treemap using  GGPLOT
# Libraries

library(packcircles)
library(ggplot2)
library(dplyr)
library(reshape2)
library(scales)
library(tidyr)

Sample data

# Create data


d <- tidyr::population
d <- data.frame(d)
head(d)
FALSE       country year population
FALSE 1 Afghanistan 1995   17586073
FALSE 2 Afghanistan 1996   18415307
FALSE 3 Afghanistan 1997   19021226
FALSE 4 Afghanistan 1998   19496836
FALSE 5 Afghanistan 1999   19987071
FALSE 6 Afghanistan 2000   20595360
dMax <- d%>%
        dplyr::filter(year == max(year))%>%
        dplyr::arrange(-population)%>%
        head(20)


pck <- circleProgressiveLayout(dMax$population, sizetype='area')
head(pck)
FALSE             x         y    radius
FALSE 1 -21000.9411      0.00 21000.941
FALSE 2  19964.1782      0.00 19964.178
FALSE 3    255.4463 -22694.04 10093.330
FALSE 4    225.7062  21085.20  8918.223
FALSE 5  15807.6418  27639.45  7986.062
FALSE 6  31212.9476  25180.12  7614.315
# Following two datasets will be use in our plot
#----------------------------------------
mydata <- cbind(dMax, pck)
head(mydata)
FALSE                    country year population           x         y    radius
FALSE 1                    China 2013 1385566537 -21000.9411      0.00 21000.941
FALSE 2                    India 2013 1252139596  19964.1782      0.00 19964.178
FALSE 3 United States of America 2013  320050716    255.4463 -22694.04 10093.330
FALSE 4                Indonesia 2013  249865631    225.7062  21085.20  8918.223
FALSE 5                   Brazil 2013  200361925  15807.6418  27639.45  7986.062
FALSE 6                 Pakistan 2013  182142594  31212.9476  25180.12  7614.315
myPlotCord <- circleLayoutVertices(pck)
head(myPlotCord)
FALSE             x         y id
FALSE 1      0.0000     0.000  1
FALSE 2   -659.7832  5222.722  1
FALSE 3  -2597.6761 10117.281  1
FALSE 4  -5691.9139 14376.133  1
FALSE 5  -9748.0741 17731.681  1
FALSE 6 -14511.2934 19973.082  1
#----------------------------------------



# create the plot
# You would need two data sources
# one for the actual data and the other for the


library(randomcoloR)
n <- 20
palette <- randomColor(20, hue = "red", luminosity = "light")

Create the chart

See how we are using two datasets here. myPlotCord which contains the geometry details for drawing the cicles based on the population The second dataset called mydata has been used to create the labels.

pl <- ggplot()
pl <- pl + geom_polygon(data = myPlotCord, aes(x, y, group = id, fill= as.factor(id)))
pl <- pl + geom_text(data = mydata, aes(x, y, size=population, label = paste0(country, '\n', scales::comma(population))))
pl <- pl + scale_size_continuous(range = c(2,4))
pl <- pl+ coord_equal()
pl <- pl + theme_void()
pl <- pl + theme(legend.position="none")
pl <- pl + labs(title ="Circular packing chart")
pl <- pl + labs(subtitle ="Population")
pl <- pl + labs(caption =paste('For Year',max(mydata$year)))
pl <- pl + scale_fill_manual(values= palette)
pl