3

Instead of adding legends outside the plot, I want to add names such as "Quantile 1", "Quantile 10" etc. inside the plot as labels.

ggplot() + 
  geom_line(data=df.quants, aes(x=xvars, y=q01), color="black", size =0.5) +
  geom_line(data=df.quants, aes(x=xvars, y=q10), color="black", size =0.7) +
  geom_line(data=df.quants, aes(x=xvars, y=q25), color="black", size =1) +
  geom_line(data=df.quants, aes(x=xvars, y=q50), color="black", size =1.2) +
  geom_line(data=df.quants, aes(x=xvars, y=q75), color="black", size =1) +
  geom_line(data=df.quants, aes(x=xvars, y=q90), color="black", size =0.7) +
  geom_line(data=df.quants, aes(x=xvars, y=q99), color="black", size =0.5) +
  geom_ribbon(data=df.quants,aes(x=xvars,ymin=q25,ymax=q75),alpha=0.8)+
  geom_ribbon(data=df.quants,aes(x=xvars,ymin=q10,ymax=q25),alpha=0.5)+
  geom_ribbon(data=df.quants,aes(x=xvars,ymin=q75,ymax=q90),alpha=0.5)+
  geom_ribbon(data=df.quants,aes(x=xvars,ymin=q01,ymax=q10),alpha=0.2)+
  geom_ribbon(data=df.quants,aes(x=xvars,ymin=q90,ymax=q99),alpha=0.2)+
  theme_bw()+
  ggtitle("Q")+
  ggsave("save.png")

How can I add labels on the plot in either or both of these cases:

  1. At the end of the lines (e.g. https://stackoverflow.com/a/37696769/3942806)
  2. Hovering over the lines towards the left hand side (https://stackoverflow.com/a/37672486/3942806)

enter image description here

dput(df.quants)

structure(list(xvars = c(21.1084, 40.4038, 42.4758, 44.0298, 
    44.8068, 53.0948, 53.3538, 54.3898, 56.4617, 62.9367, 67.3397, 
    68.8937, 68.8937, 74.3327, 78.7356, 80.2896, 87.8006, 95.0526, 
    98.6785, 105.6715, 114.4775, 116.5495, 121.7294, 127.4274, 133.6434, 
    146.3343, 153.0683, 158.2483, 163.6872, 169.9032, 180.7812, 186.4791, 
    186.9971, 194.5081, 196.3211, 203.8321, 209.789, 215.746, 218.336, 
    222.998, 230.7679, 237.5019, 241.1279, 242.12505, 250.9698, 254.0778, 
    264.1788, 274.5387, 284.8987, 292.6687, 303.0286, 310.7986, 326.3385, 
    336.6985, 344.4684, 362.5983, 378.1383, 389.275200000003, 393.6782, 
    409.2181, 427.348, 450.6579, 450.6579, 450.6579, 453.2479, 463.6079, 
    476.5578, 489.5078, 499.8677, 510.2277, 523.1776, 533.5376, 543.8975, 
    567.2074, 611.2372, 613.8272, 621.5971, 637.1371, 652.677, 660.447, 
    668.2169, 678.5769, 696.447800000002, 714.8367, 744.880600000001, 
    764.0465, 766.6365, 779.5864, 784.7664, 810.6663, 815.8463, 839.1561, 
    852.1061, 872.826, 896.1359, 903.9059, 916.8558, 937.5757, 966.0656, 
    984.1955), q01 = c(0.07287449, 0.0573897264, 0.0557969357, 0.0537193707, 
    0.0518303892, 0.0518080158, 0.0514479006, 0.0517025388, 0.0516258276, 
    0.0459982978, 0.0471084591, 0.0402322638, 0.0406957203, 0.0373694866919881, 
    0.0375932247971234, 0.0378606448, 0.0374813557445557, 0.036479866, 
    0.0368506252, 0.0362731762, 0.0367099924, 0.0329852952, 0.0280992996, 
    0.0278553199, 0.0277327966, 0.0279245722, 0.0279458806, 0.0277967218, 
    0.0279831703, 0.0237609194, 0.0207585756691242, 0.0207543139705945, 
    0.0196036650330279, 0.0188940975085873, 0.0186277436, 0.0167249092, 
    0.0167046666, 0.0166865548, 0.0161687604, 0.0161591718, 0.0161879376, 
    0.011199656, 0.0111985905, 0.01105689088, 0.0110845916, 0.0106222032239633, 
    0.0109610059070531, 0.0109652675220967, 0.0109769869296186, 0.01043682232, 
    0.00988280416, 0.00995951416, 0.0109119973340507, 0.0099936076, 
    0.00955359025, 0.00895376068, 0.00836565071, 0.00829852949, 0.00825058604, 
    0.00660771382, 0.00658320918, 0.00658320918, 0.00647347103215001, 
    0.00720008536, 0.00722565544, 0.00697102063, 0.00688152578, 0.00688898372, 
    0.00692733882, 0.00618580850401875, 0.0061218835, 0.0061090985, 
    0.00609311725, 0.00584274444, 0.00583954821, 0.00575325, 0.00575325, 
    0.0050873645, 0.00522693399, 0.00516087826, 0.00496803746, 0.00490198142, 
    0.00490198142, 0.00490198142, 0.0057798848, 0.00555082036, 0.00518857905, 
    0.00508949534, 0.0050127851, 0.0049701683, 0.0049275515, 0.00487428075, 
    0.00479011332, 0.00476347782, 0.0047783937, 0.00476880492, 0.00487747698, 
    0.00485510337, 0.0048582996, 0.00479330958), q10 = c(0.492542083954826, 
    0.45454932, 0.43124864, 0.41944381, 0.37937352, 0.38026848, 0.37053055, 
    0.37472832, 0.35278072, 0.34281907, 0.34762414, 0.28118472, 0.28004471, 
    0.272746634508843, 0.24855102, 0.25194972, 0.242531451945451, 
    0.2256552, 0.206232684634562, 0.19673984, 0.20522054, 0.18095038, 
    0.170956713240997, 0.16841043, 0.163157896833582, 0.16456426, 
    0.16100576, 0.15623268, 0.15471976, 0.15003198, 0.143000209281909, 
    0.1429789, 0.14078417, 0.13547836, 0.13492432, 0.13260178, 0.12952273, 
    0.1337311, 0.1273386, 0.123471123238866, 0.12135096, 0.10445342, 
    0.10836355, 0.10458128, 0.10262094, 0.10075645, 0.10061794, 0.099659066, 
    0.099925431, 0.0973364588484978, 0.0949499261725975, 0.0949499261725975, 
    0.1001705, 0.09823141, 0.091689753, 0.0820796952618794, 0.080683995, 
    0.079256336, 0.078052418, 0.0678883475337737, 0.067302367, 0.0675474110377157, 
    0.065970594, 0.068889834, 0.069379928, 0.06499041, 0.0639143363015129, 
    0.0628169628570211, 0.061410608, 0.0606541642970382, 0.0605369686562966, 
    0.06094183, 0.0605476229844449, 0.0566162367773279, 0.056371194, 
    0.054080544, 0.0546025993937779, 0.051779245, 0.052375877, 0.051907098, 
    0.050639252, 0.050308969, 0.050308969, 0.050308969, 0.053643725, 
    0.053505218, 0.0507671, 0.049691026, 0.04884935, 0.04810356, 
    0.04757085, 0.04687833, 0.045727678, 0.045503944, 0.04687833, 
    0.046729174, 0.051971022, 0.050308975, 0.0498615, 0.048039636
    ), q25 = c(0.6404219, 0.6244939, 0.605822475, 0.602040281120818, 
    0.5927978, 0.588349675, 0.579639875, 0.5777754, 0.5689325, 0.55953015, 
    0.5641647, 0.53696995, 0.53358725, 0.518511625, 0.509855075, 
    0.5100149, 0.4870019, 0.477573, 0.46324315, 0.45551885, 0.4599403, 
    0.4263797, 0.414766675, 0.403526531046239, 0.39313875, 0.385361200596633, 
    0.3733752, 0.364585555646708, 0.358486025, 0.34375665, 0.321915594917963, 
    0.3211166, 0.317574060052206, 0.299701695823567, 0.2932026, 0.28931385, 
    0.27794055, 0.28041765, 0.2627317, 0.252370517653953, 0.248348618836565, 
    0.2258683, 0.2306094, 0.22400385, 0.2216066, 0.21785105, 0.214521628563819, 
    0.2121244, 0.210153425, 0.2043469, 0.2011506, 0.2003516, 0.207196875, 
    0.206317924738973, 0.200431525, 0.189537609205199, 0.187220325, 
    0.181600263653314, 0.179948860004262, 0.1733433, 0.1733433, 0.170413425, 
    0.1667377, 0.16812275, 0.16993395, 0.1648199, 0.16276905, 0.15768165, 
    0.15347325, 0.150916225, 0.14910505, 0.14990415, 0.1474537, 0.1441509, 
    0.142712575, 0.138690575, 0.14098125, 0.135813975, 0.137385475, 
    0.14217985, 0.139969074168975, 0.139516275, 0.139516275, 0.139516275, 
    0.143751322267206, 0.1424995, 0.140688225, 0.1389303, 0.136639675, 
    0.134801865933305, 0.137785025, 0.135813975, 0.132537822288515, 
    0.128356075, 0.129900875, 0.1293416, 0.14106115, 0.137598575, 
    0.1421266, 0.1387705), q50 = c(0.7691242, 0.7631579, 0.75559345, 
    0.751917760334541, 0.7506925, 0.746484148060942, 0.7420094, 0.7371617, 
    0.7325804, 0.72565525, 0.72682715, 0.7165992, 0.7147347, 0.702002962412103, 
    0.69640955, 0.6950778, 0.6869806, 0.6838909, 0.6740358, 0.6714255, 
    0.6721713, 0.6567228, 0.64947795, 0.63834435, 0.6361602, 0.6261453, 
    0.613253782228852, 0.603665, 0.593756671084594, 0.579267, 0.5622203, 
    0.5610484, 0.5582783, 0.5308971, 0.5144897, 0.5100149, 0.49355425, 
    0.4915832, 0.4779459, 0.4607394, 0.4513105, 0.424462, 0.42510125, 
    0.4174302, 0.4121031, 0.40336673736416, 0.3968677, 0.3919668, 
    0.39036865, 0.3808864, 0.373961218836565, 0.3727893, 0.38408265, 
    0.3834434, 0.37646495, 0.3681014, 0.3637332, 0.3615491, 0.3576603, 
    0.3552099, 0.35398465, 0.34775195, 0.3416791, 0.3381632, 0.3388025, 
    0.33187725, 0.33059875, 0.3258044, 0.3180268, 0.3064138, 0.3049222, 
    0.3091839, 0.3065736, 0.2999148, 0.29900915, 0.2925634, 0.3009802, 
    0.2956531, 0.29698485, 0.3044961, 0.30412315, 0.3014596, 0.3014596, 
    0.3014596, 0.3147773, 0.3135521, 0.3128596, 0.30998295, 0.3091306, 
    0.3039633, 0.30907735, 0.303856783177072, 0.2949073, 0.28595785, 
    0.285371829778393, 0.2846793, 0.2958662, 0.29607925, 0.3035372, 
    0.30359045), q75 = c(0.8668229, 0.8637865, 0.86104305, 0.858752425, 
    0.8584061, 0.8549169, 0.852066925, 0.8492436, 0.8462604, 0.843783275, 
    0.843410426177285, 0.838909, 0.838349675, 0.833795025, 0.831530975, 
    0.8301726, 0.8252717, 0.8239399, 0.820557211213509, 0.81797355, 
    0.8167483, 0.8088643, 0.80244515, 0.7948274, 0.7947475, 0.787183054698487, 
    0.7810036, 0.77716815, 0.769097575, 0.7540486, 0.74861495, 0.7479224, 
    0.746617297091413, 0.7200618, 0.704879607926699, 0.702588945727679, 
    0.690203494363946, 0.68458345, 0.6740891, 0.658480725, 0.6435649, 
    0.630833155763904, 0.626811225, 0.62129765, 0.6129342, 0.60827295, 
    0.60275945, 0.593117408906883, 0.59125295, 0.58507352145749, 
    0.5743661, 0.5681334, 0.5741264, 0.575431476028127, 0.573353929725122, 
    0.5659493, 0.560702075, 0.556706775, 0.5519923, 0.54645215, 0.53976665, 
    0.53526525, 0.5334541, 0.5324419, 0.52743445, 0.519204135014916, 
    0.519017675, 0.517899, 0.50905605, 0.495258875, 0.5004794, 0.50298315, 
    0.491023875559344, 0.4816748, 0.485164082175581, 0.479171075, 
    0.48889305, 0.482500525, 0.485696775, 0.4886533, 0.4919028, 0.490890725, 
    0.490890725, 0.490890725, 0.506552325, 0.50649905, 0.504741075, 
    0.49978695, 0.502477093543576, 0.492915025, 0.49629765, 0.4903047, 
    0.4775197, 0.4731249, 0.464175375, 0.4632431, 0.46942255, 0.472725375, 
    0.4760281, 0.474536573433838), q90 = c(0.9314937, 0.93079054, 
    0.92979966, 0.928574429757085, 0.92748774, 0.92662476, 0.92585765, 
    0.9235457, 0.92188366, 0.92058388, 0.91802683, 0.91566162, 0.91534197, 
    0.9129022, 0.91192198, 0.91067548, 0.907671, 0.9073088, 0.90595568, 
    0.904773066349883, 0.9035372, 0.897741346746218, 0.89466225, 
    0.889740052584701, 0.88860004, 0.88158958, 0.88022584, 0.878883448075858, 
    0.86893244, 0.863030027896868, 0.86177288, 0.8616024, 0.859290453515875, 
    0.8365012, 0.82799918, 0.824568514587684, 0.819273379833795, 
    0.81630088, 0.81448968, 0.79439591, 0.78960152, 0.783315567773279, 
    0.776464946030258, 0.776731300370765, 0.76594926, 0.75919456, 
    0.75371832, 0.75075644, 0.74735775, 0.74310678, 0.72495204, 0.7112508, 
    0.713807792386533, 0.7141487, 0.71468144, 0.713530774319199, 
    0.71050504, 0.707809474097592, 0.70632856, 0.70217348, 0.69276586, 
    0.68746007, 0.68621354, 0.68354994, 0.67928834, 0.66894312, 0.66838907, 
    0.66891114, 0.66275304248029, 0.65147025, 0.655188582416365, 
    0.65889626, 0.64831662, 0.640571045322821, 0.64626035, 0.64103984, 
    0.637385467717878, 0.63600045, 0.639899844314937, 0.638908996019603, 
    0.64134882, 0.64137014, 0.64137014, 0.64137014, 0.6497443, 0.64977628, 
    0.64734715, 0.64238229, 0.6446303, 0.634775211037716, 0.635307911037716, 
    0.631685502855317, 0.62823354, 0.62139354, 0.61645005, 0.61583208, 
    0.61267846, 0.61427655, 0.6135734, 0.61338161), q99 = c(0.9813552, 
    0.983496688, 0.982289538, 0.981875103, 0.98133816, 0.981210342, 
    0.980869382, 0.98074156, 0.980605188, 0.980628629, 0.980474118, 
    0.97956530234349, 0.979405495101854, 0.979192435, 0.979102933, 
    0.97900704, 0.978866424, 0.97889414, 0.978801398, 0.978465764, 
    0.978167456, 0.97708924, 0.977054095, 0.977258668, 0.975373952, 
    0.972782846, 0.972770058, 0.972859574, 0.972306655, 0.97231411, 
    0.972591068, 0.97259746, 0.972355645, 0.962840406286384, 0.963136582842531, 
    0.943974016, 0.944772016962071, 0.944242504, 0.944018764, 0.935328143137652, 
    0.935241848623481, 0.934724015665886, 0.926759001178564, 0.926816535265289, 
    0.926234804, 0.917745605, 0.91511187081824, 0.915103346793096, 
    0.913937796999999, 0.90959943, 0.903311344, 0.894027274664394, 
    0.895148090432559, 0.897357759215854, 0.897743441, 0.894050713829107, 
    0.893945237587897, 0.889042197096101, 0.889281912489239, 0.888979338, 
    0.886543834, 0.886235893490944, 0.886239091206478, 0.886258277499681, 
    0.868235644, 0.868526506, 0.870900274999999, 0.871323255148946, 
    0.869995737948434, 0.867188371, 0.864700617419987, 0.864726187301939, 
    0.856755777, 0.844385296, 0.855691437, 0.83904428249137, 0.832750945, 
    0.837923525, 0.856581078, 0.855506063483912, 0.855462386340294, 
    0.838475343, 0.838475343, 0.838475343, 0.855438949824206, 0.855484757560196, 
    0.855492214633497, 0.855447472193693, 0.855524173519071, 0.85631259, 
    0.859764525, 0.861629, 0.865305772, 0.861820776, 0.861149560000001, 
    0.861581055999999, 0.861485167999999, 0.858912205, 0.86234816, 
    0.860478344)), .Names = c("xvars", "q01", "q10", "q25", "q50", 
    "q75", "q90", "q99"), row.names = c(NA, -100L), class = "data.frame")
Community
  • 1
  • 1
maximusdooku
  • 5,242
  • 10
  • 54
  • 94
  • 2
    Your code is not [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself); you should reshape your data to long form, and map size and alpha to an aesthetic. – alistaire Jan 19 '17 at 21:55
  • @alistaire Can you show me how to do it, please? It does the job, but I would like to know how to do it efficiently. – maximusdooku Jan 19 '17 at 22:01
  • @maximusdooku lukeA's answer shows how to do it. See how it has *one* `geom_line` call instead of 7. – Gregor Thomas Jan 19 '17 at 22:02
  • @Gregor I am not "melting" into the long form because I need the lines to be of different sizes. Also, the geom_ribbons come with different alpha values. Can I reshape the data and still do these things? lukeA's answer solves my labels issue but introduces new problem of same line sizes and no ribbon colours. – maximusdooku Jan 19 '17 at 22:06
  • 1
    Yes, you can. If you have trouble, ask a new question, but read some similar questions first. [Heroka's answer here looks good](http://stackoverflow.com/q/32267709/903061), [this is another good one](http://stackoverflow.com/a/37329255/903061). – Gregor Thomas Jan 19 '17 at 22:15

1 Answers1

8

You should try the directlabels package. Here's an example:

library(tidyverse)
library(directlabels)
df.quants %>% 
  gather(var,val,-1) %>% 
  ggplot(aes(xvars,val,fill=var)) + 
  geom_area(position="identity", alpha = .5) +
  geom_line() + 
  scale_fill_grey(guide="none") + 
  geom_dl(aes(label=var), method="last.points") 

enter image description here

Make sure to browse through its documentation as there are many more options.


You can extend that approach to also use custom labels, linewidths, fillings etc. One way could be:

labs <- setNames(c("G", "F", "E", "D", "C", "B", "A"), c("q99", "q90", "q75", "q50", "q25", "q10", "q01"))
df.quants %>% 
  rev %>%
  gather(var,val,-xvars,factor_key=TRUE) %>% 
  ggplot(aes(xvars,val,fill=var)) + 
  geom_area(position="identity") +
  geom_line(aes(size=var)) + 
  scale_fill_manual(values = c(grey.colors(3,.9,.3), grey.colors(4)), guide="none") + 
  scale_size_manual(values = c(1:3,4:1), guide="none") + 
  geom_dl(aes(label=labs[var]), method="last.points") 

enter image description here

lukeA
  • 53,097
  • 5
  • 97
  • 100
  • Thanks. This helps. But, I don't want the column names to be the labels. Instead I want to add verbose labels such as "Quantile 1, Quantile 2" etc. How can I modify the gather line? I can always rename the column names I guess, but that is cumbersome and changes a lot of my ggplot code. Also the ggplot I have provided has different levels of alpha and line sizes. – maximusdooku Jan 19 '17 at 22:12
  • Thanks a lot! That works. Just one last question: The labels overflow if the names are long (e.g. Quantile 1) and doesn't resize the plot automatically (Say: https://i.stack.imgur.com/GzwAT.png). It works fine for A,B,C, D ofcourse. How can I stop that from happening? – maximusdooku Jan 19 '17 at 22:59
  • In case of long names, you could expand the x limits. E.g. `+ scale_x_continuous(breaks = seq(0,1000,250), limits = c(0,1200))` – lukeA Jan 20 '17 at 07:41
  • I fixed it using extend. Thank you. – maximusdooku Jan 20 '17 at 21:17