<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/bubble-packed-chart-with-r-using-packcircles-package/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://chichacha.netlify.com/2018/12/22/bubble-packed-chart-with-r-using-packcircles-package/"> R on Chi's Impe[r]fect Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="https://www.tableau.com/" target="_blank">Tableau</a> has chart type called “<a rel="nofollow" href="https://onlinehelp.tableau.com/current/pro/desktop/en-us/buildexamples_bubbles.htm" target="_blank">Packed Bubble Chart</a>”, while I haven’t really utilized packed bubble chart much, I always thought they are fun and beautiful. I wanted to try creating same chart using R, and I came across package called <a rel="nofollow" href="https://cran.r-project.org/web/packages/packcircles/index.html" target="_blank">packcircles</a>.</p>
<p>Reading vignettes was really helpful to figure out how to use the package!!<br>
– <a rel="nofollow" href="https://cran.r-project.org/web/packages/packcircles/vignettes/intro.html" target="_blank">introduction vignettes</a> – <a rel="nofollow" href="https://cran.r-project.org/web/packages/packcircles/vignettes/progressive_packing.html" target="_blank">Progressive packing vignettes</a> to get started on using this package.</p>
<div>
<h2>Creating Bubble Packed Abstract Art…</h2>
<p>I didn’t really have data sets handy to use it for this type of chart, so I’ve decided to play around with image. Initially I wanted to create images used to <a rel="nofollow" href="https://www.colour-blindness.com/colour-blindness-tests/ishihara-colour-test-plates/" target="_blank">test colour blindness</a>, but I wasn’t sure what colour palettes should be used to create these images.</p>
<p>Since I currently love <a rel="nofollow" href="https://support.apple.com/en-ca/HT208986" target="_blank">Memoji</a> on my iPhone, so I’ve decided I’d use Memoji image as base to create abstract art.</p>
<div>
<h3>Steps</h3>
<ul>
<li>Step 1. Import image and convert image to data frame, so you can extract colour value (RGB)</li>
<li>Step 2. Genearate circle packing layout using circleProgressiveLayout function. The resulting data frame here contains center points of circle (x,y) and its radius.</li>
<li>Step 3. Convert x &amp; y coordinate from data frame in step 2 so that you can figure out what colour to fill the circle. i.e. Data Frame from Step 1 and Step 2 should be joined, so you need to adjust the scaling.</li>
<li>Step 4. Create data frame using circleLayoutVertices function for plotting with ggplot2. The resulting data frame now have specified amount of points per indivisual circle so that you can use geom_path or geom_polygon to draw.
</li>
<li>Step 5. Join data from Step 4 with colour value from Step 3, so that you can use geom_polygon with fill value to colour the circle!</li>
</ul>
<pre><code>library(tidyverse) ## I need tidyverse for everything :) 
library(imager) ## to create data frame from image
library(scales) ## rescale function is so handy!
library(packcircles) ## making circle packing easy! 

## Step 1
im &lt;- load.image(&quot;https://farm5.staticflickr.com/4868/45503751845_948f121563_z.jpg&quot;) #memoji2


## if you want to take a look at image.. :) 
#plot(im)

## Convert Image into Data Frame
im.df.colour &lt;- im %>%
  as.data.frame(wide=&quot;c&quot;) %>% ## so that rgb value is in separate column.
  rename(im_x=x,im_y=y) %>%
  mutate(hex=rgb(c.1,c.2,c.3))

## Step 2 using circleProgressiveLayout function.
## Generate circle packing layout using rbeta distribution as size of circles
pack_layout &lt;- circleProgressiveLayout(rbeta(2000,1,2), sizetype='area') %>% 
## Step 3 - I want to figure out what colour to use, so I want layout &amp; image df to have same scaling. 
  mutate(im_x=floor(rescale(x,to=range(im.df.colour$im_x))),  
         im_y=floor(rescale(y,to=range(im.df.colour$im_y))),
         ## also generate id, so i can join the data frame easily later!
         id=row_number()) %>% 
  inner_join(im.df.colour %>% select(im_x,im_y,hex), by=c(&quot;im_x&quot;,&quot;im_y&quot;))


## Step 4 
## Using the layout above create data frame using circleLayoutVertices function so that you can plot circle using ggplot2
data_gg &lt;- circleLayoutVertices(pack_layout) %>% 
  inner_join(pack_layout %>% select(id,hex), by=c(&quot;id&quot;))

## Step 5
data_gg %>% 
  ggplot(aes(x=x,y=y,group=id)) +
  geom_polygon(aes(fill=hex)) +  
  scale_fill_identity() + 
  coord_equal() +
  scale_y_reverse() +  ## you need to reverse y-axis
  theme_void() </code></pre>
<p><img src="https://i1.wp.com/chichacha.netlify.com/post/2018-12-21-bubble-packed-chart-with-r-using-packcircles-package_files/figure-html/unnamed-chunk-1-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<div>
<img alt="Just few more of these…" src="https://i2.wp.com/chichacha.netlify.com/post/2018-12-21-bubble-packed-chart-with-r-using-packcircles-package_files/MemojiArt.png?w=456&ssl=1" data-recalc-dims="1">
<p>Just few more of these…</p>
</div>
<p>Good image to use is square image with above, but it’s fun turning into logo &amp; images!!!</p>
<p>Below are just few more things I’ve experimented with circle packing technique…</p>
</div>
</div>
<div>
<h2>Experimenting with RGB Colour…</h2>
<pre><code>## Generate layout from 500 uniformly distributed number 0 to 1 as area.
pack_layout1 &lt;- circleProgressiveLayout(runif(n=500), sizetype='area')

## I want to colour each circle with different rgb value, so I'll append data 
pack_layout1 &lt;- pack_layout1 %>% 
  mutate(hex_r=rgb(1,rescale(x),rescale(y),rescale(radius)),
         hex_g=rgb(rescale(x),1,rescale(y),rescale(radius)),
         hex_b=rgb(rescale(x),rescale(y),1,rescale(radius)),
         id = row_number()) ## append id, so you can join this table later. 

## pack_layout1 contains data where center of circle should be placed with its radius.
## Now generate data so that you can actually draw circle using ggplot2
data_gg1 &lt;- circleLayoutVertices(pack_layout1, npoints=25) 
## notice now you have for each circle, you have 25 x and y coordinates to draw circle!


## Since the colour I want to use for each circle is retained in pack_layout1 data frame, I want to combine the info.  Also I want to create 3 sets of different colouring. I want to make long table. 
data_gg1 &lt;- data_gg1 %>% inner_join(pack_layout1 %>% select(-x,-y), by=c(&quot;id&quot;))

## I want to create 3 different coloured variations, so convert above table to long format. 
data_gg_long &lt;- data_gg1 %>% 
  gather(key=&quot;colour_group&quot;,value=&quot;hex&quot;,hex_r:hex_b) %>%
  mutate(colour_group = factor(colour_group,levels=c(&quot;hex_r&quot;,&quot;hex_g&quot;,&quot;hex_b&quot;), 
                               labels=c(&quot;keeping red value constant\nmore green to the right - more blue to the top\nsmaller circle has more transparency&quot;,
                                       &quot;keeping green value constant\nmore red to the right - more blue to the top\nsmaller circle has more transparency&quot;,
                                       &quot;keeping blue value constant\nmore red to the right - more green to the top\nsmaller circle has more transparency&quot;)))


## Now the fun part!
data_gg_long %>%
  ggplot(aes(x=x,y=y)) + 
  geom_polygon(aes(group=id),fill=&quot;#ffffff&quot;) +  ## first draw all circle white.
  geom_polygon(aes(group=id, fill=hex)) + ## then colour with value with some transparency
  coord_equal() +
  theme_void() +
  scale_fill_identity() +
  scale_y_reverse() +
  facet_wrap(~colour_group) +
  theme(plot.background=element_rect(fill=&quot;#000000de&quot;),
        strip.text=element_text(family=&quot;Roboto Condensed&quot;, color=&quot;#ffffffde&quot;))</code></pre>
<p><img src="https://i2.wp.com/chichacha.netlify.com/post/2018-12-21-bubble-packed-chart-with-r-using-packcircles-package_files/figure-html/experiment_with_rgba-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
</div>
<div>
<h1>Drawing Smaller Circles Inside of Circles</h1>
<pre><code>## Instead of using uniform distribution, used beta distribution this time! 
pack_layout2 &lt;- circleProgressiveLayout(rbeta(1000,1,1), sizetype='area')

## This time I want to fill circle using hue value...
pack_layout2  &lt;- pack_layout2 %>% 
  mutate(r = sqrt(x^2 + y^2), ## calculate distance from 0,0 coordinate 
         angle_t = atan2(y,x),  ## The arc-tangent of two arguments atan2(y, x) returns the angle between the x-axis and the vector from the origin to (x, y)
         angle = rescale(angle_t, from=c(-pi,pi)), ## convert theta value to value betwwen 0 and 1
         hex = hsv(h=angle, s=rescale(r), v=0.8),
         id = row_number())

## use circleLayoutVertices function to generate data frame for ggplot2 &amp; bring colour info.
data_gg2 &lt;- circleLayoutVertices(pack_layout2,npoints=25) %>%
  inner_join(pack_layout2 %>% select(-x,-y), by=c(&quot;id&quot;)) 

## Now create data for inner circles!! But I'm sampling so that NOT all circle has inner circles! I want to pick more bigger circles than smaller circle, so using raidus as weight to sample. 
data_gg2_1 &lt;- circleLayoutVertices(pack_layout2 %>% 
                                     sample_n(800, weight=radius) %>%
                                     mutate(radius=0.7*radius), npoints=25) ## I want to draw smaller circle, so shrink the radius
data_gg2_2 &lt;- circleLayoutVertices(pack_layout2 %>% 
                                     sample_n(700,weight=radius) %>%
                                     mutate(radius=0.5*radius),npoints=25) 
data_gg2_3 &lt;- circleLayoutVertices(pack_layout2 %>% 
                                     sample_n(900,weight=radius) %>%
                                     mutate(radius=0.3*radius),npoints=25)

## Draw Black and White Version  
bw &lt;-data_gg2 %>% 
  ggplot(aes(x=x,y=y, group=id)) + 
  geom_path(data=data_gg2, size=0.5, color=&quot;#00000090&quot;) +
  geom_path(data=data_gg2_1,size=1, color=&quot;#00000090&quot;) +
  geom_path(data=data_gg2_2,size=0.5, color=&quot;#00000090&quot;) +
  geom_path(data=data_gg2_3,size=0.5, color=&quot;#00000090&quot;) +
  scale_fill_identity() +
  scale_color_identity() +
  theme_void() +
  coord_fixed()

## Draw colourful version 
hue &lt;-data_gg2 %>% 
  ggplot(aes(x=x,y=y, group=id)) + 
  geom_polygon(aes(fill=hex)) +  
  geom_path(data=data_gg2, size=0.5, color=&quot;#ffffff90&quot;) +
  geom_path(data=data_gg2_1,size=1, color=&quot;#ffffff90&quot;) +
  geom_path(data=data_gg2_2,size=0.5, color=&quot;#ffffff90&quot;) +
  geom_path(data=data_gg2_3,size=0.5, color=&quot;#ffffff90&quot;) +
  scale_fill_identity() +
  scale_color_identity() +
  theme_void() +
  coord_fixed()

library(patchwork)
bw + hue</code></pre>
<p><img src="https://i0.wp.com/chichacha.netlify.com/post/2018-12-21-bubble-packed-chart-with-r-using-packcircles-package_files/figure-html/design_with_inner_circle-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://chichacha.netlify.com/2018/12/22/bubble-packed-chart-with-r-using-packcircles-package/"> R on Chi's Impe[r]fect Blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/D5eFkARw6tY" height="1"></a></div>