热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在热图(ggplot2)中,Faceting把值标签弄乱。-Facetingmessesupvaluelabelsinheatmap(ggplot2)

Immakingaheatmapusingggplot2.Iwanttoputthevaluesthemselvesinsidetheheatmapboxes,bu

I'm making a heatmap using ggplot2. I want to put the values themselves inside the heatmap boxes, but I'm finding that while that works okay when not faceting, as soon as I facet, the numbers go to all the wrong boxes - see attached pictures. Any tips on how to make the numbers stay in the right boxes when faceting?

我正在用ggplot2做一个热图。我想把这些值放到加热地图盒里,但是我发现当我不面朝时,当我看到这些数字的时候,这些数字就会出现在所有错误的盒子里——见附件图片。关于如何让数字保持在正确的盒子里的任何提示?

ALso, some other minor questions: how to change the tile sizes when and when not faceting; changing the color of the value text in the boxes (<- tried the obvious solutions but don't seem to work...)

另外,还有一些次要问题:如何在不使用面板的情况下改变尺寸大小;改变框中值文本的颜色(<-尝试了明显的解决方案,但似乎没有效果…)

Thanks everyone!

谢谢大家!

Code (Can see where the facet is commented out):

代码(可以看到facet被注释掉的地方):

fpdata<-read.csv("fp.csv",header=T)
fpdata$Dimension=factor(fpdata$Dimension,levels(fpdata$Dimension)[c(4,1,3,2,5)])
fpdata$Trait=factor(fpdata$Trait,levels(fpdata$Trait)[c(22,15,16,3,4,9,21,20,10,14,6,2,1,
                                                        13,8,11,12,17,24,25,18,5,23,19,7)])
matrix <- melt(fpdata)
matrix
matrix$value[is.na(matrix$value)] <- 2
matrix


p3 <- ggplot(data = matrix, aes(variable, Trait, fill = value))+
  geom_tile(color = "white")+
  scale_fill_gradient2(low = "blue", high = "firebrick", mid = "deepskyblue", 
                         midpoint = 0.6, limit = c(0.6,1), space = "Lab", 
                         name="Factor Loadings")+labs(x = "Reduction #")+
    geom_text(aes(label=ifelse(matrix$value<2,matrix$value," ")))
  ##+facet_grid(~Dimension, scale="free") 

p3 <- p3 +  
  theme (axis.title.x = element_text(size=20),
         axis.title.y = element_text(size=20),
         axis.text.x = element_text(angle = 0, vjust = 1, size = 14, hjust = 0.5),
         axis.text.y = element_text(size=16),
         panel.grid.major = element_blank(),
         panel.grid.minor = element_blank(),
         legend.title = element_text(size=16),
         legend.text = element_text(size=14),
         strip.text.x = element_text(size=13, face="bold"))+coord_fixed()
p3

Heat Map without Facet

热图不方面

enter image description here

Heat Map with Facet

热量地图方面

enter image description here

1 个解决方案

#1


0  

df <- data.frame(trait = sample(letters, 15),
                 dimension = rep(LETTERS[1:5], 3), 
                 variable = paste0('R', 1:3), 
                 value = runif(15))

ggplot(df, aes(variable, trait, fill = value)) +
  geom_tile(aes(fill = ifelse(value > 0.25, value, NA))) +
  geom_text(aes(label = ifelse(value > 0.25, round(value, 2), NA))) +
  facet_grid(~dimension, scales = 'free')

Reproducible example I hope mirrors your data somewhat. As suggested above, value is already defined in your input (df$value) in this case. So you can refer to it directly as a bare (unquoted) symbol.

可重复的例子,我希望能在一定程度上反映你的数据。如上所述,在本例中,值已经在您的输入(df$value)中定义了。因此,您可以直接引用它作为一个空的(未引用的)符号。


推荐阅读
author-avatar
卓慧大美女_502
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有