[英]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.