pisistrato pisistrato - 1 month ago 8
R Question

Create annotation for a balloon plot from gplots package

I was wondering if it is possible to change the rows and columns annotation of a balloon plot generated by the gplots package.

Assuming my data are a subset of the mtcars dataset

data(mtcars)
dt <- as.table(as.matrix(mtcars[1:10,]))


I can make the balloon plot as follows

library("gplots")
balloonplot(t(dt), xlab ="", ylab="", label = FALSE, show.margins = FALSE)


Can I change the color of the gray bars (for both x and y) with an arbitrary color? For instance I want mpg, cyl, disp and drat in red, all the other in blue.

Is this possible? Or I need to look at another package?

Thanks

Answer Source

Download myballoonplot.r here and save it in your working directory.
Then, run the following code:

data(mtcars)
dt <- as.table(as.matrix(mtcars[1:10,]))

source("myballoonplot.r")

# Define colors for y bars 
col.bar.y <- rep("lightgray",ncol(dt))
col.bar.y[colnames(dt) %in% c("mpg","cyl","disp","drat")] <- "red"

# Define colors for x bars     
col.bar.x <- rep("lightgray",nrow(dt))
col.bar.x[rownames(dt) %in% c("Mazda RX4","Valiant","Duster 360")] <- "green"
col.bar.x[rownames(dt) %in% c("Datsun 710")] <- "#0000FF77"

# Plot using the modified version of balloonplot
myballoonplot.table(t(dt), xlab ="", ylab="", label = FALSE, 
    show.margins = FALSE, col.bar.y = col.bar.y, col.bar.x = col.bar.x)

enter image description here