定义您的主题函数
在视频中,您看到如何通过将一段代码替换为函数调用,从而减少大量输入。您也看到了函数的一般定义方式。现在,请把这项知识应用到之前的两个 theme() 调用上,使其可以复用。
本练习是课程的一部分
在 Tidyverse 中用数据进行沟通
练习说明
- 根据您对函数定义的新认识,创建一个名为
theme_ilo()的函数,用于封装您的主题设置。 - 函数体应包含通常直接应用到绘图对象的两个
theme()调用:theme_minimal()+ 您自定义的theme()设置。 - 将您之前的做法复制到这个
theme_ilo()函数中,并使用示例代码中提供的theme()设置。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# For a starter, let's look at what you did before: adding various theme calls to your plot object
ilo_plot +
theme_minimal() +
theme(
text = element_text(family = "Bookman", color = "gray25"),
plot.subtitle = element_text(size = 12),
plot.caption = element_text(color = "gray30"),
plot.background = element_rect(fill = "gray95"),
plot.margin = unit(c(5, 10, 5, 10), units = "mm")
)
# Define your own theme function below
theme_ilo <- function() {
___() +
___(
___
)
}