[英]ggplot2::geom_text font as the rest of graph
我使用下面給出的代碼得到了以下圖表:
library(ggplot2)
library(ggthemes)
p <- ggplot(data = mtcars, mapping = aes(x = wt, y = mpg)) +
geom_point() +
theme_igray()
p
p + geom_text(mapping = aes(label = rownames(mtcars)))
p + geom_text(mapping = aes(label = rownames(mtcars)), family = "Times New Roman")
geom_text
的字體與圖表的其余字體不同。 我想知道如何獲得geom_text
相同字體作為geom_text
的其余字體。
編輯
sessionInfo()
R version 3.6.0 (2019-04-26)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.2 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/atlas/libblas.so.3.10.3
LAPACK: /usr/lib/x86_64-linux-gnu/atlas/liblapack.so.3.10.3
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggthemes_4.2.0 ggplot2_3.1.1
loaded via a namespace (and not attached):
[1] Rcpp_1.0.1 rstudioapi_0.10 magrittr_1.5 tidyselect_0.2.5
[5] munsell_0.5.0 colorspace_1.4-1 R6_2.4.0 rlang_0.3.4.9003
[9] stringr_1.4.0 plyr_1.8.4 dplyr_0.8.1 tools_3.6.0
[13] grid_3.6.0 gtable_0.3.0 withr_2.1.2 lazyeval_0.2.2
[17] assertthat_0.2.1 tibble_2.1.1 crayon_1.3.4 purrr_0.3.2
[21] vctrs_0.1.0.9003 zeallot_0.1.0 glue_1.3.1 labeling_0.3
[25] stringi_1.4.3 compiler_3.6.0 pillar_1.4.0 scales_1.0.0
[29] backports_1.1.4 pkgconfig_2.0.2
我不確定為什么軸標題的字體與圖形中geom_text
調用產生的字體不同。 如果我運行你的代碼,字體是相同的。
根據Hadley Wickham的“ggplot2:用於數據分析的優雅圖形”(第2版),
只有3種字體可以保證在任何地方都可以使用:“sans”,“serif”和“mono”(第37頁)
如果您使用以下代碼,我認為您將使用相同的字體axis和geom_text。
# solution for text family
### explicitely setting "family" twice
p <- ggplot(data = mtcars, mapping = aes(x = wt, y = mpg)) +
geom_point() +
theme_igray(base_family = "sans") ## <----
p + geom_text(mapping = aes(label = rownames(mtcars)), family = "sans") ## <----
就我而言,這產生了以下圖表:
在我這邊,我可以為圖中的兩種類型的文本打開“sans”,“serif”和“mono”的任意組合。
請告訴我這是否適合您。
這對你有用嗎? 我通過運行theme_igray %>% View()
主題參數,我看到基線text
大小和顏色是12 pt black,但是axis.text
是grey30
並且相對大小為0.8,即9.6 pt。 與全黑字體相比,略微淺黑色的顏色與使用較輕字體重量的外觀相似。
對於神秘的原因,指出這里 ,在文本大小geom_text
都以相近的比例調整為0.353 [編輯,看到@zeehio評論; 是5:14]與主題大小相比。 隨着顏色和大小,這些應該匹配。
library(ggplot2)
library(ggthemes)
p <- ggplot(data = mtcars, mapping = aes(x = wt, y = mpg)) +
geom_point() +
theme_igray()
p
p + geom_text(mapping = aes(label = rownames(mtcars)),
color = "gray30", size = 12 * 5/14 * 0.8)
這是另一個例子。 在我的系統(OSX 10.13,R 3.5.1)上,這些匹配,我通過使用GIMP中的“差異”過濾器確認,顯示它們排成一行。
base_size = 36
ggplot(data = mtcars, mapping = aes(x = wt, y = mpg)) +
annotate("text", x = 1, y = 5*3:6, label = 5*3:6,
color = "gray30", size = 12 * 0.353 * 0.8) +
annotate("text", x = 10, y = 5*3:6, label = 5*3:6,
color = "gray30", size = 12 * 0.353 * 0.8) +
theme_igray() +
theme(panel.grid = element_blank())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.