我想自动调整我的图例的垂直位置,以便图例最终接近数据。我希望图例的中心与图例描述的不同分组的中位数具有相同的垂直位置。或者,如果将图例居中于中位数,则会使顶部对齐或底部对齐将导致图例延伸到y轴范围之外。
这对我来说是一个问题,因为我制作了数百张图,并且需要y轴来覆盖所有可能的值,即使数据通常聚集在一起。非常感谢您的帮助 :)
以下是我的数据和制作图表的代码:
mydf <- structure(list(date=structure(c(15573,15574,15575,15576,
15577,15578,15579,15580,15581,15582,15583,15573,15574,
15575,15576,15577,15578,15579,15580,15581,15582,15583,
15573,15574,15575,15576,15577,15578,15579,15580,15581,
15582,15583,15573,15574,15575,15576,15577,15578,15579,
15580,15581,15582,15583,15573,15574,15575,15576,15577,
15578,15579,15580,15581,15582,15583,15573,15574,15575,
15576,15577,15578,15579,15580,15581,15582,15583,15573,
15574,15575,15576,15577,15578,15579,15580,15581,15582,
15583,15573,15574,15575,15576,15577,15578,15579,15580,
15581,15582,15583,15573,15574,15575,15576,15577,15578,
15579,15580,15581,15582,15583),class="Date"),outcome=c(0.0564,
0.0539,0.0586,0.05,0.055,0.061,0.0507,0.0495,0.0609,0.0517,
0.0509,0.0326,0.028,0.0315,0.0286,0.0296,0.0281,0.036,
0.0352,0.026,0.0283,0.0324,0.0098,0.003,0.0039,0.0045,
0.0028,0.0087,0.0108,0.0091,0.0042,3e-04,0.0126,0.0199,
0.013,0.0144,0.0215,0.0193,0.0181,0.0255,0.0138,0.0166,
0.024,0.026,0.0511,0.0533,0.0497,0.0498,0.0087,0.0087,
0.0095,0.0124,0.0063,0.0125,0.0114,0.0097,0.0213,0.0242,
0.0205,0.0207,0.0207,0.0199,0.0199,0.0217,0.0193,0.0144,
0.0084,0.0046,0.0122,0.0116,0.006,0.0158,-0.0013,0.0114,
0.0028,0.0119,0.0125,0.0029,0.0069,0.0053,-0.0019,0.0086,
0.0071,0.0086,0.0063,0.0064,0.0125,0.0152,0.1306,0.1233,
0.1231,0.1185,0.1238,0.1205,0.1257,0.1242,0.1164,0.1248,
0.1147),outcome2=c(0.3564,0.3539,0.3586,0.35,0.355,0.361,
0.3507,0.3495,0.3609,0.3517,0.3509,0.3326,0.328,0.3315,
0.3286,0.3296,0.3281,0.336,0.3352,0.326,0.3283,0.3324,
0.3098,0.303,0.3039,0.3045,0.3028,0.3087,0.3108,0.3091,
0.3042,0.3003,0.3126,0.3199,0.313,0.3144,0.3215,0.3193,
0.3181,0.3255,0.3138,0.3166,0.324,0.326,0.3511,0.3533,
0.3497,0.3498,0.3087,0.3087,0.3095,0.3124,0.3063,0.3125,
0.3114,0.3097,0.3213,0.3242,0.3205,0.3207,0.3207,0.3199,
0.3199,0.3217,0.3193,0.3144,0.3084,0.3046,0.3122,0.3116,
0.306,0.3158,0.2987,0.3114,0.3028,0.3119,0.3125,0.3029,
0.3069,0.3053,0.2981,0.3086,0.3071,0.3086,0.3063,0.3064,
0.3125,0.3152,0.4306,0.4233,0.4231,0.4185,0.4238,0.4205,
0.4257,0.4242,0.4164,0.4248,0.4147),outcome3=c(0.8564,
0.8539,0.8586,0.85,0.855,0.861,0.8507,0.8495,0.8609,0.8517,
0.8509,0.8326,0.828,0.8315,0.8286,0.8296,0.8281,0.836,
0.8352,0.826,0.8283,0.8324,0.8098,0.803,0.8039,0.8045,
0.8028,0.8087,0.8108,0.8091,0.8042,0.8003,0.8126,0.8199,
0.813,0.8144,0.8215,0.8193,0.8181,0.8255,0.8138,0.8166,
0.824,0.826,0.8511,0.8533,0.8497,0.8498,0.8087,0.8087,
0.8095,0.8124,0.8063,0.8125,0.8114,0.8097,0.8213,0.8242,
0.8205,0.8207,0.8207,0.8199,0.8199,0.8217,0.8193,0.8144,
0.8084,0.8046,0.8122,0.8116,0.806,0.8158,0.7987,0.8114,
0.8028,0.8119,0.8125,0.8029,0.8069,0.8053,0.7981,0.8086,
0.8071,0.8086,0.8063,0.8064,0.8125,0.8152,0.9306,0.9233,
0.9231,0.9185,0.9238,0.9205,0.9257,0.9242,0.9164,0.9248,
0.9147),trial=c("C","C","C","C","C","C","C","C","C",
"C","C","B","B","B","B","B","B","B","B","B","B","B",
"F","F","F","F","F","F","F","F","F","F","F","E","E",
"E","E","E","E","E","E","E","E","E","H","H","H","H",
"H","H","H","H","H","H","H","I","I","I","I","I","I",
"I","I","I","I","I","G","G","G","G","G","G","G","G",
"G","G","G","D","D","D","D","D","D","D","D","D","D",
"D","A","A","A","A","A","A","A","A","A","A","A")),.Names=c("date",
"outcome","outcome2","outcome3","trial"),class="data.frame",row.names=c(94L,
95L,96L,97L,98L,99L,100L,101L,102L,103L,104L,359L,
360L,361L,362L,363L,364L,365L,366L,367L,368L,369L,624L,
625L,626L,627L,628L,629L,630L,631L,632L,633L,634L,889L,
890L,891L,892L,893L,894L,895L,896L,897L,898L,899L,1154L,
1155L,1156L,1157L,1158L,1159L,1160L,1161L,1162L,1163L,
1164L,1419L,1420L,1421L,1422L,1423L,1424L,1425L,1426L,
1427L,1428L,1429L,1684L,1685L,1686L,1687L,1688L,1689L,
1690L,1691L,1692L,1693L,1694L,1949L,1950L,1951L,1952L,
1953L,1954L,1955L,1956L,1957L,1958L,1959L,2214L,2215L,
2216L,2217L,2218L,2219L,2220L,2221L,2222L,2223L,2224L
))
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))