This is an htmlwidget port of Mike Bostocks’s circle packing visualization. The source code is available on Github.

Usage

This package can be downloaded from github with the following command:

devtools::install_github("jeromefroe/circlepackeR")

Currently, the circlepackeR function asccepts json data, hierarchical lists, and data.tree structures provided by the data.tree package. Furthermore, one can use the data.tree package to transform a data frame into a data.tree structure which can be passed as an argument to circlepackeR. The size argument accepts a string which gives the key that will be used to set the size of the circles in the visualization. The color_min and color_max arguments accept strings in the form of Hexadecimal, RGB, or HSL colors and set the minimum and maximum values for the color range of the circles.

Data Frame

Adapating the example from the data.tree vignette of converting a data frame to a tree provides an example of how to use a data frame with circlepackeR:

library(circlepackeR)
library(data.tree)
library(treemap)

data(GNI2010)
head(GNI2010)
##   iso3              country     continent population  GNI
## 1  ABW                Aruba North America        108    0
## 2  AFG          Afghanistan          Asia      34385  410
## 3  AGO               Angola        Africa      19082 3960
## 4  ALB              Albania        Europe       3205 3960
## 5  ARE United Arab Emirates          Asia       7512    0
## 6  ARG            Argentina South America      40412 8620
GNI2010$pathString <- paste("world", 
                            GNI2010$continent, 
                            GNI2010$country, 
                            sep = "/")
population <- as.Node(GNI2010)

circlepackeR(population, size = "population", color_min = "hsl(56,80%,80%)", 
             color_max = "hsl(341,30%,40%)")

JSON

url <- "https://gist.githubusercontent.com/mbostock/1093025/raw/05621a578a66fba4d2cbf5a77e2d1bb3a27ac3d4/flare.json"
circlepackeR(url)

Hierarchical List

hierarchical_list <- list(name = "World",
            children = list(
              list(name = "North America",
                   children = list(
                     list(name = "United States", size = 308865000),
                     list(name = "Mexico", size = 107550697),
                     list(name = "Canada", size = 34033000))),
              list(name = "South America", 
                   children = list(
                     list(name = "Brazil", size = 192612000),
                     list(name = "Colombia", size = 45349000),
                     list(name = "Argentina", size = 40134425))),
              list(name = "Europe",  
                   children = list(
                     list(name = "Germany", size = 81757600),
                     list(name = "France", size = 65447374),
                     list(name = "United Kingdom", size = 62041708))),
              list(name = "Africa",  
                   children = list(
                     list(name = "Nigeria", size = 154729000),
                     list(name = "Ethiopia", size = 79221000),
                     list(name = "Egypt", size = 77979000))),
              list(name = "Asia",  
                   children = list(
                     list(name = "China", size = 1336335000),
                     list(name = "India", size = 1178225000),
                     list(name = "Indonesia", size = 231369500)))
            )
)

circlepackeR(hierarchical_list)