该问题是我上一个问题here的继续。
我有一个带有可用数据集的热图。数据集粘贴在下面:
library(ggplot2)
library(colorspace)
library(directlabels)
smalltest <- structure(list(x = c(-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8,
-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, -8,
-7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, -8, -7,
-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, -8, -7, -6,
-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, -8, -7, -6, -5,
-4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8),
y = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5),
z = c(0.353727812855041, 0.512450876310741,
0.668920303216554, 0.770942367746301, 0.829915628317595, 0.873001682466956,
0.900219155289838, 0.918353789896507, 0.936488424503176, 0.954641684205298,
0.961439844045867, 0.972770110446816, 0.975042372092157, 0.981846740297877,
0.986385055223408, 0.986385055223408, 0.986385055223408, 0.33104865495769,
0.464820298870698, 0.62128351741136, 0.752801524774481, 0.804964208774903,
0.850322524569605, 0.879812259037828, 0.913821683336127, 0.934222371222986,
0.950109577644919, 0.959173790765678, 0.970504057166626, 0.975042372092157,
0.981846740297877, 0.986385055223408, 0.986385055223408, 0.986385055223408,
0.31064175870568, 0.428544821292209, 0.589558771488704, 0.725596468681902,
0.786835782533385, 0.838986049803505, 0.872995474101805, 0.897946893644497,
0.920613634811545, 0.943299001074047, 0.956907737485488, 0.970504057166626,
0.970504057166626, 0.981840531932726, 0.986385055223408, 0.986385055223408,
0.986385055223408, 0.29023486245367, 0.419493024901753, 0.569145666871543,
0.702929727514853, 0.775480682671832, 0.827655783402557, 0.866197314261236,
0.891148733803927, 0.916075319886014, 0.931943901212494, 0.952369422559957,
0.970504057166626, 0.970504057166626, 0.981840531932726, 0.981840531932726,
0.986385055223408, 0.986385055223408, 0.272100227847001, 0.396807658639251,
0.557778150279687, 0.691580836018451, 0.766410261185922, 0.807248887150547,
0.857126892775325, 0.888876472158586, 0.911543213325635, 0.929684056297455,
0.941020531063555, 0.959155165670224, 0.968231795521285, 0.977302217007196,
0.981840531932726, 0.984112793578067, 0.984112793578067, 0.265302068006432,
0.396789033543797, 0.557784358644838, 0.680244361252351, 0.761871946260391,
0.800444518944826, 0.841264519813997, 0.882078312318017, 0.909277160045445,
0.931950109577645, 0.941014322698404, 0.954623059109845, 0.961421218950414,
0.972763902081665, 0.977302217007196, 0.984112793578067, 0.984112793578067)),
row.names = c(NA, -102L), class = c("tbl_df", "tbl", "data.frame"))
我可以根据上述数据集生成热图和轮廓线。
ggplot(smalltest, aes(x = x, y = y)) +
geom_tile(aes(fill = z)) +
scale_fill_continuous_divergingx(palette = 'RdBu', rev = FALSE, mid = 0.9, l3 = 0, p3 = 0.95, p4 = 0.85) +
scale_x_continuous(expand = c(0, 0), breaks = -8:8) +
scale_y_continuous(expand = c(0, 0), breaks = 0:5) +
geom_contour(aes(z = z), breaks = c(0.8, 0.9, 0.95), color = 'black', size = 1) +
geom_dl(aes(label = c(rep(NA, 99), 'Low', 'Middle', 'High')), method = 'last.points')
我有以下问题:
geom_dl()
仅查看/使用这些标签的轮廓线的末端或侧面?我最初添加rep(NA,99)
是因为出现错误,指出label
的长度必须等于1或数据集的长度(此处为102)。但是,我怎样才能仅通过自定义字符串向量呢? 谢谢!
由reprex package(v0.3.0)于2019-11-07创建