R 自定义相关图
嗨,我想自定义绘图,如下所示: -我希望在绘图中有一些直线,并希望将图例改为左侧,而不是右侧的普通图例。在变量旁边添加一些文本(分类)。我试过GGCORRPROOT,ggcorr,CORRPROOT,ggplot来制作这个,但仍然找不到解决方案。有人能帮忙吗?谢谢 样地如何制作R 自定义相关图,r,ggplot2,correlation,r-corrplot,R,Ggplot2,Correlation,R Corrplot,嗨,我想自定义绘图,如下所示: -我希望在绘图中有一些直线,并希望将图例改为左侧,而不是右侧的普通图例。在变量旁边添加一些文本(分类)。我试过GGCORRPROOT,ggcorr,CORRPROOT,ggplot来制作这个,但仍然找不到解决方案。有人能帮忙吗?谢谢 样地如何制作 ggcorr(data = NULL, cor_matrix = corr, nbreaks = 4, hjust = 1, size = 3, color = "grey60", layout.exp
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"))
我在《希望你能得到启发》上找到了一个示例代码:
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))
library(重塑2)
图书馆(GG2)
mydata我在《希望你能受到启发》上找到了一个示例代码:
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))
library(重塑2)
图书馆(GG2)
mydatamydatamydata谢谢,但我的问题是添加行、文本和更改图例。到目前为止,使用ggplot似乎比使用其他软件包更可行谢谢,但我的问题是添加行、文本和更改图例。到目前为止,与其他软件包相比,使用ggplot似乎更有可能做到这一点