Balloon plot using ggplot2

March 19, 2010
By

(This article was first published on One R Tip A Day, and kindly contributed to R-bloggers)

Following Tal Galili example and using part of his code, I want to plot the balloonplot you can see here using R and the excellent ggplot2 package by Hadley Wickham.

### I retrieve the data from the google document you can find here using Tal Galili code:
## I slightly modified Tal code to include popularity stats:
supplement.popularity <- supplements.data[ss,7]
supplements.df <- na.omit(data.frame(supplement.name, supplement.benefits, supplement.popularity, supplement.score)) ## remove rows containing NAs
colnames(supplements.df) <- c("name", "benefits", "popularity", "score")
## For sake of simplicity I select only the cardio metacondition
cardio <- (supplements.df[supplement.benefits=="cardio",])[, -2]
## For reproducibility I add the cardio data.frame so you can use it right away
cardio <- read.table(tc <-textConnection(
" name popularity score
2 'arginine' 1.080 3
10 'vitamin b3' 0.201 3
15 'omega 3' 4.000 3
22 'hawthorn' 0.442 4
27 'red yeast rice' 0.264 4
29 'vitamin d' 6.700 4
31 'omega 6' 2.000 4
35 'green tea' 26.100 5
37 'olive leaf' 0.224 5
41 'fish oil' 4.000 6
43 'red yeast rice' 0.264 6")); close(tc)
cardio$name <- gsub(" ", "\n", cardio$name) #substitute ' ' with '\n' in the names
library(ggplot2)
myTheme <- function(base_size = 10) {
structure(list(
panel.background = theme_rect(size = 1, colour = "lightgray"),
panel.grid.major = theme_blank(),
panel.grid.minor = theme_blank(),
axis.line = theme_blank(),
axis.text.x = theme_blank(),
axis.ticks = theme_blank(),
strip.background = theme_blank(),
strip.text.y = theme_blank(),
legend.background = theme_blank(),
legend.key = theme_blank(),
legend.key.size = unit(1.2, "lines"),
legend.title = theme_text(size = 8, face = "bold", hjust = 0),
legend.position = "right"
), class = "options")
}
s <- ggplot(cardio, aes(name, score)) + xlab(NULL) + ylab(NULL) + myTheme()
s <- s + geom_point( aes(size=popularity, colour=score, fill=score), legend=TRUE) +
scale_y_continuous( breaks=as.numeric(levels(factor(cardio$score))), labels=c("Conflicting", "Promising", "Good", "Strong") ) +
scale_area( breaks=c(min(cardio$popularity),mean(cardio$popularity),max(cardio$popularity)), to=c(4,60) ) +
geom_text(aes(y=cardio$score, label=cardio$name, size=cardio$popularity/90), legend=FALSE)
#pdf("cardio.pdf",height=8,width=12);s;dev.off()
png("cardio.png",height=700,width=1000);s;dev.off()

To leave a comment for the author, please follow the link and comment on his blog: One R Tip A Day.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.