Circle packing in R (again)

By Michael Bedward

(This article was first published on Last Resort Software, and kindly contributed to R-bloggers)

Back in 2010 I posted some
And here’s the code:

# Create some random circles, positioned within the central portion
# of a bounding square, with smaller circles being more common than
# larger ones.

ncircles limits inset rmax
xyr x = runif(ncircles, min(limits) + inset, max(limits) - inset),
y = runif(ncircles, min(limits) + inset, max(limits) - inset),
r = rbeta(ncircles, 1, 10) * rmax)

# Next, we use the `circleLayout` function to try to find a non-overlapping
# arrangement, allowing the circles to occupy any part of the bounding square.
# The returned value is a list with elements for the layout and the number
# of iterations performed.


res cat(res$niter, "iterations performed")

# Now draw the before and after layouts with ggplot


## plot data for the `before` layout
## plot dta for the `after` layout returned by circleLayout
doPlot ggplot(dat) +
geom_polygon(aes(x, y, group=id), colour="brown", fill="burlywood", alpha=0.3) +
coord_equal(xlim=limits, ylim=limits) +
theme_bw() +
axis.title=element_blank()) +

doPlot(dat.before, "before"),
doPlot(dat.after, "after"),

To leave a comment for the author, please follow the link and comment on his blog: Last Resort Software. offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more…

Source:: R News

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.