您好,这实际上是我之前在这里提出的问题的跟进问题: Customize correlation plot r
所以我仍然不能将标签移到对角线一侧,而是将颜色比例更改为离散比例,如下所示(示例):
这是到目前为止的代码:
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))
答案 0 :(得分:1)
我可以帮忙色阶...
cols <- c("-1" = "red", "0" = "yellow", "1" = "green")
ggplot(cormat, aes(Var2, Var1, fill = value, colour=""))+
geom_tile(aes(fill = factor(value)))+
scale_fill_manual(values = cols)
您可以使用函数cut
来创建一些新变量,然后根据cut的值将cols
更改为4而不是3
https://ggplot2.tidyverse.org/reference/scale_manual.html
对于轴文本,您可以尝试关闭y轴,然后在绘图中添加注释。 https://ggplot2.tidyverse.org/reference/annotate.html
http://www.sthda.com/english/wiki/ggplot2-axis-ticks-a-guide-to-customize-tick-marks-and-labels