将树状图添加到 ggplot2 热图中

Cha*_*tte 4 r colors dendrogram heatmap

新 R 用户在这里。我正在尝试向使用 ggplot2 创建的热图添加树状图。我怎样才能做到这一点?我已将代码添加到下面的热图中。

#Mtcars using ggplots and reshape2 
install.packages("ggplot2")
library(ggplot2)
intall.packages("reshape2")
library(reshape2)
data(mtcars)
Cars <- mtcars[c(1:7)] #subset to 6 genres

cor(Cars) # 6x6 cor matrix

#ggplot likes the data 'melted' one value per row
m <-melt(cor(Cars)) 
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); black=rgb(0,0,0)
RtoBrange<-colorRampPalette(c(red, black ) )
BtoGrange<-colorRampPalette(c(black, green) ) 

p <- p + scale_fill_gradient2(low=RtoBrange(100), mid="black",           high=BtoGrange(100))
p
Run Code Online (Sandbox Code Playgroud)

感谢您的帮助,

夏洛特

小智 5

使用包heatmap.2中的函数gplotshttps://cran.r-project.org/web/packages/gplots/gplots.pdf),它会自动将树状图添加到热图中。使用你的例子:

install.packages("gplots")
library(gplots)

data(mtcars)
Cars <- mtcars[c(1:7)]

mycolors <- colorRampPalette(c("red", "black", "green"))
heatmap.2(cor(Cars), trace = "none", col = mycolors)
Run Code Online (Sandbox Code Playgroud)