patchwork icon indicating copy to clipboard operation
patchwork copied to clipboard

Could a top legend and top colourbar align better?

Open davidhodge931 opened this issue 1 year ago • 1 comments

Hi, awesome package! I was wondering if a top legend and top colourbar could align a bit better?

library(tidyverse)
library(palmerpenguins)
library(patchwork)
#> Warning: package 'patchwork' was built under R version 4.4.1
  
p1 <- penguins |> 
  ggplot() +
  geom_histogram(aes(x = flipper_length_mm, 
                     fill = species)) +
  theme(legend.position = "top") +
  theme(legend.title.position = "top")


p2 <- penguins |> 
  ggplot() +
  geom_point(aes(x = flipper_length_mm, 
                 y = body_mass_g,
                 colour = bill_length_mm)) +
  theme(legend.position = "top") +
  theme(legend.title.position = "top")

p1 + p2
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> Warning: Removed 2 rows containing non-finite outside the scale range
#> (`stat_bin()`).
#> Warning: Removed 2 rows containing missing values or values outside the scale range
#> (`geom_point()`).

Created on 2024-11-20 with reprex v2.1.1

davidhodge931 avatar Nov 19 '24 20:11 davidhodge931

Perhaps you want legend.justification="top"?

library(tidyverse)
library(palmerpenguins)
library(patchwork)

p1 <- penguins |>
  ggplot() +
  geom_histogram(aes(
    x = flipper_length_mm,
    fill = species
  )) +
  theme(legend.position = "top") +
  theme(legend.title.position = "top")


p2 <- penguins |>
  ggplot() +
  geom_point(aes(
    x = flipper_length_mm,
    y = body_mass_g,
    colour = bill_length_mm
  )) +
  theme(legend.position = "top") +
  theme(legend.title.position = "top")

p1 + p2 &
  theme(
    legend.justification = "top"
  )
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> Warning: Removed 2 rows containing non-finite outside the scale range
#> (`stat_bin()`).
#> Warning: Removed 2 rows containing missing values or values outside the scale range
#> (`geom_point()`).

trekonom avatar Mar 22 '25 07:03 trekonom