tag:blogger.com,1999:blog-7556813435224291579.post2728759694555468181..comments2023-03-23T10:46:08.543+00:00Comments on One R Tip A Day: Statistical Visualizations - Part 2Paolohttp://www.blogger.com/profile/01969817827028660433noreply@blogger.comBlogger17125tag:blogger.com,1999:blog-7556813435224291579.post-32890738769583029212012-03-26T08:53:47.114+01:002012-03-26T08:53:47.114+01:00This post is a bit outdated. In order to produce b...This post is a bit outdated. In order to produce better bubble plot with all bells and whistles (legends, etc.)take a look at the following links referring to more updated information and better alternatives:<br /><br />http://flowingdata.com/2010/11/23/how-to-make-bubble-charts/<br />http://stackoverflow.com/questions/8131326/sp-r-package-and-missing-values<br />http://www.matthewmaenner.com/blog/?p=150<br /><br />and if you want to use Google Visualization API from R take a look at the googleVis package:<br />http://lamages.blogspot.it/2012/03/googlevis-0215-is-released-improved-geo.htmlPaolohttps://www.blogger.com/profile/01969817827028660433noreply@blogger.comtag:blogger.com,1999:blog-7556813435224291579.post-12971135408813557872012-03-26T00:09:34.152+01:002012-03-26T00:09:34.152+01:00Hi there,
Thank you for very useful information.
I...Hi there,<br />Thank you for very useful information.<br />I would like to ask for some help to make bubble-plot look better and more informative.<br /><br />1) How could put a "legend-like thing" where the size of bubble represents the percentage/number of data.<br />2) In the figure, there is an extra space or line in the beginning or end of the column line, ie, there is an extra empty column infront of the column-name "1820-30" and similarly an extra column at the end of column-name "2001/06". How can that gap be manipulated ?<br /><br />Thank you for your help in advance !Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7556813435224291579.post-34396153033876194692009-02-26T15:03:00.000+00:002009-02-26T15:03:00.000+00:00Dear ACH,Thanks for the point, it is quite agreeab...Dear ACH,<BR/>Thanks for the point, it is quite agreeable.<BR/>With these 2 consecutive posts I tried to recreate few visualizations I had found interesting and easy to reproduce using simple and short R code. <BR/>By now, I'm quite unsatisfied of the bubble plot that I think can be better reproduced taking advantage of the ggplot2 package (see, among the links, the website of Hadley Wickham ): a task for a future post...<BR/>For any other alternative/better visualization, any contribution is more than welcome, also as a regular poster if anybody is interested!Paolohttps://www.blogger.com/profile/01969817827028660433noreply@blogger.comtag:blogger.com,1999:blog-7556813435224291579.post-31877940674373318282009-02-26T14:20:00.000+00:002009-02-26T14:20:00.000+00:00Awesome site - added to my google reader :-)I thin...Awesome site - added to my google reader :-)<BR/><BR/>I think the immigration data will be best visualised with two (or three) different plots (latice like maybey). The top plot shows the absolute number of immigrants, the second shows number of immigrants as percentage of total inhabitants, and the third shows percentage of originating country for each year.<BR/><BR/>Just my 2 cents :-)<BR/><BR/>Will be readingACHhttps://www.blogger.com/profile/10155854149874397299noreply@blogger.comtag:blogger.com,1999:blog-7556813435224291579.post-79640750591155984962009-01-24T06:48:00.000+00:002009-01-24T06:48:00.000+00:00You're welcome!By the way, good use of italian! ;-...You're welcome!<BR/>By the way, good use of italian! ;-) <BR/>Bravo! :-)Paolohttps://www.blogger.com/profile/01969817827028660433noreply@blogger.comtag:blogger.com,1999:blog-7556813435224291579.post-45906914725973105732009-01-23T21:50:00.000+00:002009-01-23T21:50:00.000+00:00Hi Paolo, Fantastico, meraviglioso ora tutto funzi...Hi Paolo, <BR/><BR/>Fantastico, meraviglioso ora tutto funziona perfettamente! I simply did not see the name the "png" file was saved as. This R line to save a file is a "piĆ¹ utile" function because it saves us manual clicks to save the R image that is produced. <BR/><BR/>Grazie, JimTerrapinhttps://www.blogger.com/profile/07556378915868769419noreply@blogger.comtag:blogger.com,1999:blog-7556813435224291579.post-70367141430646409442009-01-23T15:39:00.000+00:002009-01-23T15:39:00.000+00:00Dear Jim,the code produces a .png image file in yo...Dear Jim,<BR/>the code produces a .png image file in your working directory (help("png"), help("dev") for more information). If you wish to see the plot in a windows graphic device simply use the below code (It is the identical code without the png() ... dev.off() stuff ):<BR/><BR/>library(RColorBrewer) # take a look at http://www.personal.psu.edu/cab38/ColorBrewer/ColorBrewer_intro.html<BR/># display.brewer.all()<BR/>FD.palette <- c("#984EA3","#377EB8","#4DAF4A","#FF7F00","#E41A1C")<BR/>options(scipen=10)<BR/>par(mar=c(6, 6, 3, 3), las=2)<BR/>data4bp <- t(original[,c(5,4,2,3,1)])<BR/>barplot( data4bp, beside=F,col=FD.palette, border=FD.palette, space=1, legend=F, ylab="Number of People", main="Migration to the United States by Source Region (1820 - 2006)", mgp=c(4.5,1,0) )<BR/>legend( "topleft", legend=rev(rownames(data4bp)), fill=rev(FD.palette) )<BR/>box()Paolohttps://www.blogger.com/profile/01969817827028660433noreply@blogger.comtag:blogger.com,1999:blog-7556813435224291579.post-76548836327967762252009-01-23T15:23:00.000+00:002009-01-23T15:23:00.000+00:00Hi Paolo, The file read might not be the issue. Bo...Hi Paolo,<BR/> <BR/>The file read might not be the issue. Both your code and my code produce identical R data frames on input. <BR/><BR/>Again, the code returns nothing. No errors except for the following at the very end. <BR/>> dev.off()<BR/>null device <BR/> 1<BR/><BR/>This rather intrigues me. It almost seems a simple statement is missing. But no, barplot does the work. Is barplot failing silently somehow? <BR/><BR/>My environment is MS XP SP 3, R 2.81 and my R packages are up to date. <BR/><BR/>Also my R successfully runs the examples found in R's help(barplot). And I appreciate that you put in the statement to display.brewer(all) which nicely shows what we can use. <BR/><BR/>Thanks,<BR/>JimTerrapinhttps://www.blogger.com/profile/07556378915868769419noreply@blogger.comtag:blogger.com,1999:blog-7556813435224291579.post-42528504943536094782009-01-23T08:34:00.000+00:002009-01-23T08:34:00.000+00:00Dear Jim,Don't worry, at the beginning R can s...Dear Jim,<BR/>Don't worry, at the beginning R can seem a bit <BR/>'idiosyncratic' ;-)<BR/><BR/>Try this:<BR/><BR/>Copy the below table and paste it in a editor(vim, emacs, nano, TextMate, not MS WORD):<BR/><BR/>Europe Asia Americas Africa Oceania<BR/>1820-30 106487 36 11951 17 33333<BR/>1831-40 495681 53 33424 54 69911<BR/>1841-50 1597442 141 62469 55 53144<BR/>1851-60 2452577 41538 74720 210 29169<BR/>1861-70 2065141 64759 166607 312 18005<BR/>1871-80 2271925 124160 404044 358 11704<BR/>1881-90 4735484 69942 426967 857 13363<BR/>1891-00 3555352 74862 38972 350 18028<BR/>1901-10 8056040 323543 361888 7368 46547<BR/>1911-20 4321887 247236 1143671 8443 14574<BR/>1921-30 2463194 112059 1516716 6286 8954<BR/>1931-40 347566 16595 160037 1750 2483<BR/>1941-50 621147 37028 354804 7367 14693<BR/>1951-60 1325727 153249 996944 14092 25467<BR/>1961-70 1123492 427642 1716374 28954 25215<BR/>1971-80 800368 1588178 1982735 80779 41254<BR/>1981-90 761550 2738157 3615225 176893 46237<BR/>1991-00 1359737 2795672 4486806 354939 98263<BR/>2001-06 1073726 2265696 3037122 446792 185986<BR/><BR/>Now in R type the below code, it should work (row.names=1 should make the trick):<BR/><BR/>original <- read.table("original.txt",row.names=1)<BR/>png("immigration_barplot_me.png", width = 1419, height = 736)<BR/>library(RColorBrewer) # take a look at http://www.personal.psu.edu/cab38/ColorBrewer/ColorBrewer_intro.html<BR/># display.brewer.all()<BR/>FD.palette <- c("#984EA3","#377EB8","#4DAF4A","#FF7F00","#E41A1C")<BR/>options(scipen=10)<BR/>par(mar=c(6, 6, 3, 3), las=2)<BR/>data4bp <- t(original[,c(5,4,2,3,1)])<BR/>barplot( data4bp, beside=F,col=FD.palette, border=FD.palette, space=1, legend=F, ylab="Number of People", main="Migration to the United States by Source Region (1820 - 2006)", mgp=c(4.5,1,0) )<BR/>legend( "topleft", legend=rev(rownames(data4bp)), fill=rev(FD.palette) )<BR/>box()<BR/>dev.off()Paolohttps://www.blogger.com/profile/01969817827028660433noreply@blogger.comtag:blogger.com,1999:blog-7556813435224291579.post-6738681299726911962009-01-23T03:44:00.000+00:002009-01-23T03:44:00.000+00:00Issues still with the barplot. inputting original ...Issues still with the barplot. <BR/>inputting original as below<BR/><BR/>original <- read.table("original.txt", header=TRUE, sep = "")<BR/><BR/>When I run your example in 2.8.1, I get no output and a "null device 1" after the last line. <BR/><BR/>My apologies as I am a bit new to R. <BR/><BR/>Thanks<BR/>JimTerrapinhttps://www.blogger.com/profile/07556378915868769419noreply@blogger.comtag:blogger.com,1999:blog-7556813435224291579.post-77519843325041173592009-01-22T12:58:00.000+00:002009-01-22T12:58:00.000+00:00Dear Jim,Thanks for the compliments! (*^_^*)You're...Dear Jim,<BR/>Thanks for the compliments! (*^_^*)<BR/>You're absolutely right about the original object. I included it in the first part of this 'series' but for an easier usage It's better to include it here too.<BR/>Any contribution is more than welcome! :-)Paolohttps://www.blogger.com/profile/01969817827028660433noreply@blogger.comtag:blogger.com,1999:blog-7556813435224291579.post-6829820474828177452009-01-22T06:57:00.000+00:002009-01-22T06:57:00.000+00:00Could you fix Immigration BarPlot? "original&...Could you fix Immigration BarPlot? "original" is not defined in the statement below. <BR/>data4bp <- t(original[,c(5,4,2,3,1)])<BR/><BR/>I came across this site tonight and absolutely love it! Keep up the wonderful work. I'll contribute when I have bright ideas. <BR/><BR/>Thanks so much too for link to PennState's NSF funded ColorBrewer. Kudos to Mark Harrower & Cindy Brewer! <BR/><BR/>Jim BurkeTerrapinhttps://www.blogger.com/profile/07556378915868769419noreply@blogger.comtag:blogger.com,1999:blog-7556813435224291579.post-82128313525327091412009-01-08T16:42:00.000+00:002009-01-08T16:42:00.000+00:00Ah, the elusive good-quality balloon or bubbleplot...Ah, the elusive good-quality balloon or bubbleplot. I have tried for ages to get this one to work (http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=60), but never get it work properly.Chaserhttps://www.blogger.com/profile/02124246378936489539noreply@blogger.comtag:blogger.com,1999:blog-7556813435224291579.post-36495010973821862562009-01-07T10:46:00.000+00:002009-01-07T10:46:00.000+00:00Yihui, thanks so much for your contribution!I real...Yihui, thanks so much for your contribution!<BR/>I really appreciate it!<BR/>I fixed my code taking advantage of your suggestions.<BR/>Regarding the "axes" warning:<BR/>I noticed the warning and decided to pass over it because the result was what I expected...<BR/>After your remark, I fixed it using xaxt="n", and yaxt="n".<BR/>Thanks again!Paolohttps://www.blogger.com/profile/01969817827028660433noreply@blogger.comtag:blogger.com,1999:blog-7556813435224291579.post-62779447281828906592009-01-06T16:03:00.000+00:002009-01-06T16:03:00.000+00:00Oh... my spaces were "removed" as I could not use ...Oh... my spaces were "removed" as I could not use the HTML tag "pre"...Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7556813435224291579.post-37377418758019370332009-01-06T15:59:00.000+00:002009-01-06T15:59:00.000+00:00and... yet another suggestion on "tidying up&...and... yet another suggestion on "tidying up" R code: <BR/><BR/>> library(animation)<BR/># copy the code to clipboard now, and<BR/>> tidy.source()<BR/>png("immigration_bubbleplot_me.png", width = 1400, <BR/> height = 400)<BR/>par(mar = c(3, 6, 3, 2))<BR/>mag = 0.9<BR/>original.vec <- as.matrix(original)<BR/>dim(original.vec) <- NULL<BR/>symbols(rep(1:dim(original)[[1]], dim(original)[[2]]), <BR/> rep(5:1, each = dim(original)[[1]]), circles = original.vec, <BR/> inches = mag, fg = "grey85", bg = "grey20", axes = F, ylab = "", <BR/> xlab = "", xlim = range(1:dim(original)[[1]]), ylim = c(1, <BR/> 6), main = "Immigration to the USA - 1821 to 2006")<BR/>axis(1, 1:dim(original)[[1]], labels = rownames(original), <BR/> las = 1, col = "grey85", tck = 1)<BR/>axis(2, 1:dim(original)[[2]], labels = rev(colnames(original)), <BR/> las = 1, col = "grey85", tck = 1)<BR/>box(col = "grey85")<BR/>symbols(rep(1:19, dim(original)[[2]]), rep(5:1, each = dim(original)[[1]]), <BR/> circles = original.vec, main = "", inches = mag, fg = "grey85", <BR/> bg = "grey20", axes = F, ylab = "", xlab = "", add = T, xlim = range(1:dim(original)[[1]]), <BR/> ylim = c(1, 6))<BR/>dev.off() <BR/># but a big disadvantage is you cannot keep the comments when using tidy.source()! :-(<BR/># any ideas on "reformatting R code"?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7556813435224291579.post-66848124869493532492009-01-06T15:55:00.000+00:002009-01-06T15:55:00.000+00:00For the bubble plot:1. I was warned as below using...For the bubble plot:<BR/><BR/>1. I was warned as below using R 2.8.1:<BR/><BR/>Warning message:<BR/>In symbols(rep(1:19, dim(original)[[2]]), rep(5:1, each = dim(original)[[1]]), :<BR/> "axes" is not a graphical parameter<BR/><BR/>2. I think dim(original)[[2]] can be replaced as ncol() (and number of rows to be nrow()) to make the code more tidy.<BR/><BR/>3. And another suggestion is to make use of the 'panel.first' argument to avoid redrawing the bubble plot if you want the grid lines, e.g.<BR/>symbols(1:10, 1:10, circles = runif(10), panel.first = grid())<BR/><BR/>this argument is actually from plot.default().Anonymousnoreply@blogger.com