作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
ggplot2
有没有优雅的方式制作geom_text
/geom_label
继承theme
规范如 base_family
?
或者反过来问:我可以指定 theme
这也适用于geom_text
/geom_label
?
示例:
我要text/labels
看起来就像 axis.text
如 theme
中指定...
显然,我可以将规范作为可选参数手动添加到 geom_text
,但我希望它“自动”继承规范...
library("ggplot2")
ggplot(mtcars, aes(x = mpg,
y = hp,
label = row.names(mtcars))) +
geom_point() +
geom_text() +
theme_minimal(base_family = "Courier")
添加:适用于 ggrepel::geom_text_repel/geom_label_repel
的解决方案那就更完美了...
最佳答案
你可以
首先,根据系统,您需要检查哪些字体可用。当我在 Windows 上运行时,我使用以下命令:
install.packages("extrafont")
library(extrafont)
windowsFonts() # check which fonts are available
theme_set
函数可让您指定 ggplot 的整体主题。因此,theme_set(theme_minimal(base_family = "Times New Roman"))
允许您定义绘图的字体。
要使标签继承此文本,我们需要使用两件事:
update_geom_defaults
允许您更新 ggplot 中 future 绘图的几何对象样式:http://ggplot2.tidyverse.org/reference/update_defaults.html theme_get()$text$family
提取当前全局 ggplot 主题的字体。通过结合这两者,标签样式可以更新如下:
# Change the settings
update_geom_defaults("text", list(colour = "grey20", family = theme_get()$text$family))
update_geom_defaults("text_repel", list(colour = "grey20", family = theme_get()$text$family))
theme_set(theme_minimal(base_family = "Times New Roman"))
# Change the settings
update_geom_defaults("text", list(colour = "grey20", family = theme_get()$text$family))
# Basic Plot
ggplot(mtcars, aes(x = mpg,
y = hp,
label = row.names(mtcars))) +
geom_point() +
geom_text()
# works with ggrepel
update_geom_defaults("text_repel", list(colour = "grey20", family = theme_get()$text$family))
library(ggrepel)
ggplot(mtcars, aes(x = mpg,
y = hp,
label = row.names(mtcars))) +
geom_point() +
geom_text_repel()
关于r - 如何让geom_text继承主题规范? (ggplot2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48977963/
我是一名优秀的程序员,十分优秀!