I have some ggplot2
plots with lots of text in them (no way around that), that I am building inside an *.Rmd
, like so:
---
title: "SO Test"
output: pdf_document
---
```{r}
library(ggplot2)
df <- data.frame(rep(1:5,5),rep(1:5, each=5),rep("Test",5))
colnames(df) <- c("x", "y", "word")
ggplot(data = df, mapping = aes(x = x, y = y, label = word)) + geom_text()
```
That works great, giving me something like the below as a PDF.
I would now like to make these labels (Test
) as large as possible without overplotting.
My problem is that I now see 2 "levers" which I could pull to change the font size:
- "inside"
ggplot2
, change, add, say,geom_text(size=12)
- upon plotting, "inside"
knitr
(orRmarkdown
?), add a chunk option like `fig.width=15, fig.height=15), which also changes the font size relative to the overall plot size (for reasons I don't really understand).
Obviously, 1. would be the straightforward way, but I then need to adjust geom_text(size=...)
always depending on the size of the output, say, when the linewidth
in LaTeX changes or the column width of my blog post.
That seems inelegant, to say the least.
How do I go about this in an elegant, scalable way that produces a pretty plot?
I'm still a bit fuzzy about this, and where in the toolchain (Rmarkdown
?, Knitr
?, GGplot2
) I should start ...
Ps.: as a bonus, the font size should be arrived at programmatically, because the length of string Text
varies from plot to plot in real life.
{r, fig.height=x, fig.width=y}
where x and y were created in a previous chunk, maybe getting the size of blog page or using the data frame dimensions. would that work? Also, I'm not sure I understand how the size of the ticks and text labels are directly affected by the fig.height/fig.width. Seems like you should be messing with the element size orgeom_text(size=)
– rawr