Search code examples
rchartsbubble-chart

Bubble chart without Axis in R


I'd like to create such chart in R: http://bl.ocks.org/mbostock/4063269

so just a bubble chart without axis, where the bubbles can be randomly scattered and only characterized by the size argument.

I'm interested in doing that in R, where familar options to me requires providing x, y and size variables.


Solution

  • Here's one way using bubbles (it is based on htmlwidgets so it can be used from the R console, RStudio, R Markdown documents, and Shiny applications.):

    # devtools::install_github("jcheng5/bubbles")
    library(bubbles)
    
    bubbles(value = runif(26), label = LETTERS,
            color = rainbow(26, alpha=NULL)[sample(26)])
    

    Which gives:

    enter image description here


    Alternatively, you could use packcircles. From the documentation:

    The function circleProgressiveLayout arranges a set of circles, which are denoted by their sizes, by consecutively placing each circle externally tangent to two previously placed circles while avoiding overlaps. It was adapted from a version written in C by Peter Menzel.

    # install.packages("packcircles")
    library(packcircles)
    library(ggplot2)
    
    p <- circleProgressiveLayout(runif(26))
    d <- circleLayoutVertices(p)
    
    ggplot(d, aes(x, y)) + 
      geom_polygon(aes(group = id, fill = id), 
                   colour = "black", show.legend = FALSE) +
      geom_text(data = p, aes(x, y), label = LETTERS) +
      scale_fill_distiller(palette = "RdGy") +
      theme_void()
    

    Which gives:

    enter image description here