How to create a Bubble Plot

4

Anyone with experience in formulating this chart? I am very much in doubt, especially in the diameter of the circles and in the placement of the X-axis variable. I am a doctoral student at UFPE and many statisticians that I looked for did not know how to formulate the chart in question. I've always worked with SPSS and it's been a challenge to go into R.

In my case, follow the example:

On Y axis: The levels of functional disability.

Values:

  • Minimum Disability (0 to 20%),
  • Moderate disability (21-40%),
  • Severe disability (41-60%),
  • Disability (61-80%),
  • Bed Restricted (81 to 100%).
  • On axis X: Location and involvement of the pelvic joints.

    Values:

  • Pelvic Girdle Syndrome,
  • Sinfisiólise,
  • Unilateral Sacroiliac Syndrome,
  • Bilateral Sacroiliac Syndrome,
  • Miscellaneous.
  • ** Circles represent the intensity of pain (the larger the square, the greater the pain intensity). This variable ranges from 0 to 100mm (continuous variable).

        
    asked by anonymous 24.08.2015 / 00:04

    2 answers

    3

    The easiest way to do this in R is by using the symbols function. Since you did not provide specific data, I created some random, using the prob argument to cause an unequal distribution.

    n <- 50
    set.seed(0)
    y <- sample(c(1:10), n, replace = TRUE, prob = 10:1)
    x <- sample(1:5, n, replace = TRUE, prob = 1:5)
    r <- sample(1:100, n, replace = TRUE)
    

    To use the function, simply define the parameters with the x and y positions of each polygon, and in the case of circles, use the argument circle with the radius of the circles:

    symbols(x, y, circles = r)
    

    ThisfunctionworkslikemostgraphicalfunctionsofR.Youcansetmain,xlab,cex,etc.Youcanalsoaddotherelementsbyusingabline.

    Aspecialfeatureisthat,regardlessoftheheight/widthratio,circleswillbeplotted.Aspecialhighlightshouldbegiventothefollowingpartofthefunctionhelp:

      

    Argumentinchescontrolsthesizesofthesymbols.IfTRUE(thedefault),thesymbolsarescaledtothelargestdimensionofanysymbolisoneinch.

    Thatis,evenifyouchanger,thegreaterwillalwayshaveafixedfinalsize,whichishardlypleasing.Wecanuseinches=FALSEandcontroltherays(whichwillnowbeonthesamescaleasthex-axis):

    symbols(x,y,circles=r/300,inches=FALSE)

    Another important detail: The function creates the circles from the radius value, but we usually want the bubbles with area proportional to the measure. In that case it would be important to make the transformation. We just need to rearrange the circle area formula ( A = π * r² ):

    area <- sample(1:100, n, replace = TRUE)
    r <- sqrt(area / pi)
    

    In some cases the symbols function can be limiting, and using polygon() with basic trigonometry may be more appropriate, as long as you also control the proportions correctly. Another possibility is to use the ggplot2 package that certainly gives the message.

        
    24.08.2015 / 06:14
    2

    Look, the ideal is to provide along with the question at least some sample dataset.

    But I'll give you an example here, similar to yours, using ggplot2:

    ## Dados de exemplo
    dados = data.frame(x = sort(rep(seq(from = 1960, to = 2010, by = 5), 3)),
                       y = rep(1:3, 33),
                       raio = rnorm(33, mean = 2, sd = 3),
                       categoria = sample(x = 1:10, size = 33, replace = T))
    
    ## Colocando o tamanho das bolas pela categoria
    g = ggplot(data = dados, aes(y = y, x = x, size = categoria)) + geom_point()
    

    ##Colocandootamanhodasbolaspeloraio(variávelnumérica)g=ggplot(data=dados,aes(y=y,x=x,size=categoria,colour=raio))+geom_point()

    See that in ggplot2 you can map data characteristics, such as values for the x-axis, y-axis, color, size, and shape, into the aes () function. So to get the bubbles to have size depending on a variable is just put the size = variable variable.

    You should be able to customize this code for your purposes.

        
    24.08.2015 / 19:18