【问题标题】:In ggplot2 correlogram how to make diagonal label and customize the legend在ggplot2相关图中如何制作对角线标签并自定义图例
【发布时间】:2018-10-18 14:53:28
【问题描述】:

您好,这实际上是我之前的问题的后续问题: 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))

【问题讨论】:

    标签: r ggplot2 correlation r-corrplot


    【解决方案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

    【讨论】:

    • 昨天才发现无法解色标。所以我放了色标的图片,并放了标注标尺的注释。对于对角线,它现在可以通过添加 geom_text 来工作,使用来自熔化 cormat 的数据 where Var1 = Var2 。
    猜你喜欢
    • 1970-01-01
    • 2016-06-12
    • 2021-08-27
    • 2018-03-21
    • 2020-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-05
    相关资源
    最近更新 更多