In r notebooks or markdown documents, if one specifies fig.width
or fig.height
for generating an image with ggplot
, the size of the box containing the actual data is heavily influenced by the size of the margins and their elements (labels, legends etc.). So for example, if one rotates the x-axis-labels, the height of the box becomes smaller (see image), if one adds a legend, the width gets smaller.
My goal is to have the boxes containing the data in different figures the same size. How would one achieve that? The code for the two images can be found further down.
MWE for the sample images.
---
title: "R Notebook"
output:
pdf_document: default
html_notebook: default
---
library(ggplot2)
data(iris)
ggplot(iris, aes(Sepal.Length)) +
geom_bar() +
theme_linedraw()
library(ggplot2)
data(iris)
ggplot(iris, aes(Species, fill=Species)) +
geom_bar(position = position_dodge2(preserve = "single")) +
theme_linedraw() +
theme(axis.text.x = element_text(angle=45, vjust=0.5))