Get the same height for parcels with different facet numbers and with coordinates?

Let me explain in the pictures what I mean:

set.seed(1)  ## dummy data.frame:
df <- data.frame( value1 = sample(5:15, 20, replace = T), value2 = sample(5:15, 20, replace = T),
                  var1 = c(rep('type1',10), rep('type2',10)), var2 = c('a','b','c','d'))

## Plot 1 

ggplot() +
  geom_point(data = df, aes(value1, value2)) +
  facet_grid(~var1) +
  coord_fixed()

ggsave("plot_2facet.pdf", height=5, units = 'in')
    #Saving 10.3 x 5 in image

## Plot 2  which I want to save in a separate file (!)

ggplot() +
  geom_point(data = df, aes(value1, value2)) +
  facet_grid(~var2) +
  coord_fixed()

ggsave("plot_4facet.pdf", height=5, units = 'in')
    #Saving 10.3 x 5 in image

enter image description here

Now, what happens here is that the devices have the same height, but the graphs have different heights. But I would like to get the same height for the plots.

In the above code, I tried to specify only the height, but ggsave then just takes a fixed-width size for the device.

I tried theme(plot.margin = margin(t=1,b=1)), but it didn’t change anything.

The output coord_fixed()gives graphs with the same height:

enter image description here

But I would like to use coord_fixed().

Is there a solution for this, or do I need to "guess" the width of the device to get the correct height of the graph?

Greetings


Edit

Charts should ideally be created in separate devices / files.

+6
3

ggplot, , , , , , . , coord_fixed y x.

:

  • expand scale_y_continuous. y . - , , , , ..

    /li >
  • . , , , .

, ( y). , , .

, y

, ( ), , , , , , :

1)

p1 = ggplot(df1) +
  geom_point(aes(value1, value2)) +
  facet_grid(~var1) +
  coord_fixed()

p2 = ggplot(df1) +
  geom_point(aes(value1, value2)) +
  facet_grid(~var2) +
  coord_fixed() 

2) , , :

t_blank = theme(strip.background = element_rect(fill = NA),
      strip.text = element_text(color=NA),
      axis.title = element_text(color = NA),
      axis.text = element_text(color = NA),
      axis.ticks = element_line(color = NA))

p1 + geom_rect(aes(xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf), fill='black') + 
     t_blank
ggsave(fn1 <- tempfile(fileext = '.png'), height=5, units = 'in')

p2 + geom_rect(aes(xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf), fill='black') + 
     t_blank
ggsave(fn2 <- tempfile(fileext = '.png'), height=5, units = 'in')

3), ( )

library(png)
p1.saved = readPNG(fn1)[,,1]
p2.saved = readPNG(fn2)[,,1]

4) ( , value = )

p1.height = diff(row(p1.saved)[range(which(p1.saved==0))])
p2.height = diff(row(p2.saved)[range(which(p2.saved==0))])

5) , . , 1.1, , 0,05 . - . , , , .

height.expand = 1.1 - p2.height / p1.height

6) ,

ggsave("plot_2facet.pdf", p1, height=5, units = 'in')
ggsave("plot_4facet.pdf", p2 + scale_y_continuous(expand=c(height.expand, 0)), 
        height=5, units = 'in')

g3OeW.pngFu5Vl.png

,

,

p1.width = 10

, , , , .

p1 + geom_rect(aes(xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf), fill='black') + 
     t_blank
ggsave(fn1 <- tempfile(fileext = '.png'), height=5, width = p1.width, units = 'in')
p1.saved = readPNG(fn1, info = T)[,,1]
p1.height = diff(row(p1.saved)[range(which(p1.saved==0))])

, , ( - , , , , , )

, uniroot, , . uniroot, , . , . if (x==0) x = -1e-8 , uniroot , , - . .

fn2 <- tempfile(fileext = '.png')
find.p2 = function(w){
  p = p2 + geom_rect(aes(xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf), fill='black') + 
           t_blank
  ggsave(fn2, p, height=5, width = w, units = 'in')
  p2.saved = readPNG(fn2, info = T)[,,1]
  p2.height = diff(row(p2.saved)[range(which(p2.saved==0))])
  x = abs(p1.height - p2.height)
  if (x==0) x = -1e-8
  x
}

N1 = length(unique(df$var1)) 
N2 = length(unique(df$var2)) 
p2.width = uniroot(find.p2, c(p1.width, p1.width*N2/N1))

, .

p1
ggsave("plot_2facet.pdf", height=5, width = p1.width, units = 'in')
p2
ggsave("plot_4facet.pdf", height=5, width = p2.width$root, units = 'in')

14GDQ.pngm19We.png

+6

plot_layout patchwork.

library(patchwork)
library(ggplot2)

p1 <- ggplot() +
 geom_point(data = df, aes(value1, value2)) +
 facet_grid( ~ var1) +
 coord_fixed()

p2 <- ggplot() +
 geom_point(data = df, aes(value1, value2)) +
 facet_grid( ~ var2) +
 coord_fixed()

p1 + p2 + plot_layout(nrow = 1, widths = c(1, 2))

enter image description here

widths , .


: fooobar.com/questions/43420/...,

library(grid)
grid.draw(cbind(ggplotGrob(p1), ggplotGrob(p2), size = "first"))

, .

your_height <- 4
your_width <- 7

ggplot() +
 geom_point(data = df, aes(value1, value2)) +
 facet_grid(~var1) +
 coord_fixed()

ggsave("p1.pdf", path = "/path/to/file/", width = your_width / 2, height = your_height)

ggplot() +
 geom_point(data = df, aes(value1, value2)) +
 facet_grid(~var2) +
 coord_fixed()

ggsave("p2.pdf", path = "/path/to/file/", width = your_width, height = your_height)
+3

() egg. , , , ; , ggarrange . - , !

library(egg)
getScale <- ggarrange(p1, p2, draw = F, ncol=2)
p1_sc <- ggarrange(p1, heights = getScale$heights[2])
ggsave("plot_2facet.pdf", plot=p1_sc, height=5, units = 'in')

p2_sc <- ggarrange(p2, heights = getScale$heights[2])
ggsave("plot_4facet.pdf", plot=p2_sc, height=5, units = 'in')

, , :

getScale$heights[2]
# [1] max(1*1null, 1*1null)
class(getScale$heights[2])
# [1] "unit.list" "unit"   

.. ,

p3 <- ggplot() +
    geom_point(data = df, aes(value1, value2)) +
    facet_wrap(~var2, nrow=2) +
    coord_fixed()

getScale <- ggarrange(p1, p2, p3, draw = F, ncol=3)

p1_sc <- ggarrange(p1, heights = getScale$heights[2])
ggsave("plot_2facet.pdf", plot=p1_sc, height=5, units = 'in')

p2_sc <- ggarrange(p2, heights = getScale$heights[2])
ggsave("plot_4facet.pdf", plot=p2_sc, height=5, units = 'in')

p3_sc <- ggarrange(p3, heights = getScale$heights[2])
ggsave("plot_4facet_2row.pdf", plot=p3_sc, height=5, units = 'in')
0

All Articles