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')


,
,
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')

