如何让geom_text继承主题规范? (ggplot2)
How to let geom_text inherit theme specifications? (ggplot2)
ggplot2
中是否有优雅的方法使 geom_text
/geom_label
像 base_family
一样继承 theme
规范?
或者反过来问:我可以指定一个也适用于 geom_text
/geom_label
的 theme
吗?
示例:
我希望 text/labels
看起来与 theme
中指定的 axis.text
完全一样...
显然我可以手动将规范作为可选参数添加到 geom_text
,但我希望它继承规范 "automatically"...
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 中未来绘图的几何对象样式: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()
ggplot2
中是否有优雅的方法使 geom_text
/geom_label
像 base_family
一样继承 theme
规范?
或者反过来问:我可以指定一个也适用于 geom_text
/geom_label
的 theme
吗?
示例:
我希望 text/labels
看起来与 theme
中指定的 axis.text
完全一样...
显然我可以手动将规范作为可选参数添加到 geom_text
,但我希望它继承规范 "automatically"...
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 中未来绘图的几何对象样式:http://ggplot2.tidyverse.org/reference/update_defaults.htmltheme_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()