R 向ggplot2热图添加树状图

R 向ggplot2热图添加树状图,r,colors,heatmap,dendrogram,R,Colors,Heatmap,Dendrogram,新的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

新的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
#使用GGPLOT和整形的Mtcars 2
安装程序包(“ggplot2”)
图书馆(GG2)
所有包装(“重塑2”)
图书馆(E2)
数据(mtcars)

汽车使用
gplots
软件包()中的
heatmap.2
功能,它会自动将树状图添加到热图中。以您的例子:

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)
install.packages(“gplots”)
图书馆(gplots)
数据(mtcars)

汽车这有点棘手,因为不是所有的零件都准备好了,但我开始工作的目的就是要达到这一点。如果您对此感兴趣,以便使用plotly创建带有树状图的交互式热图,那么您应该查看

如果你对静态热图感兴趣,我相信现有的软件包已经做得很好了,所以重新发明这个轮子可能不值得。 但如果这仍然是您想要做的,那么以下是主要步骤:

  • 生成树状图对象
  • 在ggplot2中绘制树状图对象
  • 按照树状图中的行(或列)顺序创建热图
  • 合并对象
  • 第1步可以使用
    hclust
    as.dendrogram
    ,第2步需要dendextend的
    [as.ggdend][2]
    函数。第3步可以使用heatmaply::heatmapr+heatmaply:::ggplot\u heatmap(它目前是隐藏的,但将来会为这种类型的东西而暴露)。第4步很棘手,我无法让它工作“足够好”,因为元素的比例不好

    我将其包装到一个新的ggheatmap函数中,并将其上载到。但这需要更多的工作,因此我愿意拉取请求。同时,以下是如何做到这一点:

    devtools::install_github("ropensci/plotly") # you will probably benefit from the latest version of plotly
    devtools::install_github('talgalili/heatmaply')
    
    library(heatmaply)
    x <- heatmapr(iris[,-5], scale = "column", colors = "Blues")
    ggheatmap(x)
    
    devtools::install_github(“ropensci/plotly”)#您可能会受益于plotly的最新版本
    devtools::安装\u github('talgalili/heatmaply'))
    图书馆(热图)
    
    x或尝试
    heatmap3
    功能:

    library(heatmap3)
    Cars <- mtcars[c(1:7)]
    heatmap3(cor(Cars), scale = "none", sym = T)
    
    库(热图3)
    
    汽车也许
    ggdendro
    软件包能帮你做到这一点?有一个很好的例子,使用
    ggdendro
    plotly
    @MattSandgren,我鼓励你看看dendextend。它有一个叉子,用于创建带有ggplot2的树状图,保存树的颜色和线宽等图形参数。请参阅此处:heatmap3很棒-但它不是基于GGPLOT2是的,你绝对正确。也许他不想重新发明轮子。否则你的方法似乎是a解决方案。同意。:(我也更愿意使用现有的解决方案之一)
    ggheatmap
    不再在
    heatmaply
    中,对吗?它在版本
    0.8.3
    中,但不在当前的
    0.11.1
    Hi@deeens中。这是正确的。ggheatmap函数被删除,因为它没有足够的抛光。但是,您可以使用“文件”将heatmaply交互输出保存到静态文件中“争论。我还添加了一个问题,可能会在将来添加热图。。。