2

I am trying to plot many attributes in color bars beneath a dendrogram and am having trouble getting the positioning right (i.e. how to adjust y_scale and/or y_shift). The default plots 5 out of 25 color bars and setting y_shift=0.7 does allow all color bars to show up, although they cover the dendrogram.

I was wondering how you would change the last lines to get the spacing correct and how you came up with the correct adjustments? Thank you!

### LIBRARIES #################################################################
library(RColorBrewer);
library(amap);
library(dendextend);

### MADE UP DATA ##############################################################
N <- 200 # number samples
G <- 1000 # number of features
A <- 25 # number of attributes (# color_bars)

# data will make no sense, just for example
data <- matrix(rnorm(G*N,mean=0,sd=1), G, N);
data <- cov(data);
# fake binary attributes 
attributes <- matrix(sample(c(1,2), N*A, replace=TRUE), N, A);

### DENDOGRAM #################################################################
hc <- hcluster(data, method="correlation", link="average");
dend <- as.dendrogram(hc);
dend.idx <- labels(dend);

### COLOR BAR COLORS ##########################################################

# gather enough colors for all of the different attributes
color1 <- list(brewer.pal(12,"Set3"));
color2 <- list(brewer.pal(12, "Paired"));
color3 <- list(brewer.pal(8, "Dark2"));
color <- c(color1[[1]], color2[[1]], color3[[1]]);

# going to bin them all into 4 bins 
n <- 4;

# make A=25 color bars, each with a 4 shades of a single color
color.bar.colors <- NULL; 
for (count in 1:A){
    col.func <- colorRampPalette(c("white", color[count]));
    col.gradient <- col.func(n);
    col.item <- col.gradient[attributes[,count]];
    color.bar.colors <- cbind(color.bar.colors, col.item[dend.idx]);
}

### PLOT ######################################################################

svg("minimal-dend-example.svg", width=100, height = 10);

dend %>% 
    plot(main = "sample");
    colored_bars(
        colors = color.bar.colors, 
        dend = dend, 
        sort_by_labels_order = FALSE,
        # y_shift= 0.7
        );

dev.off();
Catalina
  • 171
  • 9
  • Oops, the 4 bins part is silly (I had started off with continuous attributes), but the code should work just the same. – Catalina Jun 26 '16 at 00:27

0 Answers0