ggplot r中热图标签中的上标 [英] Superscripts in heat plot labels in ggplot r

查看:178
本文介绍了ggplot r中热图标签中的上标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好

我正在用ggplot绘制特定表型之间相关性的热图.我想用关联的R ^ 2标记每个图块.
我有一个相关矩阵max_all,它看起来像这样:

    phenolist2 pheno1 pheno2 pheno3 pheno4   pheno5
max.pheno1     pheno1    0.05475998        0.05055959   0.05056578  0.10330301 0.05026997
max.pheno2 pheno2    0.15743312        0.05036100   0.05151750  0.04880302 0.31008809
max.pheno3      pheno3    0.05458550        0.07672537   0.04043422  0.16845294 0.14268895
max.pheno4       pheno4    0.05484327        0.04391523   0.05151107  0.09521869 0.19776296
max.pheno5           pheno5    0.08658449        0.05183693   0.16292683  0.22369817 0.53630569

否则,我的代码如下:

    tmp_Rsq <- melt(max_all)

tmp_Rsq <- ddply(tmp_Rsq, .(variable), transform, rescale=rescale(value))

labels_Rsq <- expression(paste(R^2, " = ", format(tmp_Rsq$value, digits=2), sep=""))

ggplot(tmp, aes(variable, phenolist2)) + 
  geom_tile(aes(fill =-log10(value)), colour = "white") +
  geom_text(aes(label=as.character(labels_Rsq), parse = TRUE, size=4)) +
  scale_fill_gradientn(colours = myPalette(101), name="-log10(P)", limits=c(0 , 3.5)) +
  theme(axis.title.x = element_blank(), axis.title.y=element_blank(),
        plot.title=element_text(size=20))+
  theme(axis.text = element_text(colour="black", face="bold"))

我的问题是我无法写出表达式,所以2是R的上标. 我意识到此网站上有很多类似问题的问题,例如 ggplot2 two-带有表达式的行标签将paste()和expression( )功能添加回归线方程式和图上的R2 ,但我无法获得这些答案中建议的解决方案以适用于我的情况(可能是因为我一直在尝试使用标签向量).

非常感谢您的帮助.

解决方案

解析必须在aes之外,并且标签必须是字符向量.

labels_Rsq <- paste0("R^2 ==", format(tmp_Rsq$value, digits=2))

> head(labels_Rsq)
[1] "R^2 ==0.055" "R^2 ==0.157" "R^2 ==0.055" "R^2 ==0.055" "R^2 ==0.087" "R^2 ==0.051"

ggplot(tmp_Rsq, aes(variable, phenolist2)) + 
  geom_tile(aes(fill =-log10(value)), colour = "white") + 
  geom_text(aes(label=as.character(labels_Rsq)), parse = TRUE, size=4) +
 # scale_fill_gradientn(colours = myPalette(101), name="-log10(P)", limits=c(0 , 3.5)) +
  theme(axis.title.x = element_blank(), axis.title.y=element_blank(), 
        plot.title=element_text(size=20))+
  theme(axis.text = element_text(colour="black", face="bold"))

Good morning,

I am making a heat map in ggplot of correlations between specific phenotypes. I would like to label each tile with the R^2 for the association.
I have a correlation matrix, max_all, which looks like this:

    phenolist2 pheno1 pheno2 pheno3 pheno4   pheno5
max.pheno1     pheno1    0.05475998        0.05055959   0.05056578  0.10330301 0.05026997
max.pheno2 pheno2    0.15743312        0.05036100   0.05151750  0.04880302 0.31008809
max.pheno3      pheno3    0.05458550        0.07672537   0.04043422  0.16845294 0.14268895
max.pheno4       pheno4    0.05484327        0.04391523   0.05151107  0.09521869 0.19776296
max.pheno5           pheno5    0.08658449        0.05183693   0.16292683  0.22369817 0.53630569

Otherwise, my code is as follows:

    tmp_Rsq <- melt(max_all)

tmp_Rsq <- ddply(tmp_Rsq, .(variable), transform, rescale=rescale(value))

labels_Rsq <- expression(paste(R^2, " = ", format(tmp_Rsq$value, digits=2), sep=""))

ggplot(tmp, aes(variable, phenolist2)) + 
  geom_tile(aes(fill =-log10(value)), colour = "white") +
  geom_text(aes(label=as.character(labels_Rsq), parse = TRUE, size=4)) +
  scale_fill_gradientn(colours = myPalette(101), name="-log10(P)", limits=c(0 , 3.5)) +
  theme(axis.title.x = element_blank(), axis.title.y=element_blank(),
        plot.title=element_text(size=20))+
  theme(axis.text = element_text(colour="black", face="bold"))

My problem is that I can not get the expression to write out so that 2 is a superscript of R. I realize there are a number of questions on this website addressing similar issues, for example ggplot2 two-line label with expression, Combining paste() and expression() functions in plot labels and Adding Regression Line Equation and R2 on graph but I have been unable to get the solutions suggested in these answers to apply to my case (likely because I have been trying to use a vector of labels).

Thanks a lot for your help.

解决方案

Parse needs to be outside the aes, and the labels need to be a character vector.

labels_Rsq <- paste0("R^2 ==", format(tmp_Rsq$value, digits=2))

> head(labels_Rsq)
[1] "R^2 ==0.055" "R^2 ==0.157" "R^2 ==0.055" "R^2 ==0.055" "R^2 ==0.087" "R^2 ==0.051"

ggplot(tmp_Rsq, aes(variable, phenolist2)) + 
  geom_tile(aes(fill =-log10(value)), colour = "white") + 
  geom_text(aes(label=as.character(labels_Rsq)), parse = TRUE, size=4) +
 # scale_fill_gradientn(colours = myPalette(101), name="-log10(P)", limits=c(0 , 3.5)) +
  theme(axis.title.x = element_blank(), axis.title.y=element_blank(), 
        plot.title=element_text(size=20))+
  theme(axis.text = element_text(colour="black", face="bold"))

这篇关于ggplot r中热图标签中的上标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆