0

I used coord_fixed to change the aspect ratio of my plot. Unfortunately, as you can see below, the size of the window of the plot is still square, resulting in large amounts of white space above and below my plot. How do I change the size of the window to, say "wrap the content" that's inside of it?example plot

flooose
  • 499
  • 8
  • 25
  • 2
    Open a plotting device with the required dimensions. Usually you should simply use ggsave and specify height and width. – Roland Oct 01 '16 at 09:49
  • Could you show me a link, or some code explaining how to do this programmatically? I need to embed the image in a report generated with rmarkdown and in the html output the white space above and below the image is also there and it interrupts the flow of the report. – flooose Oct 01 '16 at 12:35
  • Wait, I think this is the relevant info/man page for what you're saying https://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/Devices.html. I'll take a look at that and report back. – flooose Oct 01 '16 at 12:39
  • If you use rmarkdown you should specify the dimensions within it. You should always provide all relevant context (usually as a minimal reproducible example) in a question. – Roland Oct 01 '16 at 12:45

1 Answers1

1

Ok, with the help of Roland, I've figured it out. ggplot2 plots are always displayed in a device and that's where the height has to be set. For instance, I'm on linux and if I want to display a plot on screen, it gets done with x11(). In the R console, simply doing

> x11(width = 7, height = 3.5)

will result in a window with a dimension of 7 inches by 3.5 inches. Subsequent calls to ggplot2 will automatically use this window. The standard size is 7 inches by 7 inches and in my case resulted in the large areas of white space above and below the plot, which of course are smaller when the window has a height of 3.5 inches.

The next step, was getting the height right when rmarkdown uses the png device. This is actually a knitr issue and can be specified in the chunk options of the code block where the plot gets generated.

Here's the relevant code for my case

```{R, fig.width = 7, fig.height = 3.5}
# ggplot2 code goes here
```
flooose
  • 499
  • 8
  • 25