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=FALSE
andcontroltherays(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.