Change width and height of ggplot2 graphic?

I am trying to change the "height" and "width" of my plot and while I have changed the plot margins I would like to change the background to be proportionate with my plot. This is what my current plot looks like:

And I want it to look more like this:

I've added the background to so it's easier to see what I mean. Ultimatley, I just want my plot to be wider than it is tall, and I would like if it keeps it's proportion when printed to any device.

Here is a small reprex, not really the plot I'm trying to change:

library(tidyverse)

tibble(x = c("F1", "F2", "F3"),
       y = rexp(3)) %>% 
  ggplot(aes(x = x, y = 10, size = y)) + 
  geom_point() +
  guides(size = FALSE) + 
  theme(plot.margin = margin(2,.8,2,.8, "cm"),
        plot.background = element_rect(fill = "darkgrey"))
2 Likes

The size of the plot is dependent on the size of the window (in RStudio) or whatever you set it as if you are exporting it.

ggsave(g, height = ..., width = ...)

If you want to keep a constant aspect ratio...

aspect_ratio <- 2.5
height <- 7
ggsave(g, height = 7 , width = 7 * aspect_ratio)
3 Likes