【发布时间】:2016-12-08 05:09:20
【问题描述】:
这里是新的 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
感谢您的帮助,
夏洛特
【问题讨论】:
-
也许
ggdendro包可以帮助您解决这个问题? -
有一个很好的例子here 使用
ggdendro和plotly -
@MattSandgren 我鼓励你看看dendextend。它有一个用于使用 ggplot2 创建树状图的叉子,它保留了图形参数,例如树的颜色和线宽。见这里:cran.r-project.org/web/packages/dendextend/vignettes/…
标签: r colors heatmap dendrogram