開始使用免費開始

自訂你的主題函式

在影片中你看到,將一段段程式碼改寫為函式呼叫,可以省下大量輸入。你也看過函式的一般定義方式。現在就把這些知識用在讓你之前的兩個 theme() 呼叫可以重複使用。

本練習屬於課程

在 Tidyverse 中以資料溝通

檢視課程

練習說明

  • 運用你對函式定義的新知,建立名為 theme_ilo() 的函式,將你的主題設定封裝起來。
  • 函式本體應包含你平常會直接套用在圖形物件上的兩個呼叫: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() {
    ___() +
    ___(
        ___
    )
}
編輯並執行程式碼