0

I want to combine with ggplot2 a layer where I compare three regressions and a layer where I just display three new samples to evaluate their locations to the regressions lines. (the mock data is placed at the end of the mail).

  1. Comparison of the three regressions (it works). Using group, I plot firstly the three regressions for comparison with the regression line and the samples used to do the regression.
library(ggplot2)

p1 <- ggplot(  data = ci_95
              , aes(x=x, y=fit
                    , group  = cond1
                    , colour = cond1
                    , fill = cond1)) +
  geom_ribbon( data = ci_95
               ,   aes(
                 x = x
                 ,  ymin = lwr
                 , ymax = upr)
               , alpha = 0.3
               , linetype = 0
  ) +  geom_line(data=ci_95, aes(x=x,y=fit)) +
    geom_point(data = data_gg
             , aes(x=x,y=y
                   , col = cond1))

print(p1)

Then, in a separate graph, I look at the four new samples (it works) :

p2 <- ggplot() + geom_point(data=mydata
                            , aes(x=x,y=y
                                  ,col=new_sample_name)
)

print(p2)

But now I want to overlay the comparison and the new samples. To display the new samples I did exactly the same graph but I added group=1 and it doesn't work.

p3 <- p1 +  geom_point(data=mydata
                      , aes(x=x,y=y
                            , group=1
                            ,col=new_sample_name)
)
print(p3)

I don't understand what's happen. I don't understand the legends !!! Why isn't there new_sample_name ?

I found the question "ggplot combine layer and keep legend group" and tried to use the package ggnewscale but it doesn't work :

library(ggnewscale)
p3 <- p1 +
  ggnewscale::new_scale_color() +
  geom_point(data=mydata
                      , aes(x=x,y=y
                      , group = 1
                      , col=new_sample_name)
)
print(p3)

There are three legends.

##------- the mock data ---------------------------

ci_95 <-
structure(list(cond1 = 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, 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, 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, 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, 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, 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, 3L, 3L, 3L), levels = c("gr1",
"gr2", "gr3"), class = "factor"), x = c(0, 5, 10, 15, 20, 25,
30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100,
105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 165,
170, 175, 180, 185, 190, 195, 200, 205, 210, 215, 220, 225, 230,
235, 240, 245, 250, 255, 260, 265, 270, 0, 5, 10, 15, 20, 25,
30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100,
105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 165,
170, 175, 180, 185, 190, 195, 200, 205, 210, 215, 220, 225, 230,
235, 240, 245, 250, 255, 260, 265, 270, 0, 5, 10, 15, 20, 25,
30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100,
105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 165,
170, 175, 180, 185, 190, 195, 200, 205, 210, 215, 220, 225, 230,
235, 240, 245, 250, 255, 260, 265, 270), fit = c(0, 4.9418680120587464,
9.8837360241174927, 14.825604036176239, 19.767472048234985, 24.709340060293734,
29.651208072352478, 34.593076084411223, 39.534944096469971, 44.476812108528719,
49.418680120587467, 54.360548132646215, 59.302416144704956, 64.244284156763712,
69.186152168822446, 74.128020180881194, 79.069888192939942, 84.01175620499869,
88.953624217057438, 93.895492229116186, 98.837360241174935, 103.77922825323368,
108.72109626529243, 113.66296427735116, 118.60483228940991, 123.54670030146866,
128.48856831352742, 133.43043632558616, 138.37230433764489, 143.31417234970365,
148.25604036176239, 153.19790837382115, 158.13977638587988, 163.08164439793865,
168.02351240999738, 172.96538042205614, 177.90724843411488, 182.84911644617361,
187.79098445823237, 192.73285247029111, 197.67472048234987, 202.6165884944086,
207.55845650646737, 212.5003245185261, 217.44219253058486, 222.3840605426436,
227.32592855470233, 232.26779656676109, 237.20966457881983, 242.15153259087859,
247.09340060293732, 252.03526861499608, 256.97713662705485, 261.91900463911355,
266.86087265117231, 0, 5.3286116058825348, 10.65722321176507,
15.985834817647605, 21.314446423530139, 26.643058029412675, 31.971669635295211,
37.300281241177743, 42.628892847060278, 47.957504452942814, 53.28611605882535,
58.614727664707885, 63.943339270590421, 69.271950876472957, 74.600562482355485,
79.929174088238028, 85.257785694120557, 90.5863973000031, 95.915008905885628,
101.24362051176817, 106.5722321176507, 111.90084372353324, 117.22945532941577,
122.5580669352983, 127.88667854118084, 133.21529014706337, 138.54390175294591,
143.87251335882846, 149.20112496471097, 154.52973657059351, 159.85834817647606,
165.1869597823586, 170.51557138824111, 175.84418299412366, 181.1727946000062,
186.50140620588871, 191.83001781177126, 197.1586294176538, 202.48724102353634,
207.81585262941886, 213.1444642353014, 218.47307584118394, 223.80168744706648,
229.130299052949, 234.45891065883154, 239.78752226471408, 245.1161338705966,
250.44474547647914, 255.77335708236168, 261.1019686882442, 266.43058029412674,
271.75919190000928, 277.08780350589183, 282.41641511177437, 287.74502671765691,
0, 5.0746119502607421, 10.149223900521484, 15.223835850782226,
20.298447801042968, 25.37305975130371, 30.447671701564452, 35.522283651825198,
40.596895602085937, 45.671507552346682, 50.746119502607421, 55.820731452868166,
60.895343403128905, 65.969955353389651, 71.044567303650396, 76.119179253911128,
81.193791204171873, 86.268403154432619, 91.343015104693364, 96.41762705495411,
101.49223900521484, 106.56685095547559, 111.64146290573633, 116.71607485599708,
121.79068680625781, 126.86529875651856, 131.9399107067793, 137.01452265704003,
142.08913460730079, 147.16374655756152, 152.23835850782226, 157.31297045808302,
162.38758240834375, 167.46219435860451, 172.53680630886524, 177.61141825912597,
182.68603020938673, 187.76064215964746, 192.83525410990822, 197.90986606016895,
202.98447801042968, 208.05908996069044, 213.13370191095117, 218.20831386121191,
223.28292581147267, 228.3575377617334, 233.43214971199416, 238.50676166225489,
243.58137361251562, 248.65598556277638, 253.73059751303711, 258.80520946329784,
263.8798214135586, 268.95443336381936, 274.02904531408007), lwr = c(-3.6272433922174332,
1.3144992249573315, 6.255991078370168, 11.19723224601222, 16.138222857778928,
21.078963095335556, 26.019453191929369, 30.959693432148967, 35.89968415163127,
40.839425736716933, 45.778918624054938, 50.718163300157428, 55.657160300905623,
60.595910211008274, 65.534413663413531, 70.472671338676079, 75.410683964280452,
80.348452313922508, 85.285977206750502, 90.223259506567416, 95.160300120996354,
100.09710000061095, 105.0336601380324, 109.96998156699527, 114.90606536138408,
119.84191263424225, 124.77752453675608, 129.71290225721518, 134.64804701995195,
139.5829600842616, 144.51764274330523, 149.45209632299799, 154.38632218088375,
159.32032170499946, 164.25409631272973, 169.18764744965497, 174.120976588394,
179.05408522744364, 183.98697489001663, 188.91964712288012, 193.85210349519622,
198.7843455973661, 203.71637503987989, 208.64819345217305, 213.57980248149158,
218.51120379176675, 223.44239906250132, 228.37338998766811, 233.30417827462216,
238.23476564302814, 243.16515382380331, 248.09534455807761, 253.02533959617151,
257.95514069659271, 262.88474962505251, -9.4081243666885896,
-4.0798380024664249, 1.2477979458878838, 6.5747836806630477,
11.901119538773989, 17.22680599141307, 22.551843643562954, 27.876233233372304,
33.199975631395851, 38.523071839700599, 43.845522990840266, 49.167330346700574,
54.488495297217845, 59.809019358974176, 65.128904173672254, 70.448151506493559,
75.766763244343451, 81.084741393987457, 86.402088080082706, 91.718805543109099,
97.034896137204527, 102.35036232790932, 107.66520668982425, 112.97943190418755,
118.29304075637575, 123.60603613333357, 128.91842102093838, 134.2301985013039,
139.54137175002927, 144.85194403339824, 150.16191870553391, 155.47129920551453,
160.7800890544554, 166.08829185256261, 171.39591127616271, 176.70295107471458,
182.00941506780791, 187.31530714215288, 192.62063124856658, 197.92539139896002,
203.22959166333084, 208.53323616676551, 213.83632908645566, 219.1388746487325,
224.44087712612293, 229.74234083443122, 235.0432701298499, 240.34366940610312,
245.64354309162516, 250.94289564677808, 256.24173156111033, 261.54005535065932,
266.83787155530041, 272.13518473614454, 277.43199947298638, -3.6193222006905046,
1.4551646285242055, 6.5294012415960934, 11.603387716345988, 16.677124182385661,
21.750610820983649, 26.823847864877866, 31.896835598035416, 36.969574355360251,
42.042064522349335, 47.114306534698102, 52.186300877856233, 57.258048086534707,
62.329548744165358, 67.400803482314146, 72.471812980049506, 77.542577963267306,
82.613099203973817, 87.683377519528435, 92.753413771847747, 97.823208866572784,
102.89276375220133, 107.96207941918688, 113.03115689900655, 118.09999726319964,
123.16860162237896, 128.23697112521683, 133.30510695740787, 138.37301034061079,
143.4406825313707, 148.50812482002473, 153.57533852959244, 158.64232501465318,
163.70908566021262, 168.77562188056007, 173.84193511811912, 178.90802684229271,
183.97389854830516, 189.03955175604284, 194.10498800889508, 199.17020887259716,
204.23521593407727, 209.30001080030843, 214.36459509716778, 219.42897046830407,
224.49313857401481, 229.55710109013512, 234.62085970693843, 239.68441612805134,
244.74777206938307, 249.81092925807087, 254.87388943144242, 259.93665433599585,
264.99922572639866, 270.06160536450597), upr = c(3.6272433922174332,
8.5692367991601621, 13.511480969864817, 18.453975826340258, 23.396721238691043,
28.339717025251911, 33.282962952775584, 38.226458736673479, 43.170204041308672,
48.114198480340505, 53.058441617119996, 58.002932965135003, 62.94767198850429,
67.89265810251915, 72.83789067423136, 77.783369023086308, 82.729092421599432,
87.675060096074873, 92.621271227364375, 97.567724951664957, 102.51442036135352,
107.46135650585641, 112.40853239255246, 117.35594698770706, 122.30359921743575,
127.25148796869507, 132.19961209029879, 137.14797039395714, 142.09656165533784,
147.04538461514571, 151.99443798021954, 156.94372042464431, 161.89323059087602,
166.84296709087783, 171.79292850726503, 176.74311339445731, 181.69352027983575,
186.64414766490358, 191.59499402644812, 196.54605781770209, 201.49733746950352,
206.44883139145111, 211.40053797305484, 216.35245558487915, 221.30458257967814,
226.25691729352044, 231.20945804690334, 236.16220314585408, 241.11515088301749,
246.06829953872904, 251.02164738207134, 255.97519267191456, 260.92893365793816,
265.8828685816344, 270.83699567729212, 9.4081243666885896, 14.737061214231495,
20.066648477642254, 25.396885954632161, 30.727773308286288, 36.059310067412284,
41.391495627027467, 46.724329248983182, 52.057810062724705, 57.391937066185029,
62.726709126810434, 68.062124982715204, 73.39818324396299, 78.734882393971745,
84.072220791038717, 89.410196669982497, 94.748808143897662, 100.08805320601874,
105.42792973168855, 110.76843548042724, 116.10956809809687, 121.45132511915716,
126.7937039690073, 132.13670196640905, 137.48031632598594, 142.82454416079315,
148.16938248495345, 153.51482821635301, 158.86087817939267, 164.20752910778879,
169.55477764741821, 174.90262035920267, 180.25105372202682, 185.60007413568471,
190.94967792384969, 196.29986133706285, 201.6506205557346, 207.00195169315472,
212.3538507985061, 217.7063138598777, 223.05933680727196, 228.41291551560238,
233.76704580767731, 239.1217234571655, 244.47694419154016, 249.83270369499695,
255.1889976113433, 260.54582154685517, 265.90317107309818, 271.26104172971031,
276.61942902714316, 281.97832844935925, 287.33773545648324, 292.6976454874042,
298.05805396232745, 3.6193222006905046, 8.6940592719972791, 13.769046559446874,
18.844283985218464, 23.919771419700275, 28.995508681623772, 34.071495538251042,
39.14773170561498, 44.224216848811622, 49.300950582344029, 54.37793247051674,
59.4551620278801, 64.53263871972311, 69.610361962613936, 74.688331124986647,
79.766545527772749, 84.845004445076441, 89.92370710489142, 95.002652689858294,
100.08184033806047, 105.1612691438569, 110.24093815874984, 115.32084639228579,
120.40099281298761, 125.48137634931598, 130.56199589065815, 135.64285028834178,
140.72393835667219, 145.80525887399079, 150.88681058375235, 155.96859219561978,
161.05060238657359, 166.13283980203431, 171.21530305699639, 176.29799073717041,
181.38090140013281, 186.46403357648074, 191.54738577098976, 196.6309564637736,
201.71474411144283, 206.79874714826221, 211.88296398730361, 216.96739302159392,
222.05203262525603, 227.13688115464126, 232.22193694945199, 237.30719833385319,
242.39266361757134, 247.4783310969799, 252.56419905616968, 257.65026576800335,
262.73652949515326, 267.82298849112135, 272.90964100124006, 277.99648526365417
)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA,
-165L), groups = structure(list(cond1 = structure(1:3, levels = c("gr1",
"gr2", "gr3"), class = "factor"), .rows = structure(list(1:55,
    56:110, 111:165), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -3L), .drop = TRUE))


data_gg <-
  structure(list(sample_name = structure(c(1L, 1L, 1L, 3L, 3L,
                                           3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L,
                                           9L, 9L, 9L, 10L, 10L, 10L, 2L, 2L, 2L), levels = c("sample_1",
"sample_10", "sample_2", "sample_3", "sample_4", "sample_5",
"sample_6", "sample_7", "sample_8", "sample_9"), class = "factor"),
                 cond1 = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L,
                                     2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L,
                                     2L, 3L, 1L, 2L, 3L), levels = c("gr1", "gr2", "gr3"), class = "factor"),
                 y = c(66.841303063926276, 67.472560483496395, 67.350782025297491,
                       239.92038943963996, 263.86434567960532, 245.5828943233048,
                       79.816014407733576, 83.113561986286527, 82.472407359578497,
                       119.47728818995286, 124.6096674548085, 123.53929680828162,
                       167.74945967316481, 175.24119274447909, 173.3101276414092,
                       179.9757883770354, 188.0353953366776, 185.62371789029476,
                       211.67636686196198, 226.0245057723744, 217.67567556426681,
                       239.2332514084444, 263.43071638223682, 246.36541140534734,
                       239.12520839705132, 261.48215841483324, 244.32149876740914,
                       225.41906797385775, 243.4863489065367, 230.85842575876438
                 ), x = c(63.943257223109704, 63.943257223109704, 63.943257223109704,
                          243.86202494837679, 243.86202494837679, 243.86202494837679,
                          79.830727091681908, 79.830727091681908, 79.830727091681908,
                          120.17775154264349, 120.17775154264349, 120.17775154264349,
                          170.68397678049993, 170.68397678049993, 170.68397678049993,
                          182.76437013692455, 182.76437013692455, 182.76437013692455,
                          213.08836061920164, 213.08836061920164, 213.08836061920164,
                          242.88283256045045, 242.88283256045045, 242.88283256045045,
                          243.01167748338918, 243.01167748338918, 243.01167748338918,
                          226.30406793610797, 226.30406793610797, 226.30406793610797
                 )), row.names = c(NA, -30L), class = "data.frame")


mydata <-
  structure(list(new_sample_name = c("new_1", "new_2", "new_3",
                                     "new_4"), cond1 = c("gr3", "gr3", "gr3", "gr3"), x = c(237.95320350271339,
179.00466406850148, 114.82070964229392, 186.88105015735715),
                 y = c(236.9327999649125, 179.57983976081474, 115.38365944625376,
                       187.86604427125403)), class = c("grouped_df", "tbl_df", "tbl",
"data.frame"), row.names = c(NA, -4L), groups = structure(list(
new_sample_name = c("new_1", "new_2", "new_3", "new_4"),
                                                         .rows = structure(list(1L, 2L, 3L, 4L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -4L), .drop = TRUE))
1
  • try giving each plot its own guides statement and give them their own title, for p1 use guides( color = guide_legend( title = "condition 1" ) ) and for p2 use guides( color = guide_legend( title = "new name" ) ) Commented Apr 14 at 19:59

1 Answer 1

1

If you assign a guide to each plot and give them unique titles, you get just two legends

library(ggplot2)

p1 <- ggplot(data = ci_95) +
  aes(
    colour = cond1, 
    fill = cond1,
    group = cond1
    ) +
  geom_ribbon(
    data = ci_95,
    aes(
      x = x,
      ymin = lwr,
      ymax = upr
    ),
    alpha = 0.3,
    linetype = 0
  ) +
  geom_line(
    data = ci_95,
    aes(
      x = x,
      y = fit
    )
  ) +
  geom_point(
    data = data_gg, 
    aes(
      x = x, 
      y = y, 
      )
  )

p1
p1 +
  # combine color and fill into one legend
  guides(
    color = guide_legend(title = "condition 1"),
    fill = guide_legend(title = "condition 1")
  ) +
  ggnewscale::new_scale_color() +
  geom_point(
    data = mydata,
    aes(
      x = x,
      y = y,
      group = 1,
      col = new_sample_name
    )
  ) +
  guides(color = guide_legend(title = "new sample name"))

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

I don't understand, even with the two guides statements (I cut and pasted the code) there are three legends, the third one is cond1. I miss something.
I've updated the solution with the full code.
THANK YOU VERY MUCH for the complement because I would have never been able to find the line : fill = guide_legend(title = "condition 1") and I searched a long time. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.