# Static and moving circles

May 29, 2015
By

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

After the previous post on the packcircles package for R someone suggested it would be useful to be able to fix the position of selected circles. As a first attempt, I’ve added an optional `weights` argument to the `circleLayout` function. Weights can be in the range 0-1 inclusive, where a weight of 0 prevents a circle from moving, while a weight of 1 allows full movement. The updated code is at GitHub.

Here’s an example where the largest of a set of initially overlapping circles is fixed in place:

And here is the code for the example:

``library(packcircles)library(ggplot2)library(gridExtra)# Generate some random overlapping circlesncircles <- 200limits <- c(-50, 50)inset <- diff(limits) / 3rmax <- 20xyr <- data.frame(  x = runif(ncircles, min(limits) + inset, max(limits) - inset),  y = runif(ncircles, min(limits) + inset, max(limits) - inset),  r = rbeta(ncircles, 1, 10) * rmax)# Index of the largest circlelargest.id <- which(xyr\$r == max(xyr\$r))## Generate plot data for the `before` layoutdat.before <- circlePlotData(xyr)# Add a column to the plot data for the 'before' circles# to indicate whether a circle is static of free to movedat.before\$state <- ifelse(dat.before\$id == largest.id, "static", "free")# Run the layout algorithm with a weights vector to fix the position# of the largest circlewts <- rep(1.0, nrow(xyr))wts[ largest.id ] <- 0.0res <- circleLayout(xyr, limits, limits, maxiter = 1000, weights=wts)# A plot function to colour circles based on the state columndoPlot <- function(dat, title)  ggplot(dat) +   geom_polygon(aes(x, y, group=id, fill=state), colour="brown1") +  scale_fill_manual(values=c("NA", "brown4")) +  coord_equal(xlim=limits, ylim=limits) +  theme_bw() +  theme(axis.text=element_blank(),        axis.ticks=element_blank(),        axis.title=element_blank(),        legend.position="none") +  labs(title=title)g.before <- doPlot(dat.before, "before")# Generate a plot for the 'after' circlesdat.after <- circlePlotData(res\$layout)dat.after\$state <- ifelse(dat.after\$id == largest.id, "static", "free")g.after <- doPlot(dat.after, "after")grid.arrange(g.before, g.after, nrow=1)``

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...