当我必须调整输出图像的大小时,我常常处于某个位置。不幸的是,这意味着通常我必须调整字体大小,以使事物可读。
例如,如果以下情节
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))
被保存为12'''6''图像看起来很不错:
p + ggsave("test_small.png", width = 12, height = 6, unit = "in")
但是,如果我将尺寸增加到36英寸x 18英寸字体是不可读的:
p + ggsave("test_large.png", width = 36, height = 18, unit = "in")
是否有任何一般策略允许我们在不经常修改字体大小的情况下更改输出分辨率?
您需要定义文本项的大小以及绘图环境。
由于您希望动态扩展,因此最简单的方法是缩放字体并将大小保存为相同的值。请参阅ggplot2 - The unit of size得到除以2.834646值以校正字体大小。
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)