防止行尾的标签重叠是这个数据的一个特殊挑战,因为我希望相同的代码能够处理不同的数据。在下面的示例中,一些标签需要远离它们标记的点以腾出空间。我很好,我只是想知道是否有办法以编程方式进行,因为我无法自定义我需要应用它的每个数据集的代码。谢谢!
我也在努力格式化标签:我将它们四舍五入到最接近的百分比但是尽管玩sprintf
我无法弄清楚如何摆脱小数点和尾随零。
我的数据可以用以下方式重新创建:
mydf <-
structure(list(date = structure(c(15573, 15574, 15575, 15576,
15577, 15578, 15579, 15580, 15581, 15582, 15583, 15584, 15585,
15586, 15587, 15588, 15589, 15590, 15591, 15592, 15593, 15594,
15595, 15596, 15597, 15573, 15574, 15575, 15576, 15577, 15578,
15579, 15580, 15581, 15582, 15583, 15584, 15585, 15586, 15587,
15588, 15589, 15590, 15591, 15592, 15593, 15594, 15595, 15596,
15597, 15573, 15574, 15575, 15576, 15577, 15578, 15579, 15580,
15581, 15582, 15583, 15584, 15585, 15586, 15587, 15588, 15589,
15590, 15591, 15592, 15593, 15594, 15595, 15596, 15597, 15573,
15574, 15575, 15576, 15577, 15578, 15579, 15580, 15581, 15582,
15583, 15584, 15585, 15586, 15587, 15588, 15589, 15590, 15591,
15592, 15593, 15594, 15595, 15596, 15597, 15573, 15574, 15575,
15576, 15577, 15578, 15579, 15580, 15581, 15582, 15583, 15584,
15585, 15586, 15587, 15588, 15589, 15590, 15591, 15592, 15593,
15594, 15595, 15596, 15597, 15573, 15574, 15575, 15576, 15577,
15578, 15579, 15580, 15581, 15582, 15583, 15584, 15585, 15586,
15587, 15588, 15589, 15590, 15591, 15592, 15593, 15594, 15595,
15596, 15597, 15573, 15574, 15575, 15576, 15577, 15578, 15579,
15580, 15581, 15582, 15583, 15584, 15585, 15586, 15587, 15588,
15589, 15590, 15591, 15592, 15593, 15594, 15595, 15596, 15597,
15573, 15574, 15575, 15576, 15577, 15578, 15579, 15580, 15581,
15582, 15583, 15584, 15585, 15586, 15587, 15588, 15589, 15590,
15591, 15592, 15593, 15594, 15595, 15596, 15597, 15573, 15574,
15575, 15576, 15577, 15578, 15579, 15580, 15581, 15582, 15583,
15584, 15585, 15586, 15587, 15588, 15589, 15590, 15591, 15592,
15593, 15594, 15595, 15596, 15597), class = "Date"), outcome = c(0.0514,
0.0555, 0.0611, 0.0646, 0.0565, 0.055, 0.0588, 0.0504, 0.0585,
0.0576, 0.0546, 0.0553, 0.0123, 0.0155, 0.0066, 0.0105, 0.0179,
0.0052, 0.0057, 0.007, 0.0094, 0.0127, 0.0122, 0.0089, 0.0097,
0.0305, 0.0248, 0.033, 0.037, 0.0305, 0.0334, 0.0346, 0.0356,
0.034, 0.0317, 0.0351, 0.034, 0.0091, 0.0026, 0.0151, 0.0152,
0.0145, 0.006, 0.0052, 0.0082, 0.0122, 0.017, 0.008, 0.0136,
0.0114, 0.0068, 0.0042, 0.0085, 0.0134, 0.0091, 0.0127, 0.0082,
0.0154, 0.0088, 0.0102, 0.0127, 0.0168, 0.0057, 0.0106, 0.0107,
0.0076, 0.006, 0.0116, 0.0116, 0.0107, 0.0087, 0.0046, 0.0126,
0.005, 0.0074, 0.0259, 0.0174, 0.0172, 0.0182, 0.028, 0.0238,
0.0269, 0.0312, 0.0213, 0.0334, 0.0247, 0.0232, 0.0261, 0.0229,
0.0183, 0.0238, 0.0221, 0.016, 0.0232, 0.0177, 0.0251, 0.0202,
0.0232, 0.0153, 0.0237, 0.0511, 0.0578, 0.0547, 0.0536, 0.0202,
0.014, 0.007, 0.0126, 0.0145, 0.015, 0.0117, 0.0169, 0.0136,
0.0128, 0.0127, 0.0138, 0.0092, 0.0094, 0.0106, 0.0098, 0.0093,
0.0141, 0.0063, 0.0104, 0.0183, 0.0259, 0.0195, 0.0289, 0.0247,
0.027, 0.0264, 0.0281, 0.023, 0.0175, 0.0247, 0.0166, 0.0224,
0.0196, 0.0177, 0.0248, 0.0236, 0.0205, 0.0245, 0.0225, 0.019,
0.0167, 0.0154, 0.0204, 0.0244, 0.0235, 0.0114, 0.0108, 0.0136,
0.0064, 0.0088, 0.0161, 0.0143, 0.009, 0.0107, 0.0031, 0.0012,
0.0108, 0.0146, 0.0044, 0.0058, 0.0129, 0.006, 0.0157, 0.0107,
0.0105, 0.0093, 0.0077, 0.0078, 0.0105, 0.0067, 0.0055, 0.0137,
0.0108, 0.021, 0.0081, 0.0149, 0.0071, 0.0106, 0.0048, 0.0073,
0.0122, 0.0059, 0.0115, 0.0103, 0.0093, 0.0037, 0.0065, 0.0038,
0.0129, 0.008, 0.0045, 0.0098, 0.0047, 0.0123, 0.0088, 0.1243,
0.1209, 0.1196, 0.1311, 0.1193, 0.1269, 0.1313, 0.1332, 0.1244,
0.1308, 0.1229, 0.1273, 0.1276, 0.115, 0.1075, 0.1119, 0.1163,
0.1172, 0.112, 0.1125, 0.1184, 0.1071, 0.107, 0.1081, 0.1126),
trial = c("C", "C", "C", "C", "C", "C", "C", "C", "C", "C",
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C",
"C", "C", "C", "B", "B", "B", "B", "B", "B", "B", "B", "B",
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B",
"B", "B", "B", "B", "F", "F", "F", "F", "F", "F", "F", "F",
"F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F",
"F", "F", "F", "F", "F", "E", "E", "E", "E", "E", "E", "E",
"E", "E", "E", "E", "E", "E", "E", "E", "E", "E", "E", "E",
"E", "E", "E", "E", "E", "E", "H", "H", "H", "H", "H", "H",
"H", "H", "H", "H", "H", "H", "H", "H", "H", "H", "H", "H",
"H", "H", "H", "H", "H", "H", "H", "I", "I", "I", "I", "I",
"I", "I", "I", "I", "I", "I", "I", "I", "I", "I", "I", "I",
"I", "I", "I", "I", "I", "I", "I", "I", "G", "G", "G", "G",
"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G",
"G", "G", "G", "G", "G", "G", "G", "G", "G", "D", "D", "D",
"D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D",
"D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A")), .Names = c("date",
"outcome", "trial"), class = "data.frame", row.names = c(94L,
95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 103L, 104L, 105L,
106L, 107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L, 115L, 116L,
117L, 118L, 356L, 357L, 358L, 359L, 360L, 361L, 362L, 363L, 364L,
365L, 366L, 367L, 368L, 369L, 370L, 371L, 372L, 373L, 374L, 375L,
376L, 377L, 378L, 379L, 380L, 618L, 619L, 620L, 621L, 622L, 623L,
624L, 625L, 626L, 627L, 628L, 629L, 630L, 631L, 632L, 633L, 634L,
635L, 636L, 637L, 638L, 639L, 640L, 641L, 642L, 880L, 881L, 882L,
883L, 884L, 885L, 886L, 887L, 888L, 889L, 890L, 891L, 892L, 893L,
894L, 895L, 896L, 897L, 898L, 899L, 900L, 901L, 902L, 903L, 904L,
1142L, 1143L, 1144L, 1145L, 1146L, 1147L, 1148L, 1149L, 1150L,
1151L, 1152L, 1153L, 1154L, 1155L, 1156L, 1157L, 1158L, 1159L,
1160L, 1161L, 1162L, 1163L, 1164L, 1165L, 1166L, 1404L, 1405L,
1406L, 1407L, 1408L, 1409L, 1410L, 1411L, 1412L, 1413L, 1414L,
1415L, 1416L, 1417L, 1418L, 1419L, 1420L, 1421L, 1422L, 1423L,
1424L, 1425L, 1426L, 1427L, 1428L, 1666L, 1667L, 1668L, 1669L,
1670L, 1671L, 1672L, 1673L, 1674L, 1675L, 1676L, 1677L, 1678L,
1679L, 1680L, 1681L, 1682L, 1683L, 1684L, 1685L, 1686L, 1687L,
1688L, 1689L, 1690L, 1928L, 1929L, 1930L, 1931L, 1932L, 1933L,
1934L, 1935L, 1936L, 1937L, 1938L, 1939L, 1940L, 1941L, 1942L,
1943L, 1944L, 1945L, 1946L, 1947L, 1948L, 1949L, 1950L, 1951L,
1952L, 2190L, 2191L, 2192L, 2193L, 2194L, 2195L, 2196L, 2197L,
2198L, 2199L, 2200L, 2201L, 2202L, 2203L, 2204L, 2205L, 2206L,
2207L, 2208L, 2209L, 2210L, 2211L, 2212L, 2213L, 2214L))
以下是我创建图表的代码
library(ggplot2)
ggplot(mydf, aes(x=date, y=outcome)) +
geom_line(aes(group = trial, color = trial, linetype = trial), data = mydf, size=0.75) +
geom_text(data = mydf[mydf$date==max(mydf$date),], aes(x = date + (0.003 * nrow(mydf)), y = outcome, label = sprintf("%0.1f%%", round(outcome, 2) * 100)), size = 4, colour = "grey50") +
scale_y_continuous(limits=c(0, 1))