thomasp85 / patchwork

The Composer of ggplots
https://patchwork.data-imaginist.com
Other
2.45k stars 160 forks source link

The x_axis text margin behavior when scale_x_discrete(position = "top") #202

Closed abiyug closed 1 week ago

abiyug commented 4 years ago

When patching parallel ggplot plots with the x axis text position set to the 'top', the text jumps up. This behavior doesn't happen when position is set to the "bottom".

Here is the Reprex:

library(tidyverse)
library(tidyr)
library(patchwork)

df <- mtcars %>% 
  select(mpg:carb) %>% 
  scale() %>%           
  as.data.frame() %>%
  cbind(rown = rownames(mtcars)) %>% 
  pivot_longer(mpg:carb, names_to = "car_model", 
                      values_to = "value")

gg_heat <-
ggplot(df, aes(y = rown, x = car_model, fill = value)) +
        geom_tile(color = "white", size = .6) + 
        scale_fill_gradient(low = "white",high = "red") +
        scale_x_discrete(position = "top") +
        theme_bw()  +
        geom_text(aes(label = round(value, 2)), 
                     colour = "black",
                     size = 3,
                     fontface = "bold")
gg_line <-
ggplot(mtcars, aes(wt, mpg)) + 
  geom_line() + 
  facet_wrap(~ am)

gg_line | gg_heat

patchwork_git

abiyug commented 4 years ago

Any update?

thomasp85 commented 4 years ago

this is because you are merging it with a faceted plot. axes are aligned between plots which means that if a plot has facet strips in between it will move up the axes of non-faceted plots

abiyug commented 4 years ago

I see. So is this as designed or will you look into put a fix for merging fcated cases?

thomasp85 commented 4 years ago

It is unlikely to change, though I will not rule it out completely. When it comes to these edge cases patchwork will always err to the side of simplicity and consistency rather than complicating the API

abiyug commented 4 years ago

Don't rule it out. Thanks for responding.

thomasp85 commented 1 week ago

This can now be fixed with the new capabilities of free()

gg_line | free(gg_heat, "label", "t")

abiyug commented 1 week ago

Better late than never🙂 I suspect the fix is on dev version, right?

Thanks Thomas for all that you've done n the #rstats community.

Nostalgic of the days when #rstats had its best on display on Twitter.

Much respect ...

-A

On Monday, September 2, 2024, Thomas Lin Pedersen @.***> wrote:

This can now be fixed with the new capabilities of free()

gg_line | free(gg_heat, "label", "t")

https://camo.githubusercontent.com/b31b263c6418d1fd94e6fac2eba347b92b5f884b0217efe78c118bbfa367f237/68747470733a2f2f692e696d6775722e636f6d2f31486a76486b522e706e67

— Reply to this email directly, view it on GitHub https://github.com/thomasp85/patchwork/issues/202#issuecomment-2324997385, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADBQC6X3KJLRC3ABB3ZSIULZUSASFAVCNFSM6AAAAABNQRYYKWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMRUHE4TOMZYGU . You are receiving this because you authored the thread.Message ID: @.***>

-- From mobile phone email