Skip to content

Instantly share code, notes, and snippets.

@Ram-N
Created March 25, 2013 21:21
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 Ram-N/5240881 to your computer and use it in GitHub Desktop.
Save Ram-N/5240881 to your computer and use it in GitHub Desktop.
Correlation Heatmaps in ggplot2 with custom colors
#set up a coloring scheme using colorRampPalette
red=rgb(1,0,0); green=rgb(0,1,0); blue=rgb(0,0,1); white=rgb(1,1,1)
RtoWrange<-colorRampPalette(c(red, white ) )
WtoGrange<-colorRampPalette(c(white, green) )
p <- p + scale_fill_gradient2(low=RtoWrange(100), mid=WtoGrange(100), high="gray")
p
rm(list = ls())
library(ggplot2)
library(reshape2)
#help(package = "ggplot2")
data(movies)
movieGenres <- movies[c(18:23)] #subset to 6 genres
cor(movieGenres) # 6x6 cor matrix
#ggplot likes the data 'melted' one value per row
m <-melt(cor(movieGenres))
p <- ggplot(data=m, aes(x=Var1, y=Var2, fill=value)) + geom_tile()
p
getwd()
rm(list = ls())
library(ggplot2)
library(reshape2)
#help(package = "ggplot2")
data(movies)
movieGenres <- movies[c(18:23)] #subset to 6 genres
cor(movieGenres) # 6x6 cor matrix
#ggplot likes the data 'melted' one value per row
m <-melt(cor(movieGenres))
p <- ggplot(data=m, aes(x=Var1, y=Var2, fill=value)) + geom_tile()
p
#set up a coloring scheme using colorRampPalette
red=rgb(1,0,0); green=rgb(0,1,0); blue=rgb(0,0,1); white=rgb(1,1,1)
RtoWrange<-colorRampPalette(c(red, white ) )
WtoGrange<-colorRampPalette(c(white, green) )
##
p <- p + scale_fill_gradient2(low=RtoWrange(100), mid=WtoGrange(100), high="gray")
p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment