R 绘制相关图的下三角 (ggplot2)

标签 r ggplot2 reshape

我有以下数据框(附后)。我想绘制方法之间的相关性。我希望删除上面的三角形,因为它反射(reflect)了下面的三角形。我还想忽略相同方法之间的相关性。在这里

c = cor(df[sapply(df,is.numeric)])
p <- ggplot(subset(melt(cor(df[sapply(df,is.numeric)]))[lower.tri(c),],Var1 != Var2),
+        aes(x=Var1,y=Var2,fill=value)) + geom_tile(aes(fill = value),colour = "white") +
+     geom_text(aes(label = sprintf("%1.2f",value)), vjust = 1) + 
+     theme_bw() +
+     scale_fill_gradient2(name="R^2",midpoint=0.7,low = "white", high = "red") + xlab(NULL)+ylab(NULL) + theme(axis.text.x=element_blank(),axis.text.y=element_blank(), axis.ticks=element_blank(),panel.border=element_blank())

这给出了下面的图,这几乎就是我想要的 enter image description here

但是,我想添加一些文字以使情节更容易理解。即我希望读者查看 X 和 Y 变量并认识到左下方 block 是 Link 和 Odyssey 之间的相关性

p + geom_text(data = subset(melt(cor(df[sapply(df,is.numeric)])),Var1==Var2),aes(label=Var1),vjust=3 )

这会打乱图像中图 block 的顺序,并使情节变得一团糟。不知何故,索引很困惑。我正用头撞墙寻找 bug 。我确信这是显而易见的 enter image description here

structure(list(Study = structure(c(1L, 2L, 4L, 6L, 7L, 8L, 9L, 
10L, 11L, 12L, 14L, 15L, 16L, 17L, 19L, 22L, 23L, 24L, 27L, 29L, 
30L, 32L, 34L, 35L, 36L, 46L, 47L, 49L, 51L, 52L, 53L, 54L, 55L, 
56L, 57L, 58L, 59L, 60L, 63L, 66L, 68L, 70L, 72L, 73L, 74L, 75L, 
77L, 78L, 80L, 81L, 83L, 85L, 86L, 87L, 88L, 89L, 90L, 92L, 95L, 
97L, 101L, 103L, 107L, 109L, 112L, 113L, 114L, 115L, 117L, 118L
), .Label = c("WCBP00004", "WCBP00006", "WCBP00013", "WCBP00014", 
"WCBP02014", "WCBP02032", "WCBP02034", "WCBP02038", "WCBP02039", 
"WCBP02043", "WCBP03002", "WCBP03003", "WCBP03005", "WCBP03006", 
"WCBP03013", "WCBP03014", "WCBP03019", "WCBP03020", "WCBP03021", 
"WCBP03101", "WCBP03105", "WCBP03169", "WCBP03174", "WCBP03175", 
"WCBP03177", "WCBP03178", "WCBP03183", "WCBP03186", "WCBP03190", 
"WCBP03196", "WCBP03204", "WCBP03205", "WCBP03206", "WCBP04036", 
"WCBP04037", "WCBP04038", "WCBP04048", "WCBP04057", "WCBP04060", 
"WCBP04061", "WCBP04066", "WCBP04067", "WCBP04070", "WCBP04072", 
"WCBP04074", "WCBP04075", "WCBP05254", "WCBP05258", "WCBP05269", 
"WCBP05278", "WCBP06090", "WCBP06093", "WCBP06094", "WCBP06096", 
"WCBP06099", "WCBP06118", "WCBP06119", "WCBP06128", "WCBP06131", 
"WCBP06133", "WCBP06134", "WCBP06141", "WCBP06142", "WCBP06210", 
"WCBP06212", "WCBP06215", "WCBP06217", "WCBP06218", "WCBP06225", 
"WCBP07411", "WCBP07422", "WCBP07428", "WCBP07436", "WCBP07438", 
"WCBP07439", "WCBP13090", "WCBP13091", "WCBP13092", "WCBP13093", 
"WCBP13094", "WCBP13096", "WCBP13097", "WCBP13098", "WCBP13102", 
"WCBP13103", "WCBP13104", "WCBP13106", "WCBP13110", "WCBP13114", 
"WCBP13116", "WCBP13121", "WCBP13125", "WCBP97048", "WCBP97049", 
"WCBP97050", "WCBP97051", "WCBP97052", "WCBP98008", "WCBP98021", 
"WCBP98038", "WCBP98041", "WCBP98043", "WCBP98077", "WCBP98078", 
"WCBP99001", "WCBP99007", "WCBP99012", "WCBP99014", "WCBP99018", 
"WCBP99019", "WCBP99034", "WCBP99037", "WCBP99038", "WCBP99045", 
"WCBP99048", "WCBP99049", "WCBP99051", "WCBP99052", "WCBP99054", 
"WCBP99057", "WCBP99058", "WCBP99060", "WCBP99062", "WCBP99063", 
"WCBP99064"), class = "factor"), Odyssey = c(70.7, 23.7, 63.3, 
62.4, 78.3, 64.2, 51.8, 78.8, 45.9, 68.5, 65, 58.1, 56.9, 60.5, 
71.9, 72.8, 62.7, 56.1, 71.3, 75.8, 74, 45.5, 62.5, 46.2, 62.6, 
46.1, 80.9, 68.6, 63, 41, 36.8, 63, 47, 56.5, 59.5, 62.2, 48.2, 
65, 61.4, 64.7, 67.7, 76.8, 66.8, 50.8, 52.1, 61.3, 60.2, 71.2, 
58.4, 48.4, 59.3, 68.8, 62.8, 63.6, 66.2, 71.9, 77.1, 65.1, 54.8, 
54.3, 41.9, 32.8, 53.6, 68.8, 60.6, 60.7, 87.2, 47.4, 43.6, 58.5
), Link = c(70L, 46L, 62L, 60L, 77L, 64L, 52L, 77L, 44L, 68L, 
65L, 57L, 56L, 59L, 74L, 73L, 61L, 56L, 69L, 74L, 74L, 45L, 63L, 
46L, 63L, 45L, 80L, 68L, 62L, 28L, 34L, 62L, 46L, 57L, 60L, 61L, 
48L, 65L, 61L, 64L, 66L, 75L, 68L, 52L, 52L, 62L, 60L, 73L, 58L, 
69L, 59L, 69L, 61L, 61L, 67L, 72L, 75L, 65L, 59L, 27L, 39L, 45L, 
53L, 68L, 63L, 60L, 86L, 47L, 45L, 52L), Xeleris = c(70L, 23L, 
69L, 63L, 76L, 58L, 53L, 66L, 38L, 68L, 53L, 57L, 61L, 63L, 66L, 
66L, 61L, 53L, 70L, 75L, 76L, 50L, 60L, 53L, 49L, 42L, 73L, 73L, 
63L, 33L, 36L, 65L, 51L, 62L, 71L, 63L, 46L, 61L, 66L, 64L, 62L, 
55L, 67L, 52L, 57L, 63L, 60L, 72L, 63L, 70L, 71L, 58L, 61L, 63L, 
63L, 60L, 66L, 58L, 44L, 29L, 36L, 26L, 58L, 63L, 63L, 62L, 68L, 
47L, 49L, 36L), Hermes = c(68L, 25L, 66L, 58L, 77L, 61L, 54L, 
70L, 31L, 63L, 54L, 59L, 53L, 55L, 75L, 69L, 62L, 49L, 68L, 76L, 
73L, 49L, 62L, 51L, 56L, 39L, 75L, 68L, 57L, 29L, 25L, 59L, 47L, 
57L, 65L, 59L, 41L, 66L, 67L, 58L, 62L, 76L, 73L, 46L, 55L, 59L, 
63L, 79L, 62L, 68L, 70L, 68L, 67L, 71L, 68L, 60L, 72L, 57L, 44L, 
17L, 24L, 26L, 45L, 52L, 64L, 57L, 75L, 44L, 47L, 31L), Syngo = c(78L, 
34L, 79L, 70L, 77L, 71L, 63L, 72L, 34L, 66L, 60L, 63L, 66L, 66L, 
71L, 77L, 71L, 55L, 80L, 87L, 83L, 55L, 64L, 60L, 54L, 41L, 86L, 
79L, 66L, 28L, 24L, 67L, 54L, 63L, 71L, 70L, 51L, 70L, 73L, 70L, 
65L, 54L, 76L, 47L, 66L, 68L, 72L, 83L, 67L, 74L, 72L, 62L, 76L, 
79L, 75L, 69L, 91L, 65L, 50L, 29L, 26L, 26L, 51L, 59L, 69L, 65L, 
88L, 47L, 49L, 35L)), .Names = c("Study", "Odyssey", "Link", 
"Xeleris", "Hermes", "Syngo"), row.names = c(1L, 2L, 4L, 6L, 
7L, 8L, 9L, 10L, 11L, 12L, 14L, 15L, 16L, 17L, 19L, 22L, 23L, 
24L, 27L, 29L, 30L, 32L, 34L, 35L, 36L, 46L, 47L, 49L, 51L, 52L, 
53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 63L, 66L, 68L, 70L, 72L, 
73L, 74L, 75L, 77L, 78L, 80L, 81L, 83L, 85L, 86L, 87L, 88L, 89L, 
90L, 92L, 95L, 97L, 101L, 103L, 107L, 109L, 112L, 113L, 114L, 
115L, 117L, 118L), class = "data.frame", na.action = structure(c(3L, 
5L, 13L, 18L, 20L, 21L, 25L, 26L, 28L, 31L, 33L, 37L, 38L, 39L, 
40L, 41L, 42L, 43L, 44L, 45L, 48L, 50L, 61L, 62L, 64L, 65L, 67L, 
69L, 71L, 76L, 79L, 82L, 84L, 91L, 93L, 94L, 96L, 98L, 99L, 100L, 
102L, 104L, 105L, 106L, 108L, 110L, 111L, 116L, 119L, 120L, 121L, 
122L, 123L, 124L, 125L), .Names = c("3", "5", "13", "18", "20", 
"21", "25", "26", "28", "31", "33", "37", "38", "39", "40", "41", 
"42", "43", "44", "45", "48", "50", "61", "62", "64", "65", "67", 
"69", "71", "76", "79", "82", "84", "91", "93", "94", "96", "98", 
"99", "100", "102", "104", "105", "106", "108", "110", "111", 
"116", "119", "120", "121", "122", "123", "124", "125"), class = "omit"))

最佳答案

您可以使用注释。将以下代码添加到上面的代码中(请注意,我使用 nrow(c) 来使注释的位置更加灵活,但是,您也可以使用例如 x=0:4):

p + 
  scale_x_discrete(expand = c(0.2, 0.2)) +
  scale_y_discrete(expand = c(0.2, 0.2)) +
  annotate("text", x = (1:nrow(c))-1, y = 1:nrow(c), label = c("Odyssey", "Link", "Xeleris", "Hermes", "Syngo"))

结果如下:

enter image description here

我使用expand来更改轴范围,因此注释可见。您或许还可以尝试更改规模限制。

PS:我不建议使用 c 作为变量名,因为它是为基本函数保留的(同样适用于 df,我最近才认识到它)。

关于R 绘制相关图的下三角 (ggplot2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26713183/

相关文章:

r - 通过apply函数生成多个ggplot箱线图

r - 统计文档之间的属性 R

r - sf + ggplot 尝试绘制斐济群岛 map

R如何不将数字显示在输出中行数的括号中

r - ggplot : How to create different x-axis titles with facet_grid

R:我如何为每个级别(用于 reshape )创建一个带有订单数(按日期)的新变量。)

matrix - 如何在 TensorFlow 中更改变量的形状?

r - 使用列表列规范化数据框

r - 按因子级别对 R 数据帧进行子集,在第一次超过阈值后删除所有行

r - 在 Shiny 的应用程序中包含自定义 css