自定义相关图

时间:2018-10-03 17:03:03

标签: r ggplot2 correlation r-corrplot

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

示例图-如何制作?

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"))

2 个答案:

答案 0 :(得分:1)

我在http://www.sthda.com/english/wiki/ggplot2-quick-correlation-matrix-heatmap-r-software-and-data-visualization上找到了示例代码,希望可以为您带来启发:

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()可以帮助您更改图例的位置和方向。

最后您得到了:

答案 1 :(得分:0)

mydata <- mtcars[,c(1,3,4,5,6,7)]
cormat <- round(cor(mydata),2)

cormat[lower.tri(cormat, diag = T)]<- 100
cormat <- melt(cormat, na.rm =F)
cormat[is.na(cormat)] <- 10
cormat[cormat$value != 100 ,] ->cormat
cormat$value[cormat$value == 10 ] <- NA

cormat$value[cormat$value >= 0.5 ] <- 1
cormat$value[cormat$value <= -0.5 ] <- -1
cormat$value[cormat$value > -0.5 & cormat$value < 0.5 ] <- 0

# Create a ggcorrx
dev.new(width=15, height=15)
gcorx <- ggplot(cormat, aes(Var2, Var1, fill = value, colour=""))+
geom_tile(color = "grey60")+
scale_fill_gradient2(breaks=c(-1,-0.5,0.5,1),low = "red", high = "green", mid = 
"yellow", midpoint = 0, limit = c(-1,1), space = "Lab", name="Not                      ??             OK", na.value="black") +
theme_minimal()+ # minimal theme
theme(axis.text.x = element_text(angle = 50, vjust = 1, 
                               size = 8, hjust = 1))+
theme(axis.text.y = element_text(vjust = 1, 
                               size = 8, hjust = 1))+
scale_y_discrete(position = "right")+
scale_x_discrete()+
coord_fixed()+
ggtitle("MT CARS")+
geom_segment(aes(x=1.5,xend=5.5,y=2.5,yend=2.5), color="black", size=2)+
geom_segment(aes(x=1.5,xend=1.5,y=0.5,yend=2.5), color="black", size=2)+
annotate("text", x=0.7, y=2.5, label= "Part 1", size = 3, color="black",angle = 50, 
fontface = "bold")+
annotate("text", x=2, y=4, label="Part 2", size = 3, color="black",angle = 50, 
fontface = "bold")


gcorx + 
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.major = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.ticks = element_blank(),
plot.title = element_text(color="black", size=12, face="bold", hjust = 0.5),
legend.justification = c(1, 0),
legend.position = c(0.3, 0.7),
legend.direction = "horizontal", 
legend.title = element_text(size=9, face= "italic"))+
guides(fill = guide_colorbar(barwidth = 8, barheight = 1,title.position = "top", 
title.hjust = 0.5))

Result 但是我仍然不知道将标签移动到对角线。有人吗?