5

I would like to reproduce the following plot in R:

bubble chart

I am now using the package packcircles and I am able to create the plot in the figure:

packcircles plot

using the following code:

require(packcircles)
res <- circleLayout(xyr, limits, limits, maxiter = 1000)
dat <- circlePlotData(res$layout)

doPlot <- function(dat, title)
ggplot(dat) + 
geom_polygon(aes(x, y, group=id), colour="tomato", fill="tomato", alpha=0.3) +
coord_equal(xlim=limits, ylim=limits) +
theme_bw() +
theme(axis.text=element_blank(),
    axis.ticks=element_blank(),
    axis.title=element_blank()) +
labs(title=title)

doPlot(dat, "")

where xyr is a dataframe of the form:

          x           y    r
1  2.897344 -10.4161236 0.57
2  1.932411  13.0631120 0.52
3  6.839500 -11.0507209 0.52
4 11.117047 -17.6440597 0.77
5 22.395529  -0.6191146 0.45
6 20.313309 -13.3292887 0.69

and the output dat of the form:

         x         y id
1 10.72973 -14.18673  1
2 10.71182 -14.04498  1
3 10.65923 -13.91213  1
4 10.57524 -13.79654  1
5 10.46515 -13.70546  1
6 10.33587 -13.64463  1

It's a good start, but I need to label and color the circles according to a certain variable. Apparently, the rows in dat are not in the same order as in the input dataframe. Therefore, I'm note able to add any other information to dat (e.g., a new variable) to be used to group circles by color in ggplot2, because I lost any references to the original data.

Following the suggestion of @rawr, I used bubbles package to reproduce the plot with the following code:

require(bubbles)
text = as.character(seq(from = 1, to = 476))
bubbles(value = count,
        color = rainbow(length(count), alpha=NULL)[sample(length(count))],
        label = text,
        width=1600, height=1600)

where count is a numeric vector in [0,1]. The output is the following:

bubbles plot

The library allows to provide a different color for each circle, but it's not possible to color circles by group. Also, it allows for setting labels in a very straightforward way, differently from packcircles. Anyway, I couldn't export the plot directly from R, but I had to output it as a part of an R Markdown document.

Zetina
  • 87
  • 1
  • 7
  • What have you tried so far? Please provide additional details in your question as to this and clarify "I know how to make a bubble chart in R." with package names and code. – lmo Jun 15 '16 at 12:21
  • 2
    You are looking for "circle packing". Check out the `packcircles` package - [here](http://lastresortsoftware.blogspot.de/2015/07/graph-based-circle-packing.html) is an example. – lukeA Jun 15 '16 at 12:21
  • thanks @lukeA, I've just found the same package! – Zetina Jun 15 '16 at 12:22
  • 1
    [this package](https://github.com/jcheng5/bubbles) is using d3/htmlwidgets – rawr Jun 15 '16 at 18:12
  • thanks @rawr, bubbles seems to be a good solution. Anyway, I'm not able to export the plot in PNG or PDF format from RStudio. Any ideas? – Zetina Jun 16 '16 at 10:14
  • @Zetina those are widgets not images, but there are some ideas [here](https://github.com/ramnathv/htmlwidgets/issues/95). the "export image as" option seems easiest, but it takes a crappy screenshot for me which I could just do on my own better – rawr Jun 16 '16 at 12:47
  • Late reply, but in case it's still of interest the latest version of packcircles includes a new [vignette](https://cran.r-project.org/web/packages/packcircles/vignettes/progressive_packing.html) with an example where circles have pre-defined colours. – michael Apr 08 '17 at 10:41

0 Answers0