I have two data frames, one gives points for drawing three lines ( rAge.df
) and one for points on which the lines are based ( rEU.df
).
library(ggplot2)
rAge.df<-structure(list(Age = c(35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
57, 58, 59, 60), variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("ND", "SD", "PV"), class = "factor"),
Share = c(16.4959988843349, 17.7615473921574, 19.0861268275909,
20.4679657722585, 21.9047820649977, 23.3937954133475, 24.9317508644732,
26.5149527816908, 28.139308544203, 29.8003807884421, 31.4934466656336,
33.2135623240542, 34.9556306521092, 36.7144702485073, 38.4848836191358,
40.2617227296193, 42.0399502541911, 43.8146951366457, 45.5813013960679,
47.3353694462817, 49.0727895322484, 50.7897672004883, 52.4828409994153,
54.1488928391191, 55.785151622991, 57.3891908941929, 51.6496034356313,
49.5869357858129, 47.5118978575713, 45.4315292317247, 43.3530553560567,
41.28379010844, 39.2310337902499, 37.2019694961614, 35.2035609515484,
33.2424548980957, 31.3248909394419, 29.4566214415355, 27.6428436382166,
25.8881455520378, 24.1964667405507, 22.5710742590061, 21.0145536304736,
19.5288140682987, 18.1151067312051, 16.7740544272535, 15.5056909291712,
14.3095079213272, 13.1845075612709, 12.1292586937085, 11.1419548855016,
10.2204726380993, 31.8543976800338, 32.6515168220297, 33.4019753148377,
34.1005049960168, 34.7421625789455, 35.3224144782125, 35.8372153452769,
36.2830777221478, 36.6571305042486, 36.9571643134622, 37.1816623949245,
37.3298162344103, 37.4015257096742, 37.3973841994549, 37.3186496403135,
37.1672030113746, 36.9454961153352, 36.6564907950555, 36.303591872727,
35.8905761264648, 35.4215195385805, 34.9007248781846, 34.3326514393138,
33.7218484671724, 33.0728934915074, 32.3903364677078)), row.names = c(NA,
-78L), class = "data.frame")
rEU.df<-structure(list(MeanAge = c(48.8553004476329, 50.9208553782224,
51.7996144755705, 48.3924335010784, 49.8040291699645, 51.6064465750308,
44.2004512236123, 51.7996144755705, 50.8599429036558, 51.1341844537536,
51.5841012929359, 51.0868375055975, 52.3291349501681, 44.3581834722897,
50.125224410366, 48.9944572855644, 52.8772671501634, 46.8572780025509,
49.8726702627702, 52.9016200087484, 44.5146659042334, 52.4717924092057,
43.5684603465277, 50.3229230496888, 44.3581834722897, 51.4681525424742,
48.9944572855644, 50.125224410366, 49.8726702627702, 52.7231809158358,
49.8292117687702, 52.7231809158358, 51.0868375055975, 53.3770013131387,
52.6906296509674, 48.9944572855644, 48.8553004476329, 51.6707535830708,
51.6707535830708, 52.8772671501634, 51.3083721733126, 47.0454622157873,
50.7131510488736, 50.8081225812258, 51.9550681604832, 53.006405791163,
52.4145074733917, 48.7092245644304, 54.5400300066882, 52.2009597035923,
50.4887796487131, 50.3420635400908, 55.110505120969, 48.8622481369031,
51.4728114404938, 50.3420635400908, 46.6650241052098, 50.6677435091333,
51.4728114404938, 52.4145074733917, 51.0868375055975, 46.8572780025509,
51.5404073256315, 52.3905298999787, 50.2732415529043, 41.7969648256593,
54.5400300066882, 49.8471433170061, 51.1145214426494, 52.8772671501634,
44.2004512236123, 51.2619310686016, 50.8599429036558, 47.0454622157873,
52.6748750541104, 50.7745528422084, 52.5012011864781, 46.3075987053115,
49.8040291699645, 53.006405791163, 50.4887796487131, 53.3770013131387
), variable = structure(c(3L, 1L, 1L, 3L, 3L, 1L, 2L, 2L, 3L,
2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 3L, 2L, 1L, 2L, 1L, 2L, 3L, 2L,
1L, 2L, 1L, 3L, 1L, 3L, 2L, 3L, 2L, 3L, 3L, 2L, 1L, 3L, 3L, 1L,
2L, 2L, 2L, 1L, 1L, 3L, 3L, 3L, 1L, 3L, 3L, 3L, 2L, 2L, 1L, 1L,
2L, 1L, 2L, 1L, 2L, 2L, 3L, 1L, 1L, 1L, 3L, 3L, 1L, 1L, 2L, 1L,
3L, 1L, 3L, 1L, 2L, 1L, 2L, 2L, 3L), .Label = c("ND", "SD", "PV"
), class = "factor"), Share = c(27.625634115409, 32.2814072926139,
56.384858044164, 34.7655986509275, 38.2228587954993, 47.1566923267941,
33.1835059605237, 20.2902208201893, 31.8768659727265, 23.6260666673969,
34.8793774319066, 15.2361775316721, 51.1094443402713, 43.3937756533486,
18.8452733262605, 17.3539115669736, 23.4143178610931, 54.787770712593,
26.0606348753546, 43.0121951219512, 18.2241026646866, 48.7757255936675,
54.4961927683711, 39.7116806308526, 23.837481165896, 43.2792381233076,
12.6050884192217, 61.6003126919426, 24.895798665589, 36.2653010926418,
20.3900134327667, 12.4928115672153, 47.6885644768856, 18.6540198735321,
41.1970823798627, 70.0410000138047, 31.9584258084971, 31.1343057228244,
54.4649627548062, 27.9415690911171, 45.4125164442774, 31.0376740461905,
20.0911195757712, 28.1058295349395, 40.2115979106705, 31.6228070175439,
37.0480272557424, 25.9252026169319, 34.4557969971699, 41.6638098503028,
20.6310523953056, 36.1683017825066, 44.5089389366148, 35.5591236818947,
21.6832762379321, 42.1825341992538, 21.8815321732416, 24.6493808579052,
30.4214678708606, 19.533282045646, 37.0752579914422, 14.7469438513339,
20.5721956539978, 38.4446878422782, 43.7932453154804, 30.8756062393498,
53.8110999184535, 45.3571601296126, 42.1975168384322, 48.6441130477898,
36.8740698426061, 16.3615612873773, 45.5579762769305, 49.5034679255878,
38.9090335803248, 56.611669632979, 47.7143668370244, 22.0682432596995,
41.9495668700335, 17.1842105263158, 22.1445520425832, 28.3915192093097
)), row.names = c(178L, 60L, 75L, 174L, 190L, 4L, 134L, 158L,
225L, 166L, 61L, 98L, 30L, 32L, 151L, 82L, 99L, 218L, 89L, 53L,
159L, 3L, 156L, 179L, 115L, 70L, 165L, 68L, 172L, 25L, 175L,
108L, 181L, 164L, 194L, 248L, 95L, 58L, 224L, 182L, 23L, 84L,
85L, 90L, 37L, 55L, 243L, 223L, 207L, 33L, 212L, 229L, 200L,
155L, 103L, 63L, 54L, 154L, 20L, 160L, 15L, 135L, 162L, 183L,
47L, 5L, 41L, 209L, 211L, 16L, 51L, 101L, 59L, 167L, 67L, 240L,
48L, 119L, 24L, 138L, 129L, 247L), class = "data.frame")
When I produce the plot, the legend is not very meaningful. I would want it to pick up the symbology for the points data frame, not the lines, eg show squares, circle and triangles in the legend. Also, I'd like to provide the text of the legend, eg replace "ND" with "No Delay".
ggplot() +
geom_line(data = rAge.df, aes(x = Age, y = Share, color = variable), size = 1, linetype = "dashed") +
geom_point(data = rEU.df, aes(x = MeanAge, y = Share, color = variable),
size = 2, shape = as.numeric(rEU.df$variable) - 1) +
ylim (c(0,100)) +
scale_color_manual(values = c("black", "red", "green"))+
scale_shape_manual(values = c(0, 1, 2))
You may use show.legend
argument to hide the lines legend and add custom labels annotations
to the legend with the labels
argument of the scale_color_manual
:
annotations <- c("annot_1", "annot_2", "annot_3")
pl <- ggplot() +
geom_line(data = rAge.df, aes(x = Age, y = Share, color = variable),
size = 1, linetype = "dashed", show.legend = FALSE) +
geom_point(data = rEU.df, aes(x = MeanAge, y = Share, color = variable,
shape = variable), size = 2) +
ylim (c(0,100)) +
scale_color_manual(values = c("black", "red", "green"),
labels = annotations) +
scale_shape_manual(values = c(0, 1, 2),
labels = annotations)
Note, please, that if you want to correspond the points shape with the variable, you should include this variable into the aes()
function. That is why I had to modify the call of the geom_point()
function.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.