Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R-使用Pheatmap时的图例标题或单位_R_Heatmap_Pheatmap - Fatal编程技术网

R-使用Pheatmap时的图例标题或单位

R-使用Pheatmap时的图例标题或单位,r,heatmap,pheatmap,R,Heatmap,Pheatmap,我正在使用pheatmap创建值的热图,并希望用矩阵中z值的单位标记图例。在本例中,我希望图例顶部显示温度[°C]。我已经阅读了pheatmap的说明,对图例的唯一操作似乎是添加一个默认数字列表来显示比例。我看不到任何添加图例标题本身的选项 下面是一个通用代码块,用于使用pheatmap生成矩阵和绘图。我真的很感激任何关于如何为传奇添加标题的建议 test <- matrix(rexp(200, rate=.1), ncol=20) colnames(test) = paste("Room

我正在使用pheatmap创建值的热图,并希望用矩阵中z值的单位标记图例。在本例中,我希望图例顶部显示温度[°C]。我已经阅读了pheatmap的说明,对图例的唯一操作似乎是添加一个默认数字列表来显示比例。我看不到任何添加图例标题本身的选项

下面是一个通用代码块,用于使用pheatmap生成矩阵和绘图。我真的很感激任何关于如何为传奇添加标题的建议

test <- matrix(rexp(200, rate=.1), ncol=20)
colnames(test) = paste("Room", 1:20, sep = "")
rownames(test) = paste("Building", 1:10, sep = "")

pheatmap(test, legend = TRUE, cluster_rows = FALSE, cluster_cols = FALSE)

test好的,既然还没有人回答这个问题,如果您必须使用pheatmap函数,我将给您一个可能的选项。这是更容易做到的使用
ggplot,但它是这样的:

首先,我们将要生成我们的绘图,这样我们就可以使用所有的绘图对象来创建我们自己的带有编辑图例的绘图

#Edited to add in library names
library(gtable)
library(grid)

#Starting with data and generating initial plot
test <- matrix(rexp(200, rate=.1), ncol=20)
colnames(test) = paste("Room", 1:20, sep = "")
rownames(test) = paste("Building", 1:10, sep = "")

p<-pheatmap(test, legend = TRUE, cluster_rows = FALSE, cluster_cols = FALSE)



#Get grobs we want - will use these to create own plot later
plot.grob <- p$gtable$grob[[1]]
xlab.grob <- p$gtable$grob[[2]]  
ylab.grob <- p$gtable$grob[[3]]  
legend.grob <- p$gtable$grob[[4]]  
现在我们实际上需要构建gtable对象。为此,我们将使用与pheatmap函数生成的绘图类似的布局(经过一些修改)。还要注意,pheatmap函数生成一个gtable对象,可通过以下方式访问:

p$gtable
为了查看gtable对象中每个“扇区”的宽度/高度,我们需要做的是:

p$gtable$heights
p$gtable$widths
这些将作为我们的参考值。要获得更图形化的显示,请尝试:

gtable_show_layout(p$gtable)
这将产生以下图像:

好的,现在我们有了我们想要的Grob,我们需要做的就是根据我们看到的由pheatmap构建的GTTable来构建我们的GTTable。我编写的一些示例代码是:

my_new_gt <- gtable(widths=  unit.c(unit(0,"bigpts") + unit(5,"bigpts"),
                                    unit(0,"bigpts"),
                                    unit(1,"npc") - unit(1,"grobwidth",plot.grob) + unit(10,"bigpts") - max(unit(1.1,"grobwidth",plot.grob), (unit(12,"bigpts")+1.2*unit(1.1,"grobwidth",plot.grob))) + unit(5,"bigpts") - unit(3,"inches"),
                                    unit(1,"grobwidth",ylab.grob) + unit(10,"bigpts"),
                                    max(unit(1,"grobwidth",legend.grob2),unit(12,"bigpts")+1.2*unit(1.1,"grobwidth",legend.grob2)) + unit(1,"inches") ,
                                    max(unit(0,"bigpts"),unit(0,"bigpts"))
                                    ),
                                    
                    
                    
                    height = unit.c(unit(0,"npc"),
                                    unit(5,"bigpts"),
                                    unit(0,"bigpts"),
                                    unit(1,"npc") - unit(1,"grobheight",xlab.grob) + unit(15,"bigpts") - unit(0.2,"inches"),
                                    unit(1,"grobheight",xlab.grob) + unit(15,"bigpts")     
                      ))

my_new\u gtMikeyMike的回答令人难以置信;通过阅读,我也学到了很多

然而,我需要一个愚蠢、丑陋、10秒的解决方案:

test <- matrix(rexp(200, rate=.1), ncol=20)
colnames(test) = paste("Room", 1:20, sep = "")
rownames(test) = paste("Building", 1:10, sep = "")

pheatmap(test, legend_breaks = c(10, 20, 30, 40, max(test)), 
main = "", legend_labels = c("10", "20", "30", "40", "title\n"),
legend = TRUE, cluster_rows = FALSE, cluster_cols = FALSE)

test很抱歉我的回复太晚了,但非常感谢您的回复。你的回答非常详细,帮助我更好地理解地图!我本来想问你ggplot和pheatmap的区别,但你已经回答了!我喜欢pheatmap的一点是,它可以通过将NA值绘制为不同于比例的颜色来处理NA值。这对我来说很有用,因为我正在处理一个具有一些NA值的数据集。虽然我还没有尝试过ggplot。再次非常感谢。
gtable_show_layout(p$gtable)
my_new_gt <- gtable(widths=  unit.c(unit(0,"bigpts") + unit(5,"bigpts"),
                                    unit(0,"bigpts"),
                                    unit(1,"npc") - unit(1,"grobwidth",plot.grob) + unit(10,"bigpts") - max(unit(1.1,"grobwidth",plot.grob), (unit(12,"bigpts")+1.2*unit(1.1,"grobwidth",plot.grob))) + unit(5,"bigpts") - unit(3,"inches"),
                                    unit(1,"grobwidth",ylab.grob) + unit(10,"bigpts"),
                                    max(unit(1,"grobwidth",legend.grob2),unit(12,"bigpts")+1.2*unit(1.1,"grobwidth",legend.grob2)) + unit(1,"inches") ,
                                    max(unit(0,"bigpts"),unit(0,"bigpts"))
                                    ),
                                    
                    
                    
                    height = unit.c(unit(0,"npc"),
                                    unit(5,"bigpts"),
                                    unit(0,"bigpts"),
                                    unit(1,"npc") - unit(1,"grobheight",xlab.grob) + unit(15,"bigpts") - unit(0.2,"inches"),
                                    unit(1,"grobheight",xlab.grob) + unit(15,"bigpts")     
                      ))
#Adding each grob to the appropriate spot
gtable <- gtable_add_grob(my_new_gt,plot.grob,4,3)
gtable <- gtable_add_grob(gtable,xlab.grob,5,3)
gtable <- gtable_add_grob(gtable,ylab.grob,4,4)
gtable <- gtable_add_grob(gtable,legend.grob2,4,5)

grid.draw(gtable)
library(ggplot2)
library(reshape)
test <- as.data.frame(matrix(rexp(200, rate=.1), ncol=20))
colnames(test) = paste("Room", 1:20, sep = "")
test$building = paste("Building", 1:10, sep = "")

#Get the sorting right
test$sort <- 1:10

#Melting data so we can plot it with GGplot
test.m <- melt(test,id.vars = c("building","sort"))

#Resetting factors
test.m$building <- factor(test.m$building, levels=(test.m$building)[order(test.m$sort)])

#Creating the plot itself
plot <- ggplot(test.m,aes(variable,building)) + geom_tile(aes(fill=value),color = "white") +
        #Creating legend
        guides(fill=guide_colorbar("Temperature [°C]")) +
        #Creating color range
        scale_fill_gradientn(colors=c("skyblue","yellow","tomato"),guide="colorbar") +
        #Rotating labels
        theme(axis.text.x = element_text(angle = 270, hjust = 0,vjust=-0.05))
plot
test <- matrix(rexp(200, rate=.1), ncol=20)
colnames(test) = paste("Room", 1:20, sep = "")
rownames(test) = paste("Building", 1:10, sep = "")

pheatmap(test, legend_breaks = c(10, 20, 30, 40, max(test)), 
main = "", legend_labels = c("10", "20", "30", "40", "title\n"),
legend = TRUE, cluster_rows = FALSE, cluster_cols = FALSE)