In the following RMarkdown, I would like to have each barplot print so that the text size and box width are the same in each plot, and the height adjusts accordingly.
What is happening is that in the plots with few bars the bars are extremely wide, and in the plots with many bars the bars are thin and the labels squished.
---
title: "chunk plot height"
number_sections: yes
output: html_notebook
---
# Load Libraries
\```{r}
library(tidyverse)
library(magrittr)
library(rmarkdown)
\```
# Set Knitr Options
\```{r}
knitr::opts_chunk$set(
echo=TRUE,
dpi=300,
fig.width=12
)
\```
# Plot
\```{r}
for (n in seq(10,50,10))
{
knitr::opts_chunk$set(out.height=n)
data = data.frame(
X=sapply(c(letters, LETTERS)[1:n], function(x) rep(x,3) %>% paste0(collapse=''), USE.NAMES = F),
Y=rnorm(n)
)
plt =
data %>%
ggplot(aes(x=X, y=Y)) +
geom_col(position=position_dodge(width=1, preserve='single')) +
coord_flip()
print(plt)
}
\```