wilkelab / cowplot

cowplot: Streamlined Plot Theme and Plot Annotations for ggplot2
https://wilkelab.org/cowplot/
704 stars 84 forks source link

Use aspect ratio to fill in missing width or height #59

Closed kmanalo closed 7 years ago

kmanalo commented 7 years ago

I was surprised to find that I needed to calculate base_height if I provided the pair base_aspect_ratio and base_width.

Here is the example I worked with without using a base_aspect_ratio with explicit height and width:

save_plot(label, p, base_height = 6 / 1.618 , base_width = 6)

Instead I wanted to be able to do this:

save_plot(label, p, base_aspect_ratio = 1.618 , base_width = 6)

Finally, I settled on the below, as the presence of NULL for the missing value is necessary, so as not to disrupt the default options logic:

save_plot(label, p, base_aspect_ratio = 1.618 , base_height = NULL, base_width = 6)

The code logic is set up to be symmetric, so that if two are known the missing one is calculated, so this case should also work:

save_plot(label, p, base_aspect_ratio = 1.618 , base_height = 6, base_width = NULL)

Like above, I have provided two examples in the documentation:

# same as p3 but determine base_height given base_aspect_ratio and base_width
p4 <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) + geom_point(size=2.5)
save_plot("p4.png", p4, base_height = NULL, base_aspect_ratio = 1.618, base_width = 6)

# same as p4 but determine base_width given base_aspect_ratio and base_height
p5 <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) + geom_point(size=2.5)
save_plot("p5.png", p5, base_height = 6, base_aspect_ratio = 1.618, base_width = NULL)

Roxygen regenerated documentation.