Often I am in a position when I have to adjust the size of the output image. Unfortunately, it means that usually I have to adjust font size, to make things readable.
For example, if the following plot
library(ggplot2)
library(tibble)
library(stringi)
set.seed(1)
df <- tibble(
x = stri_rand_strings(10, 20),
y = runif(10) * 10,
label = stri_rand_strings(10, 10)
)
p <- ggplot(df, aes(x, y)) +
geom_text(aes(label = label)) +
scale_x_discrete(position = "top") +
theme(axis.text.x = element_text(angle = 90, hjust = 0))
is saved to 12'' x 6'' image things look pretty good:
p + ggsave("test_small.png", width = 12, height = 6, unit = "in")
12'' x 6'' output
however if I increase dimensions to 36'' x 18'' the fonts are unreadble:
p + ggsave("test_large.png", width = 36, height = 18, unit = "in")
36'' x 18''
Is there any general strategy which allows us to change output resolution without constant tinkering with the font sizes?
You need to define the size of your text items as well as the plotting environment.
Since you want to dynamically scale it will likely be easiest to scale your fonts and save sizes to the same values. See ggplot2 - The unit of size for the divide by 2.834646 value to correct for font sizes.
base = 6 # set the height of your figure (and font)
expand = 2 # font size increase (arbitrarily set at 2 for the moment)
ggplot(df, aes(x, y)) +
geom_text(aes(label = label), size = base * expand / 2.834646) +
scale_x_discrete(position = "top") +
theme(axis.text.x = element_text(angle = 90, hjust = 0, size = base * expand ),
axis.text.y = element_text(size = base * expand ))
ggsave("test_large.png", width = base * 2, height = base, unit = "in", dpi = 300)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With