簡體   English   中英

將字體大小調整為繪圖設備的大小

[英]Adjust font size to size of plot device

當我必須調整輸出圖像的大小時,我經常處於一個位置。 不幸的是,這意味着通常我必須調整字體大小,以使內容可讀。

例如,如果下面的情節

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'' x 6'' 圖像看起來不錯:

p + ggsave("test_small.png", width = 12, height = 6, unit = "in")

12'' x 6'' 輸出

在此處輸入圖片說明

但是,如果我將尺寸增加到 36'' x 18'',字體將無法閱讀:

p + ggsave("test_large.png", width = 36, height = 18, unit = "in")

36 英寸 x 18 英寸

在此處輸入圖片說明

是否有任何通用策略可以讓我們更改輸出分辨率而無需不斷修改字體大小?

您需要定義文本項的大小以及繪圖環境。

由於您想要動態縮放,因此縮放字體並將大小保存為相同的值可能是最簡單的。 請參閱ggplot2 -除以 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) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM