我有以下数据框(附件)。我想绘制方法之间的相关性。我希望删除上三角形,因为这反映了下三角形。我也想省略相同方法之间的相关性。在这里
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())
这给出了以下情节,这几乎是我想要的
但是,我想添加一些文字以使情节更具消化性。即我希望读者查看X和Y变量并实现左下方是Link和Odyssey之间的相关性
p + geom_text(data = subset(melt(cor(df[sapply(df,is.numeric)])),Var1==Var2),aes(label=Var1),vjust=3 )
这会弄乱图像中瓷砖的顺序,并使图形混乱。不知何故索引很混乱。我正在从墙上敲头寻找虫子。我很确定这很明显
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"))
答案 0 :(得分:2)
您可以使用annotate
。在上面的代码中添加以下代码(请注意,我使用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"))
结果如下:
我使用expand
来更改轴范围,因此注释是可见的。您也可以尝试更改比例限制。
PS:我不建议使用c
作为变量名,因为它是为基本函数保留的(同样适用于df
,我最近才认识到这一点。)
答案 1 :(得分:1)
我没有足够的声誉来评论,所以我会从这里开始。
您是否已发布完整代码&amp;数据集?
根据我的结论,你发布的内容很多。
与此同时,我将建议以下内容。我不是使用你从数据集中调用的geom_text,而是创建一个带有文本坐标的独立数据框。这样你就可以在输出的顶部绘制。
你可以创建类似的东西:
CoOr<- data.frame(
textposx = 1.8,
textposy = 84)
从那里你可以做到以下几点:
p + geom_text(data=CoOr,aes(textposx, textposy, label="Whatever you like"))
它可能有点儿麻烦,所以您可能只想将geom_path添加到原始图形中而不是添加到P但是,看看它是怎么回事。
每个标签都可以重复此操作。它有点小,但希望能有效。