title: How to generate a legend without a plot
body: <p>I wanted to generate this legend without the plot so I can position it separately (I circled it in red):</p>
  <p><a href="https://i.stack.imgur.com/q9Imp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/q9Imp.png" alt="enter image description here" /></a></p>
  <p>I tried the suggestions in this <a href="https://stackoverflow.com/questions/12041042/how-to-plot-just-the-legends-in-ggplot2">page</a> but they did not work.</p>
  <p>I'm hoping to get something like this:
  <a href="https://i.stack.imgur.com/7CeIf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7CeIf.png" alt="enter image description here" /></a></p>
  <p>But the only way that I'm thinking of to be able to position this legend is by making separate legend and then add it to them as suggested <a href="https://stackoverflow.com/questions/12041042/how-to-plot-just-the-legends-in-ggplot2">here</a>.</p>
  <p>My code is here:</p>
  <pre><code>zmapp024 &lt;-ggplot(plot_frame, aes(Var2, Var1, fill = value)) + 
    geom_tile(color = &quot;white&quot;, position = position_dodge(), show.legend = TRUE) +
    geom_point(data = data.frame(Var2 = 1:4, Var1 = 0, value = 0), size = 4,
             aes(color = factor(Var2))) +
    geom_point(data = data.frame(Var2 = 1:4, Var1 = 0, value = 1), alpha = 0) +
    scale_color_manual(values = c(&quot;black&quot;, &quot;forestgreen&quot;, &quot;#DE2D29&quot;, &quot;#3C57A8&quot;),
                       labels = c(expression(
                           CD44^{lo}~&quot;T Cells&quot;,
                           CD44^{hi}~CD69^{lo}~&quot;T Cells&quot;,
                           CD44^{hi}~CD69^{hi}~CD103^{lo}~&quot;T Cells&quot;,
                           CD44^{hi}~CD69^{hi}~CD103^{hi}~&quot;T Cells&quot;)),
                       guide = guide_legend(override.aes = list(fill = NA), label.hjust = 0, position=&quot;bottom&quot;, size = 5 )) +
  
    scale_y_discrete(position = &quot;right&quot;) +
    labs(y = &quot;&quot;, fill = &quot;&quot;, color = &quot; &quot;, x = &quot;&quot;)  +
    scale_fill_gradientn(colors = c(&quot;#3C57A8&quot;, &quot;white&quot;, &quot;#DE2D29&quot;),
                         breaks = c(1.5, 0, -1.5),
                         labels = c(&quot;1.0&quot;, &quot;0&quot;, &quot;-1.0&quot;),
                         limits = c(-1.5, 1.5),
                         space = &quot;Lab&quot;,
                         guide = &quot;colourbar&quot;,
                         aesthetics = &quot;fill&quot;) +
    theme_minimal() + 
  theme (panel.grid.major = element_blank(), 
         axis.text.y.right = element_text(margin = margin(l = unit(-5, &quot;cm&quot;))),
         axis.text.y = element_text(face=&quot;italic&quot;, size=9, 
                                   color=&quot;black&quot;),
         legend.justification = c(-0.9, 0),
         legend.direction = &quot;vertical&quot;,
         legend.key.size = unit(0.6, &quot;cm&quot;),
         legend.key.width = unit(0.2,&quot;cm&quot;),
         legend.title.align = 0.5,
         axis.text.x = element_blank()) +
  guides(
          fill = guide_colourbar(
              title = &quot;Relative gene expression \n (z score)&quot;,
              title.position = &quot;right&quot;,
              title.theme = element_text(angle = -90, size = 7.5),
              direction = &quot;vertical&quot;,
              ticks = FALSE))
  
  
  </code></pre>
  <p>The dput of the data:</p>
  <pre><code>structure(list(Var1 = 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, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 
  34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 
  47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 1L, 2L, 
  3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
  17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 
  30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 
  43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 
  56L, 57L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
  13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 
  26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 
  39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 
  52L, 53L, 54L, 55L, 56L, 57L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
  9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 
  22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 
  35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 
  48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L), .Label = c(&quot;Sdc4&quot;, 
  &quot;Ramp1&quot;, &quot;Ptpn5&quot;, &quot;Prdm1&quot;, &quot;Pmaip1&quot;, &quot;Myo1e&quot;, &quot;Lamc1&quot;, &quot;Itgb8&quot;, 
  &quot;Itgav&quot;, &quot;Foxp3&quot;, &quot;Dst&quot;, &quot;Csf1&quot;, &quot;Bmpr2&quot;, &quot;Xcl1&quot;, &quot;Vdr&quot;, &quot;Tnfsf11&quot;, 
  &quot;Spry1&quot;, &quot;Rbpj&quot;, &quot;Ptprs&quot;, &quot;Nrp1&quot;, &quot;Il5&quot;, &quot;Il4&quot;, &quot;Il13&quot;, &quot;Igfbp7&quot;, 
  &quot;Ifng&quot;, &quot;Furin&quot;, &quot;Ern1&quot;, &quot;Vim&quot;, &quot;Tiam1&quot;, &quot;Podnl1&quot;, &quot;Penk&quot;, &quot;Maf&quot;, 
  &quot;Lmna&quot;, &quot;Il2ra&quot;, &quot;Il1rl1&quot;, &quot;Il10&quot;, &quot;Id2&quot;, &quot;Ehd1&quot;, &quot;Csda&quot;, &quot;Cd44&quot;, 
  &quot;Ccr2&quot;, &quot;Capg&quot;, &quot;Calca&quot;, &quot;Bcl2l1&quot;, &quot;Areg&quot;, &quot;Adam8&quot;, &quot;S100a6&quot;, 
  &quot;S100a4&quot;, &quot;Rora&quot;, &quot;Mki67&quot;, &quot;Lgals1&quot;, &quot;Il2rb&quot;, &quot;Hist1h1b&quot;, &quot;Anxa2&quot;, 
  &quot;Itgb1&quot;, &quot;Esm1&quot;, &quot;S100a10&quot;), class = &quot;factor&quot;), Var2 = 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, 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, 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, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 
  4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
  4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
  4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
  4L, 4L, 4L), value = c(-0.716873478395867, -0.919984230086674, 
  -0.661176149944064, -0.828193431806995, -0.990890861695874, -0.985855800232094, 
  -0.823026349552051, -0.608384425062877, -0.809200195330501, -0.621395373041386, 
  -0.998621039674651, -0.795949946272161, -1.10479976781604, -0.499510689498092, 
  -0.680578082389661, -0.850155027403529, -1.02359795075789, -0.930715054575906, 
  -0.900246349411412, -1.12696248565925, -0.531673076619624, -0.634387083227473, 
  -0.567814470617106, -0.704971739502355, -0.77107142771167, -1.0577165116177, 
  -1.12689193292486, -1.07046845292562, -1.21970205863578, -1.06946734340346, 
  -0.697966304450698, -1.31865479289137, -1.1644549788312, -0.724572318411187, 
  -0.887650620239559, -0.878042971979847, -0.898835722780145, -0.966547257168572, 
  -1.44089646981222, -1.17641689690512, -0.981496097411318, -0.949054521097622, 
  -0.86984148513583, -1.03997550896008, -0.776245072529661, -0.926869652178528, 
  -1.22014851883998, -1.11565223232092, -1.45758806540816, -1.30893449397075, 
  -1.38003602497654, -1.224949334573, -1.38357974363453, -1.35339938422864, 
  -0.968416032914201, -0.65165652813915, -1.4919639415255, -0.685400251755411, 
  -0.802640661038347, -0.575837610054767, -0.629465888711167, -0.317915043043598, 
  -0.559729515180935, -0.422625579063628, -0.503276458166707, -0.565387197269664, 
  -0.414441446968764, -0.652570443296679, -0.699718481126342, -0.587483551485666, 
  -0.479784066762678, -0.474739880805875, -0.163485068386743, -0.0319151401130408, 
  -0.687844415173434, -0.793272191522238, -0.192431267926848, -0.478440062610096, 
  -0.406493550612629, -0.516470014019787, -0.516293205774877, -0.0703527612407989, 
  -0.321528054118583, -0.417459180983822, -0.410868262057476, -0.333210832661548, 
  -0.61740991961346, -0.605534106803515, -0.228283398359129, -0.490846073129063, 
  -0.588893105208149, -0.691611619542831, -0.768211373139653, -0.655714376336414, 
  -0.691777405043065, 0.27283725214197, -0.46762324186072, -0.663659775386651, 
  -0.595353641454884, -0.813248687573964, -0.672352278644248, -0.649282500217443, 
  -0.722482130363645, -0.325408266036161, -0.49770614745867, 0.177675109919899, 
  0.206312208622598, -0.0834329896761757, -0.400018473948618, 0.627959745919098, 
  0.639468920429142, 0.881985828293655, 1.4894632640724, 0.564376769280816, 
  -0.0175886218831413, 0.748001124892129, -0.237693188616826, 0.0748099685432856, 
  -0.0738067160244709, 0.262707416774023, -0.204274564295389, -0.381902355833725, 
  -0.047820862385917, -0.458203252153174, 0.498723523113106, 0.13206419664028, 
  0.811723606016846, 1.49979274596739, 1.48431659019814, 1.43869340350301, 
  1.36267422579482, 1.2280707677281, 1.09802917518633, 1.30099479431717, 
  1.49960551175132, 1.4927056563283, 1.49686976804331, 1.47397253453575, 
  1.43227082074503, 1.3325444408283, 1.1983987520954, 0.198402134411653, 
  1.08912853827601, 1.00509721706754, -0.153425140424883, 0.852612722881043, 
  0.698995996122549, -0.138755915285969, 0.296318265086462, 0.446421081491043, 
  0.251052659372192, 0.50342451499526, 0.873163189823561, 0.6704650702465, 
  0.474043909647662, 0.24672752839951, 1.12930018666821, 0.869584307302813, 
  0.0200115056815992, 0.46498720236421, 0.447990671026386, 0.485629347319394, 
  0.528646313252754, -0.0127091863060346, 0.637743076608102, 0.910524864813588, 
  -0.0726857435537057, 0.859230754317892, 0.843382686575065, -0.364665784858882, 
  0.351291400617603, 1.41986247931586, 0.974623726851428, 1.47470660621924, 
  1.38284919037663, 1.38261262076394, 1.28287812909918, 1.4499266152937, 
  1.493563316646, 1.42240835251479, 1.49404009657185, 1.15246823659118, 
  1.36360436969755, 0.880559849096722, -0.520497871814678, -0.328998889961762, 
  -0.425053223809833, -0.307160868594853, 0.390488791334733, 0.595489070523626, 
  0.0183991764982231, -0.489492083871666, -0.451824932137866, -0.412585283406412, 
  -0.252707589258521, -0.5908467120367, 0.0467001829831055, 0.345952361813286, 
  1.28293458057144, 0.463784556661154, 0.681779946448773, 1.45692561131772, 
  0.69432543075946, 0.956305055837712, 1.45222131165832, 1.28294398902894, 
  1.19983334411133, 1.30349742116338, 1.15489996934721, 0.294895869208661, 
  0.973575068519344, 1.17111204440007, 1.297680661103, 0.553790085627175, 
  0.842743233449211, 1.40551605194952, 1.184364466613, 1.09756610180677, 
  1.12772904683868, 0.751266682827816, 1.11533140667713, 0.825725901866236, 
  0.714442943708028, 0.828305803894246, -0.145300290518389, -0.756952453676937, 
  -0.473140866248911, 0.576295750132076)), class = &quot;data.frame&quot;, row.names = c(NA, 
  -228L))
  
  </code></pre>
  <p>This is what I tried:</p>
  <pre><code>legend &lt;- cowplot::get_legend(zmapp024)
  
  grid.newpage()
  grid.draw(legend)
  
  </code></pre>