Skip to content

Instantly share code, notes, and snippets.

@hakyim
Last active January 21, 2021 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hakyim/11e1ba7019cf32025bff981de1a639db to your computer and use it in GitHub Desktop.
Save hakyim/11e1ba7019cf32025bff981de1a639db to your computer and use it in GitHub Desktop.
tile plot with var names
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))
print(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()
)
print(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