0

How does one change the size of the points only in a ggplot2 legend? I want them to remain the size I have in the plot code, but to appear bigger in the legend.

My plot code is here:

ggplot(df, aes(x=log(length), y=log(wt), shape=Depth,fill=Depth,color=Depth, linetype=Depth)) +
  geom_point(aes(),size=2,alpha=1,stroke=1.3) + 
  geom_smooth(method=lm, se=FALSE, fullrange=FALSE) +
  facet_wrap(~ LakeYear, nrow=3, ncol=2) +
  labs(x="ln Length (mm)", y="ln Weight (mg)") +
  scale_fill_manual(values=cbPalette2,name="Depth")+
  scale_color_manual(values=cbPalette,name="Depth") +
  scale_shape_manual(values=c(16,15,21,18),name="Depth")+
  scale_linetype_manual(values =c("<5 m"="solid", "10-15 m"="solid", "15-20 m"="solid",">20 m"="dashed"),name="Depth")+
  PlotTheme + 
  theme(panel.spacing.x=unit(1.4, "lines"),panel.spacing.y=unit(1, "lines"),legend.title=element_text(size=16))

I have tried to add + guides(color = guide_legend(override.aes = list(size=3))) and + guides(shape = guide_legend(override.aes = list(size = 2))) but this just changes the line size. I have tried some code I found from an example here

# Make JUST the legend points larger without changing the size of the legend lines:
# To get a list of the names of all the grobs in the ggplot
g
grid::grid.ls(grid::grid.force())

# Set the size of the point in the legend to 2 mm
grid::grid.gedit("key-[-0-9]-1-1", size = unit(4, "mm"))

but I just get the error: "In editThisGrob(child, specs) : slot 'size' not found" so I'm assuming the key is wrong, but I can't figure out which from the list of all the grobs to use here. the list is here:

 grid::grid.ls(grid::grid.force())
layout
  background.1-15-23-1
    plot.background..rect.6707
  panel-1-1.8-5-8-5
    panel-1.gTree.6397
      grill.gTree.6395
        panel.background..rect.6386
        panel.grid.minor.y..polyline.6388
        panel.grid.minor.x..polyline.6390
        panel.grid.major.y..polyline.6392
        panel.grid.major.x..polyline.6394
      NULL
      geom_point.points.6337
      geom_smooth.gTree.6353
        GRID.polyline.6348
        GRID.polyline.6349
        GRID.polyline.6350
        GRID.polyline.6351
      NULL
      panel.border..rect.6383
  panel-2-1.13-5-13-5
    panel-3.gTree.6429
      grill.gTree.6427
        panel.background..rect.6418
        panel.grid.minor.y..polyline.6420
        panel.grid.minor.x..polyline.6422
        panel.grid.major.y..polyline.6424
        panel.grid.major.x..polyline.6426
      NULL
      geom_point.points.6341
      geom_smooth.gTree.6365
        GRID.polyline.6360
        GRID.polyline.6361
        GRID.polyline.6362
        GRID.polyline.6363
      NULL
      panel.border..rect.6415
  panel-1-2.18-5-18-5
    panel-5.gTree.6461
      grill.gTree.6459
        panel.background..rect.6450
        panel.grid.minor.y..polyline.6452
        panel.grid.minor.x..polyline.6454
        panel.grid.major.y..polyline.6456
        panel.grid.major.x..polyline.6458
      NULL
      geom_point.points.6345
      geom_smooth.gTree.6376
        GRID.polyline.6372
        GRID.polyline.6373
        GRID.polyline.6374
      NULL
      panel.border..rect.6447
  panel-2-2.8-9-8-9
    panel-2.gTree.6413
      grill.gTree.6411
        panel.background..rect.6402
        panel.grid.minor.y..polyline.6404
        panel.grid.minor.x..polyline.6406
        panel.grid.major.y..polyline.6408
        panel.grid.major.x..polyline.6410
      NULL
      geom_point.points.6339
      geom_smooth.gTree.6359
        GRID.polyline.6354
        GRID.polyline.6355
        GRID.polyline.6356
        GRID.polyline.6357
      NULL
      panel.border..rect.6399
  panel-1-3.13-9-13-9
    panel-4.gTree.6445
      grill.gTree.6443
        panel.background..rect.6434
        panel.grid.minor.y..polyline.6436
        panel.grid.minor.x..polyline.6438
        panel.grid.major.y..polyline.6440
        panel.grid.major.x..polyline.6442
      NULL
      geom_point.points.6343
      geom_smooth.gTree.6371
        GRID.polyline.6366
        GRID.polyline.6367
        GRID.polyline.6368
        GRID.polyline.6369
      NULL
      panel.border..rect.6431
  panel-2-3.18-9-18-9
    panel-6.gTree.6477
      grill.gTree.6475
        panel.background..rect.6466
        panel.grid.minor.y..polyline.6468
        panel.grid.minor.x..polyline.6470
        panel.grid.major.y..polyline.6472
        panel.grid.major.x..polyline.6474
      NULL
      geom_point.points.6347
      geom_smooth.gTree.6381
        GRID.polyline.6377
        GRID.polyline.6378
        GRID.polyline.6379
      NULL
      panel.border..rect.6463
  strip-t-1-3.17-5-17-5
    strip
      strip.1-1-1-1
        strip.gTree.6555
          strip.background.x..rect.6545
          strip.text.x.top..titleGrob.6540
            GRID.text.6538
  strip-t-2-3.17-9-17-9
    strip
      strip.1-1-1-1
        strip.gTree.6557
          strip.background.x..rect.6545
          strip.text.x.top..titleGrob.6543
            GRID.text.6541
  strip-t-1-2.12-5-12-5
    strip
      strip.1-1-1-1
        strip.gTree.6551
          strip.background.x..rect.6545
          strip.text.x.top..titleGrob.6534
            GRID.text.6532
  strip-t-2-2.12-9-12-9
    strip
      strip.1-1-1-1
        strip.gTree.6553
          strip.background.x..rect.6545
          strip.text.x.top..titleGrob.6537
            GRID.text.6535
  strip-t-1-1.7-5-7-5
    strip
      strip.1-1-1-1
        strip.gTree.6547
          strip.background.x..rect.6545
          strip.text.x.top..titleGrob.6528
            GRID.text.6526
  strip-t-2-1.7-9-7-9
    strip
      strip.1-1-1-1
        strip.gTree.6549
          strip.background.x..rect.6545
          strip.text.x.top..titleGrob.6531
            GRID.text.6529
  axis-t-1-3.16-5-16-5
    NULL
  axis-t-2-3.16-9-16-9
    NULL
  axis-t-1-2.11-5-11-5
    NULL
  axis-t-2-2.11-9-11-9
    NULL
  axis-t-1-1.6-5-6-5
    NULL
  axis-t-2-1.6-9-6-9
    NULL
  axis-b-1-3.19-5-19-5
    GRID.absoluteGrob.6481
      NULL
      axis
        axis.1-1-1-1
          GRID.polyline.6480
        axis.2-1-2-1
          GRID.titleGrob.6479
            GRID.text.6478
  axis-b-2-3.19-9-19-9
    GRID.absoluteGrob.6481
      NULL
      axis
        axis.1-1-1-1
          GRID.polyline.6480
        axis.2-1-2-1
          GRID.titleGrob.6479
            GRID.text.6478
  axis-b-1-2.14-5-14-5
    NULL
  axis-b-2-2.14-9-14-9
    NULL
  axis-b-1-1.9-5-9-5
    NULL
  axis-b-2-1.9-9-9-9
    NULL
  axis-l-1-2.8-8-8-8
    NULL
  axis-l-2-2.13-8-13-8
    NULL
  axis-l-3-2.18-8-18-8
    NULL
  axis-l-1-1.8-4-8-4
    GRID.absoluteGrob.6505
      NULL
      axis
        axis.1-1-1-1
          GRID.titleGrob.6503
            GRID.text.6502
        axis.1-2-1-2
          GRID.polyline.6504
  axis-l-2-1.13-4-13-4
    GRID.absoluteGrob.6505
      NULL
      axis
        axis.1-1-1-1
          GRID.titleGrob.6503
            GRID.text.6502
        axis.1-2-1-2
          GRID.polyline.6504
  axis-l-3-1.18-4-18-4
    GRID.absoluteGrob.6505
      NULL
      axis
        axis.1-1-1-1
          GRID.titleGrob.6503
            GRID.text.6502
        axis.1-2-1-2
          GRID.polyline.6504
  axis-r-1-2.8-10-8-10
    NULL
  axis-r-2-2.13-10-13-10
    NULL
  axis-r-3-2.18-10-18-10
    NULL
  axis-r-1-1.8-6-8-6
    NULL
  axis-r-2-1.13-6-13-6
    NULL
  axis-r-3-1.18-6-18-6
    NULL
  xlab-t.5-9-5-5
    NULL
  xlab-b.20-9-20-5
    axis.title.x.bottom..titleGrob.6656
      GRID.text.6654
  ylab-l.8-3-18-3
    axis.title.y.left..titleGrob.6659
      GRID.text.6657
  ylab-r.8-11-18-11
    NULL
  guide-box.8-13-18-13
    guide-box
      legend.box.background.2-4-4-2
        NULL
      guides.3-3-3-3
        layout
          background.1-6-8-1
            legend.background..rect.6696
          title.2-5-2-2
            GRID.gTree.6697
              guide.title.titleGrob.6662
                GRID.text.6660
          key-3-1-bg.4-2-4-2
            legend.key..rect.6676
          key-3-1-1.4-2-4-2
            GRID.points.6677
          key-3-1-2.4-2-4-2
            GRID.gTree.6678
              GRID.segments.6679
          key-4-1-bg.5-2-5-2
            legend.key..rect.6681
          key-4-1-1.5-2-5-2
            GRID.points.6682
          key-4-1-2.5-2-5-2
            GRID.gTree.6683
              GRID.segments.6684
          key-5-1-bg.6-2-6-2
            legend.key..rect.6686
          key-5-1-1.6-2-6-2
            GRID.points.6687
          key-5-1-2.6-2-6-2
            GRID.gTree.6688
              GRID.segments.6689
          key-6-1-bg.7-2-7-2
            legend.key..rect.6691
          key-6-1-1.7-2-7-2
            GRID.points.6692
          key-6-1-2.7-2-7-2
            GRID.gTree.6693
              GRID.segments.6694
          label-3-3.4-4-4-4
            GRID.gTree.6698
              guide.label.titleGrob.6665
                GRID.text.6663
          label-4-3.5-4-5-4
            GRID.gTree.6699
              guide.label.titleGrob.6668
                GRID.text.6666
          label-5-3.6-4-6-4
            GRID.gTree.6700
              guide.label.titleGrob.6671
                GRID.text.6669
          label-6-3.7-4-7-4
            GRID.gTree.6701
              guide.label.titleGrob.6674
                GRID.text.6672
  subtitle.4-9-4-5
    plot.subtitle..zeroGrob.6703
  title.3-9-3-5
    plot.title..zeroGrob.6702
  caption.21-9-21-5
    plot.caption..zeroGrob.6705
  tag.2-2-2-2
    plot.tag..zeroGrob.6704

Reproducible data here:

df<-structure(list(length = c(11, 12, 11, 11.5, 10.5, 13, 14.5, 
                                14, 13.5, 14.5, 16, 17, 16, 15, 16.5, 19.5, 17, 18, 18, 18, 21, 
                                22, 22, 21, 25, 21.5, 12, 11, 11, 11, 12, 14, 14, 13, 15, 15, 
                                17, 17, 18, 17, 17, 15, 18, 19, 18, 18, 18, 20, 20, 21.5, 21, 
                                20.5, 19, 10, 10, 10, 11, 10, 14, 14, 14, 12, 12, 16, 15, 15, 
                                16, 14.5, 17, 19, 17.5, 18, 16, 21, 20), wt = c(29.93, 
                                                                                      36.62, 33.43, 26.19, 29.67, 47.1, 65.19, 53.49, 44.99, 73.23, 
                                                                                      77.9, 84.43, 86.32, 92.27, 103.75, 169.24, 101.62, 90.84, 148.23, 
                                                                                      104.87, 181.57, 206.07, 260.59, 218.19, 342.74, 142.64, 40.92, 
                                                                                      29.82, 26.09, 30.9, 41.24, 48.46, 65.48, 48.97, 70.79, 72.81, 
                                                                                      87.81, 82.07, 118.5, 83.5, 73.91, 73.04, 129.38, 117.68, 117.14, 
                                                                                      110.19, 91.55, 126.26, 131.6, 200.63, 179.75, 169.95, 107.1, 
                                                                                      32.99, 24.79, 27.12, 30.12, 17.89, 48.93, 53.61, 48.38, 35.52, 
                                                                                      34.6, 85.03, 61.42, 54.22, 71.06, 71.91, 86.84, 85.96, 126.16, 
                                                                                      135.9, 73.39, 138.03, 207.45), Depth = structure(c(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, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
                                                                                                                                             4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("<5 m", "10-15 m", 
                                                                                                                                                                                             "15-20 m", ">20 m"), class = "factor"), LakeYear = 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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                                                                                                                                                            1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("one - 1995", 
                                                                                                                                                                                                                                                                                                                "two - 1993", "three - 1999", "four - 2015", "five - 2019", 
                                                                                                                                                                                                                                                                                                                "six - 2019"), class = "factor")), row.names = c(NA, 75L), class = "data.frame")

Any thoughts on making this work, or a simpler solution?

Edit: Not a duplicate question, I highlighted other posts and said their solutions don't work for me.

Original Plot here: enter image description here

Added either

 p+ guides(color = guide_legend(override.aes = list(size=5)))
p+ guides(shape = guide_legend(override.aes = list(size = 5)))

I get this, without the actual shape changed, only the line enter image description here

user3490557
  • 744
  • 2
  • 6
  • 9
  • 1
    Could you `dput()` a bit of your data to make it easier to reproduce your code? – Ottie Oct 19 '22 at 12:33
  • @Ottie Yes, code is added in my original question now for some data – user3490557 Oct 19 '22 at 13:21
  • It is not clear to me what exactly doesn't work given the list of linked threads to help you (additional reference: https://stackoverflow.com/questions/20415963/how-to-increase-the-size-of-points-in-legend-of-ggplot2?noredirect=1&lq=1). Please elaborate ideally demonstrating on pictures what is wrong in your opinion. I'll reopen this for now but I still think the linked threads should help – tjebo Oct 19 '22 at 13:33
  • 1
    @tjebo I added some pictures showing the original plot, and what I saw in the other references to demonstrate that only the lines are changing size in the legend, not the shapes. – user3490557 Oct 19 '22 at 14:13
  • very related https://stackoverflow.com/questions/25007324/can-ggplot2-control-point-size-and-line-size-lineweight-separately-in-one-lege?noredirect=1&lq=1 – tjebo Oct 19 '22 at 16:19

2 Answers2

0

The simplest answer seems to be making two versions of the same plot. The first is the initial plot you want with the point sizes you'd like in the main plot. The second plot is used to grab only the legend, so you increase the point sizes to where you want them to be in the legend. You then use cowplot::get_legend and cowplot::plot_grid to extract the legend and arrange the plots together as you'd like them.

FinalFigure<-cowplot::plot_grid(
  Figure_Initial+ theme(legend.position = "none"),
  cowplot::get_legend(Figure_LegendGrab),
  ncol = 2, rel_widths = c(4, 0.8))
user3490557
  • 744
  • 2
  • 6
  • 9
-1

Vanilla ggplot can get really really hacky when you have very specific ideas to modify your legend.

In those cases, it is often way easier to create a fake legend and stick it to your main plot. It's not fully automatic, but largely so, and requires (minor) fiddling with some spacing. But I think this should work for most of the graphs where such special modifications are needed - they will hopefully not be many. Having said that, if you need this type of visualisation on a large scale, maybe you want to reconsider your visualisation.

library(tidyverse)
library(patchwork)

## df <- ... your data df

## your plot, simplified
p <- ggplot(df, aes(x = log(length), y = log(wt), shape = Depth, color = Depth)) +
  geom_point() +
  geom_smooth(method = lm, se = FALSE) +
  theme(legend.position = "none")

## first get the only present levels of your data
df$Depth <- droplevels(df$Depth)
# this is just a hack to center your legend more or less compared with the main plot
# change the y in order to change the distance between the legend items
ylims <- range(log(df$wt))
ymid <- quantile(ylims, c(.4,.5))
y_pos <- seq(ymid[1], ymid[2], length = length(levels(df$Depth)))
# this is just a random parameter to define the segment length
x_len <- 1.5
df_leg <- data.frame(x = 1, y = y_pos, Depth = levels(df$Depth))


p_leg <-
  ggplot(df_leg, aes(x, y, color = Depth, shape = Depth)) +
  geom_point(size = 4) +
  geom_segment(aes(x = x_len-x, xend = x_len, yend = y)) +
  ## bit of a 
  geom_text(aes(x = x_len + x, label = Depth), hjust = 0.1) +
  ## need to clip off , use your preset y limits for correct centering, 
  coord_cartesian(ylim= ylims, clip = "off") +
  theme_void() +
  theme(legend.position = "none", 
  ## add a margin, otherwise you won't see your legend
        plot.margin = margin(r = .5, l = .1, unit = "in"))
    
p + p_leg +
  plot_layout(widths = c(1, .1))
#> `geom_smooth()` using formula 'y ~ x'

Created on 2022-10-19 by the reprex package (v2.0.1)

tjebo
  • 21,977
  • 7
  • 58
  • 94