R Resources‎ > ‎

### Bubble scatterplots for overlapping data

 Ever have this problem? You are doing a regression on your data, but you have overlapping data, so what comes out in the figure doesn't accurately reflect the analysis you want to convey.Here's an example:`# A sample dataset of two variables, a and ba=c(1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5)``b=c(2,2,2,4,8,2,4,3,7,4,4,4,6,10,6,6,7,8,8,8,3,8,10,2,8,10,10)``dat=as.data.frame(cbind(a,b),colnames=c("a","b"))dat`   a  b1  1  22  1  23  1  24  1  45  1  86  1  27  2  48  2  39  2  710 2  411 2  412 2  413 3  614 3 1015 3  616 3  617 3  718 4  819 4  820 4  821 4  322 4  823 5 1024 5  225 5  826 5 1027 5 10You can easily show that there is a strong linear relationship between a and b:`mod=lm(dat\$b~dat\$a,data=dat)``summary(mod)`...gives you:Call:lm(formula = dat\$b ~ dat\$a, data = dat)Residuals:    Min      1Q  Median      3Q     Max -6.3394 -1.0925  0.0874  0.9807  4.5142 Coefficients:            Estimate Std. Error t value Pr(>|t|)    (Intercept)   2.2724     0.9785   2.322 0.028651 *  dat\$a         1.2134     0.3038   3.993 0.000504 ***---Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 2.247 on 25 degrees of freedomMultiple R-squared: 0.3895,    Adjusted R-squared: 0.365 F-statistic: 15.95 on 1 and 25 DF,  p-value: 0.0005038 However, if you simply plot this data:`plot(a,b,pch=20)` ... seems not terribly convincing. This is because the scatterplot is hiding all the overlapping data points that are important to your analysis. So here is the code to produce a better figure, which scales the size of the "bubble" points according to the number of data points:`# Sample data``a=c(1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5)``b=c(2,2,2,4,8,2,4,3,7,4,4,4,6,10,6,6,7,8,8,8,3,8,10,2,8,10,10)``dat=as.data.frame(cbind(a,b),colnames=c("a","b"))``dat``# Create an "aggregated" dataset that has three columns: variable a, variable b, and the number of samples that have that combination of values. (you might notice that "newdat" actually has four columns... that is due to the inelegance of my code... but whatever, it does the trick)``newdat=aggregate(dat,by=list(a,b),length)``colnames(newdat)=c("a","b","N")``newdat``# a bubble plot of "newdat"``symbols(newdat\$a,newdat\$b,circles=newdat\$N/20,inches=FALSE,xlab="variable a",ylab="variable b")``# a linear model of the relationship between a and b, from the original dataset ("dat")``mod=lm(dat\$b~dat\$a,data=dat)``summary(mod)``# draw the regression line on the bubble plot ``lines(dat\$a,predict(mod),lty=1,lwd=2)` Comments