flooose flooose - 1 year ago 130
R Question

aspect ration of ggplot2 plot

I used

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

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download