R file requires myFluc2

From Ucsbgalaxy
Revision as of 00:32, 19 November 2011 by Oakley (talk | contribs) (Created page with "myFluc2<-function (table, type = "size", floor = 0, ceiling = max(table$freq, na.rm = TRUE)) { if (is.table(table)) table <- as.data.frame(t(table)) oldnam…")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

myFluc2<-function (table, type = "size", floor = 0, ceiling = max(table$freq,

   na.rm = TRUE)) 

{

   if (is.table(table)) 
       table <- as.data.frame(t(table))
   oldnames <- names(table)
   names(table) <- c("x", "y", "result")
   table <- add.all.combinations(table, list("x", "y"))
   table <- transform(table, x = as.factor(x), y = as.factor(y), 
       freq = result)
   if (type == "size") {
       table <- transform(table, freq = sqrt(pmin(freq, ceiling)/ceiling), 
           border = ifelse(is.na(freq), "grey90", ifelse(freq > 
               ceiling, "grey30", "grey50")))
       table[is.na(table$freq), "freq"] <- 1
       table <- subset(table, freq * ceiling >= floor)
   }
   if (type == "size") {
       nx <- length(levels(table$x))
       ny <- length(levels(table$y))
       p <- ggplot(table, aes_string(x = "x", y = "y", height = "freq/2", 
           width = "freq/2", fill = "border")) + geom_tile(colour = "white") + 
           scale_fill_identity() + opts(aspect.ratio = ny/nx, legend.position="none") + xlab("") + ylab("")
   }
   else {
       p <- ggplot(table, aes_string(x = "x", y = "y", fill = "freq")) + 
           geom_tile(colour = "grey50") + scale_fill_gradient2(low = "white", 
           high = "darkgreen")
   }
   p$xlabel <- oldnames[1]
   p$ylabel <- oldnames[2]
   p

}