# Static and moving circles

[This article was first published on

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

**Last Resort Software**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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:

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

To

**leave a comment**for the author, please follow the link and comment on their blog:**Last Resort Software**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.