Last active
November 23, 2024 00:03
-
-
Save hakyim/11e1ba7019cf32025bff981de1a639db to your computer and use it in GitHub Desktop.
tile plot with var names
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require(ggplot2) | |
| tileplot <- function(mat) | |
| { | |
| mat = data.frame(mat) | |
| mat$Var1 = factor(rownames(mat), levels=rownames(mat)) ## preserve rowname order | |
| melted_mat <- gather(mat,key=Var2,value=value,-Var1) | |
| melted_mat$Var2 = factor(melted_mat$Var2, levels=colnames(mat)) ## preserve colname order | |
| rango = range(melted_mat$value) | |
| pp <- ggplot(melted_mat,aes(x=Var1,y=Var2,fill=value)) + geom_tile() ##+scale_fill_gradientn(colours = c("#C00000", "#FF0000", "#FF8080", "#FFC0C0", "#FFFFFF", "#C0C0FF", "#8080FF", "#0000FF", "#0000C0"), limit = c(-1,1)) | |
| pp | |
| } | |
| ## vertial x labels | |
| tileplot2 <- function(mat) | |
| { | |
| mat = data.frame(mat) | |
| mat$Var1 = factor(rownames(mat), levels=rownames(mat)) ## preserve rowname order | |
| melted_mat <- gather(mat,key=Var2,value=value,-Var1) | |
| melted_mat$Var2 = factor(melted_mat$Var2, levels=colnames(mat)) ## preserve colname order | |
| rango = range(melted_mat$value) | |
| pp <- ggplot(melted_mat,aes(x=Var1,y=Var2,fill=value)) + geom_tile() + ##+scale_fill_gradientn(colours = c("#C00000", "#FF0000", "#FF8080", "#FFC0C0", "#FFFFFF", "#C0C0FF", "#8080FF", "#0000FF", "#0000C0"), limit = c(-1,1)) | |
| theme(axis.text.x = element_text(angle = 90, vjust = 1, lineheight = 10, | |
| hjust = 1), | |
| axis.text.y = element_text(vjust = 0, | |
| hjust = 1), | |
| axis.title.x = element_blank(), | |
| axis.title.y = element_blank() | |
| , axis.ticks = element_blank() | |
| ) | |
| pp | |
| } | |
| tileplot_mat <- function(mat) | |
| { | |
| ## the tile plot has the same order as the matrix | |
| ## reverse the order of the rows | |
| nr=nrow(mat) | |
| mat = mat[nr:1,] | |
| mat = data.frame(mat) | |
| mat$Var1 = factor(rownames(mat), levels=rownames(mat)) ## preserve rowname order | |
| melted_mat <- gather(mat,key=Var2,value=value,-Var1) | |
| melted_mat$Var2 = factor(melted_mat$Var2, levels=colnames(mat)) ## preserve colname order | |
| rango = range(melted_mat$value) | |
| pp <- ggplot(melted_mat,aes(x=Var2,y=Var1,fill=value)) + geom_tile() + xlab("col") + ylab("row") ##+scale_fill_gradientn(colours = c("#C00000", "#FF0000", "#FF8080", "#FFC0C0", "#FFFFFF", "#C0C0FF", "#8080FF", "#0000FF", "#0000C0"), limit = c(-1,1)) | |
| pp | |
| } | |
| ##tileplot <- function(mat,orden) | |
| ##{ | |
| ## mat = data.frame(mat) | |
| ## mat$Var1 = rownames(mat) | |
| ## melted_mat <- gather(mat,key=Var2,value=value,-Var1) | |
| ## rango = range(melted_mat$value) | |
| ## pp <- ggplot(melted_mat,aes(x=Var1,y=Var2,fill=value)) + geom_tile() ##+ scale_fill_gradientn(colours = c("#C00000", "#FF0000", "#FF8080", "#FFC0C0", "#FFFFFF", "#C0C0FF", "#8080FF", "#0000FF", "#0000C0"), limit = c(-1,1)) | |
| ## print(pp) | |
| ##} | |
| ##require(reshape2) | |
| ##require(ggplot2) | |
| ##tileplot <- function(mat) | |
| ##{ | |
| ## melted_mat <- melt(mat) | |
| ## pp <- ggplot(melted_mat,aes(x=Var1,y=Var2,fill=value)) + geom_tile() | |
| ## print(pp) | |
| ##} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment