Home > Mobile >  How to label for each trendline instead of making a legend
How to label for each trendline instead of making a legend

Time:11-29

I am trying to add label for each trend line. My code and dataset are the followings.
In specific, I want to label each line so that I may not have to put a legend. For some reason, it keeps generating error message mapping must be created by aes(). It seems that code for labeling on the plot has some mistakes of which I am not sure. I refer to Plot labels at ends of lines, but still confused.

df_hhi4 = df3 %>% group_by(year, v5) %>% 
mutate(share = (v97/sum(v97))*100) %>% 
summarize(hhi = sum(share^2)) %>% 
mutate(group = factor(v5))


ggplot(df_hhi4, aes(x = year, y = hhi, colour = group))   
geom_line()   geom_text(df_hhi4, aes(label = group, colour = group, x = Inf, y = hhi), hjust = -.1)  scale_x_continuous(breaks=c(1992, 1997, 2000,2003), lim = c(1992, 2003)) theme_bw() annotate("rect", xmin = 2000, xmax = Inf, ymin =-Inf, ymax =Inf, alpha = .15)

structure(list(year = structure(c(1992, 1992, 1992, 1992, 1992, 
1992, 1992, 1992, 1992, 1992, 1992, 1992, 1992, 1992, 1992, 1992, 
1992, 1992, 1992, 1992, 1992, 1992, 1992, 1992, 1993, 1993, 1993, 
1993, 1993, 1993, 1993, 1993, 1993, 1993, 1993, 1993, 1993, 1993, 
1993, 1993, 1993, 1993, 1993, 1993, 1993, 1993, 1993, 1993, 1994, 
1994, 1994, 1994, 1994, 1994, 1994, 1994, 1994, 1994, 1994, 1994, 
1994, 1994, 1994, 1994, 1994, 1994, 1994, 1994, 1994, 1994, 1994, 
1994, 1994, 1995, 1995, 1995, 1995, 1995, 1995, 1995, 1995, 1995, 
1995, 1995, 1995, 1995, 1995, 1995, 1995, 1995, 1995, 1995, 1995, 
1995, 1995, 1995, 1995, 1996, 1996, 1996, 1996, 1996, 1996, 1996, 
1996, 1996, 1996, 1996, 1996, 1996, 1996, 1996, 1996, 1996, 1996, 
1996, 1996, 1996, 1996, 1996, 1996, 1997, 1997, 1997, 1997, 1997, 
1997, 1997, 1997, 1997, 1997, 1997, 1997, 1997, 1997, 1997, 1997, 
1997, 1997, 1997, 1997, 1997, 1997, 1997, 1997, 1998, 1998, 1998, 
1998, 1998, 1998, 1998, 1998, 1998, 1998, 1998, 1998, 1998, 1998, 
1998, 1998, 1998, 1998, 1998, 1998, 1998, 1998, 1998, 1998, 1999, 
1999, 1999, 1999, 1999, 1999, 1999, 1999, 1999, 1999, 1999, 1999, 
1999, 1999, 1999, 1999, 1999, 1999, 1999, 1999, 1999, 1999, 1999, 
1999, 1999, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 
2000, 2000, 2000, 2000, 2000, 2001, 2001, 2001, 2001, 2001, 2001, 
2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 
2001, 2001, 2001, 2001, 2001, 2001, 2001, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2003, 2003, 
2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 
2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003
), format.stata = "%9.0g"), v5 = structure(c(10, 14, 15, 17, 
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 
34, 35, 36, 37, 10, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 25, 
26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 10, 13, 14, 15, 
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 
33, 34, 35, 36, 37, 10, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 
25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 10, 14, 15, 
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 
33, 34, 35, 36, 37, 10, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 
25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 10, 14, 15, 
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 
33, 34, 35, 36, 37, 10, 11, 12, 15, 17, 18, 19, 20, 21, 22, 23, 
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 10, 11, 
12, 15, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 
31, 32, 33, 34, 35, 36, 37, 10, 12, 15, 17, 18, 19, 20, 21, 22, 
23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 10, 
12, 15, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 
31, 32, 33, 34, 35, 36, 37, 10, 12, 15, 17, 18, 19, 20, 21, 22, 
23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37), label = "industry classification", format.stata = "s.0g", labels = c(`P(agricultural)` = 1, 
`P(forestry)` = 2, `P(fishery)` = 5, 
`P(mining)` = 10, `P(oil, natural gas)` = 11, 
`P(mining 2)` = 12, `P(mining 3)` = 13, `P(mining 4)` = 14, 
`M(food)` = 15, `M(tobacco)` = 16, `M(textile)` = 17, 
`M(clothing, fur)` = 18, `M(leather, bags, shoes)` = 19, 
`M(wood)` = 20, `M(pulp)` = 21, 
`Printing` = 22, `M(petroleum, coke, nuclear)` = 23, 
`M(chemical)` = 24, `M(gum, plastic)` = 25, 
`M(non-metal)` = 26, `M(metal)` = 27, 
`M(metal-assembly)` = 28, `M(unclassifiable machinery)` = 29, 
`M(computer)` = 30, `M(unclassifiable electrical machines)` = 31, 
`M(telecommunication)` = 32, `M(medical, optical, precision machinery)` = 33, 
`M(automobile)` = 34, `M(unclassifiable automobile)` = 35, 
`M(furniture)` = 36, `Construction` = 45), class = c("haven_labelled", "vctrs_vctr", "double")), hhi = c(394.383913182351, 
38.0801656067744, 30.0326294736874, 24.7521236733799, 18.3126043708068, 
43.6744934675323, 61.7361225699876, 164.425040214877, 95.5701278229919, 
3297.08822317966, 42.7146359884886, 64.2273420846951, 27.005573802979, 
571.36651093846, 75.2732785978811, 30.322882749353, 352.596091131053, 
85.1823231673531, 200.835325304756, 111.952988219587, 314.198431236067, 
1638.05835100482, 57.3256385420334, 675.51309091688, 457.785430479347, 
45.0697074416156, 24.7660341348632, 9.97537631608406, 29.3459789802799, 
35.5977902528109, 45.3490572230649, 71.4600830619731, 83.8334297443378, 
3878.00818396799, 41.8675682645623, 53.7559701774767, 31.0791668510115, 
529.261268839702, 108.559155202092, 22.9485078879889, 770.643175341473, 
36.7735565125138, 353.837816002246, 151.259249623411, 539.27095373908, 
681.637363471426, 26.5057069589742, 660.962197813434, 498.634833383036, 
4277.28340307325, 44.4286422165634, 31.4608977480354, 9.71835869038991, 
22.5403059759785, 66.1111750748186, 65.7228316062444, 91.99678656251, 
106.93338328768, 4565.01388338255, 61.0498579258827, 64.8785567264267, 
40.3343676510339, 798.718915809646, 8.43127228446104, 25.8518443230045, 
778.333766827968, 52.9322886043841, 455.428678877652, 59.0947877944083, 
737.292396754178, 2214.44086714103, 36.3355872349196, 440.061630194797, 
522.800606929144, 53.1831583011444, 31.2464335846594, 10.8256939818128, 
28.8435195589226, 40.153608559636, 116.378819886471, 91.9495426230522, 
81.9492010537763, 1321.00803318933, 65.5217662997803, 182.687776765954, 
47.3676554204626, 1168.47743418268, 9.35930770773066, 21.7154052930075, 
178.074815813373, 39.1773279726341, 847.87310022576, 58.5387802045211, 
762.286776750171, 579.617655398054, 33.513989294682, 575.164667242332, 
1861.86780112905, 166.888173418913, 74.4842017510786, 42.8539506234503, 
80.2939183082638, 187.094624005146, 238.709086305178, 197.552652750054, 
359.808943365664, 1171.97648452499, 150.966165264893, 64.9984020971181, 
175.299094875009, 203.224997987829, 15.7532394509596, 79.7993822300948, 
563.901475769996, 105.495584001492, 547.258828622135, 170.684171332889, 
1144.70468796318, 4050.43953096033, 113.264037861955, 726.502339882713, 
1914.5748135629, 225.478544168188, 88.8055683512757, 53.7606501348449, 
66.8605094932516, 193.931806365187, 673.216973521294, 216.394430030425, 
319.1145059085, 1584.83164045303, 127.643650730535, 95.7178678235456, 
168.603159045731, 273.812959210006, 18.5528339617831, 127.968204648469, 
658.556355252612, 150.361703232099, 924.288656175701, 112.687040268529, 
1342.95944521461, 210.314858946725, 116.660285766606, 541.823739031936, 
732.79032529478, 168.146633724925, 21.6886141930239, 14.9263463623755, 
21.0688987295627, 25.7075269680137, 107.506754487588, 132.548353928483, 
80.3008299378163, 3084.45742164655, 47.4841449512966, 62.091353288931, 
45.7980373214646, 420.819681103412, 123.276480160528, 36.0568120446956, 
1565.71466451906, 51.6558900246442, 348.366432198157, 94.9815687382927, 
399.007792357465, 848.362427322852, 29.0758885738095, 162.383952143783, 
723.073094625819, 2855.20630826536, 100.398245285199, 31.4177224432867, 
17.1574328960279, 13.3841564192827, 29.2161362941304, 89.7904799825801, 
91.9183505211285, 78.945629242499, 3746.28808378634, 37.2893474610639, 
91.7132991700561, 48.5661726726528, 390.534555618734, 27.983420175632, 
43.6289136089101, 1326.01005269323, 39.815412595509, 390.366557082487, 
93.4287095330928, 697.584214405171, 1168.85763347377, 23.1899609256412, 
125.461600224985, 919.457003502553, 3969.17629044255, 88.9775205952186, 
24.6512084082924, 12.6658289025524, 11.2083549836225, 24.6691699944811, 
74.4567733423912, 91.7614195334266, 84.6277471028106, 3476.42745006007, 
37.2259369724338, 37.1331767262695, 57.2966311629608, 377.75403488605, 
21.8152249959252, 24.9779364721636, 1719.76988421793, 36.6584853734165, 
353.671754646249, 42.0317213157869, 685.157280952937, 1217.65217855664, 
24.1564219738513, 91.7936996817451, 1086.64565527087, 110.101906969311, 
22.4090729781254, 8.85818308368766, 20.73908826467, 77.0404177282726, 
46.9494257009488, 85.0341969843073, 58.2454768933692, 3489.03544027429, 
31.2046992777881, 40.3643279889142, 55.0998354128002, 352.689378895368, 
25.7646709114131, 30.8900441176465, 1428.53106628259, 29.2003796483345, 
309.067709332579, 27.556355444592, 876.479624266762, 982.444209844482, 
27.3650829437821, 77.6141328806056, 1313.34923233111, 96.1328975042862, 
22.1340303588489, 7.39802269980194, 14.3389259373549, 49.5669343049412, 
53.2005387269099, 78.6445588480826, 66.7394362025268, 2930.5957255729, 
32.8414904527762, 35.3554264705458, 44.8397718939749, 352.09401300637, 
20.3001628780857, 26.3154395375311, 1747.31280465066, 24.3149911036694, 
373.117066992827, 35.8211863272719, 222.80627678374, 989.390987386264, 
24.9166780272894, 77.545543680839, 1241.05689771729, 93.2554427854188, 
21.5009489783546, 6.40979349557045, 30.1517289906289, 44.6539396082126, 
26.0497397953054, 73.2468558884983, 61.0431593171497, 2838.78482737057, 
34.8860370304562, 28.3469853627934, 44.7477762662968, 424.344125913353, 
12.3745036846161, 28.2001123486263, 1150.00235921001, 22.0736660859962, 
352.334738983458, 29.6319812115434, 200.583107344393, 842.377715439857, 
18.5822859951623, 77.6409838753705), group = structure(c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
19L, 20L, 21L, 22L, 23L, 24L, 1L, 25L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 
21L, 22L, 23L, 24L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 
24L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 
20L, 21L, 22L, 23L, 24L, 1L, 26L, 27L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 
22L, 23L, 24L, 1L, 26L, 27L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 
24L, 1L, 27L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 1L, 27L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 1L, 27L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
19L, 20L, 21L, 22L, 23L, 24L), .Label = c("10", "14", "15", "17", 
"18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", 
"29", "30", "31", "32", "33", "34", "35", "36", "37", "13", "11", 
"12"), class = "factor")), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -291L), groups = structure(list(
    year = structure(c(1992, 1993, 1994, 1995, 1996, 1997, 1998, 
    1999, 2000, 2001, 2002, 2003), format.stata = "%9.0g"), .rows = structure(list(
        1:24, 25:48, 49:73, 74:97, 98:121, 122:145, 146:169, 
        170:194, 195:219, 220:243, 244:267, 268:291), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -12L), .drop = TRUE))

CodePudding user response:

As you probably want only one label you could filter your dataset passed to geom_text for the last year in your dataset and map year on x:

library(ggplot2)

ggplot(df_hhi4, aes(x = year, y = hhi, colour = group))  
  geom_line()  
  geom_text(data = subset(df_hhi4, year == max(year)), aes(label = group, colour = group, x = year, y = hhi), hjust = -.1)  
  scale_x_continuous(breaks = c(1992, 1997, 2000, 2003), lim = c(1992, 2003))  
  theme_bw()  
  annotate("rect", xmin = 2000, xmax = Inf, ymin = -Inf, ymax = Inf, alpha = .15)

  • Related