自定义相关图 [英] Customize correlation plot r

查看:117
本文介绍了自定义相关图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自定义情节,如下所示: -我想在图内有一条直线,并想将图例更改为左侧的内容,而不是右侧的普通图例.还要在变量旁边添加一些文本(分类).我已经尝试过ggcorrplot,ggcorr,corrplot,ggplot来做到这一点,但仍然找不到解决方案.有人可以帮忙吗?谢谢.

Hi I want to customize the plot to something like this: -I want to have some straight line inside the plot and want to change the legend to something in the left side rather than the normal legend in right side. Also add some texts beside the variables (categorize). I have tried ggcorrplot, ggcorr, corrplot, ggplot to make this, but still can't find the solution. Anyone can help? Thanks.

示例图-如何制作?

ggcorr(data = NULL, cor_matrix = corr, nbreaks = 4, hjust = 1, size = 3, 
       color = "grey60", layout.exp = 1, legend.size = 8, name= "R", palette = "RdYlGn") + 
  labs(title = "Corr") + 
  theme(plot.title = element_text(size = 13)) + 
  theme(plot.title = element_text(size = 14, color="grey40"))

推荐答案

我在

I found an example code on http://www.sthda.com/english/wiki/ggplot2-quick-correlation-matrix-heatmap-r-software-and-data-visualization hope you can be inspired:

library(reshape2)
library(ggplot2)
mydata <- mtcars[,c(1,3,4,5,6,7)]
cormat <- round(cor(mydata),2)  # got correlation matrix

# Get lower triangle of the correlation matrix, we use upper for what you wanted
get_lower_tri<-function(cormat){
  cormat[upper.tri(cormat)] <- NA
  return(cormat)
}
# Get upper triangle of the correlation matrix
get_upper_tri <- function(cormat){
  cormat[lower.tri(cormat)]<- NA
  return(cormat)
}

upper_tri <- get_upper_tri(cormat)
melted_cormat <- melt(upper_tri, na.rm = TRUE)
ggheatmap <- ggplot(data = melted_cormat, aes(Var2, Var1, fill = value))+
  geom_tile(color = "white")+
  scale_fill_gradient2(low = "blue", high = "red", mid = "white", 
                       midpoint = 0, limit = c(-1,1), space = "Lab", 
                       name="Pearson\nCorrelation") +
  theme_minimal()+ 
  theme(axis.text.x = element_text(angle = 45, vjust = 1, 
                                   size = 12, hjust = 1))+
  coord_fixed()

ggheatmap + 
  theme(legend.justification = c(1, 0),
        legend.position = c(0.3, 0.5))+
  guides(fill = guide_colorbar(title.position = "top", title.hjust = 0.5))

themes()可以帮助您更改图例的位置和方向.

themes()can help you to change the position and direction of legend.

最后,您得到了:

这篇关于自定义相关图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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