自定义 ggplot2 主题

在 Tidyverse 中用数据进行沟通

Timo Grossenbacher

Data Journalist

自定义外观的优势

在 Tidyverse 中用数据进行沟通

theme() 函数

ggplot(plot_data) +
  geom_histogram(aes(
    x = working_hours)) +
  labs(x = "每周工作小时数",
       y = "国家数量") +
  theme(
    text = element_text(
      family = "Bookman",
      color = "gray25")
  )

在 Tidyverse 中用数据进行沟通

ggplot2 默认主题

ggplot(plot_data) +
  geom_histogram(aes(
    x = working_hours)) +
  labs(x = "每周工作小时数",
       y = "国家数量") +

  theme_classic()

在 Tidyverse 中用数据进行沟通

串联 theme() 调用

ggplot(plot_data) +
  geom_histogram(aes(
    x = working_hours)) +
  labs(x = "每周工作小时数",
       y = "国家数量") +

  theme_classic() +
  theme(
    text = element_text(
      family = "Bookman",
      color = "gray25")
  )

在 Tidyverse 中用数据进行沟通

主题配置选项

?theme

axis.title
坐标轴标题(element_text;继承自 text)

axis.title.x
x 轴标题(element_text;继承自 axis.title)

axis.title.x.top
上方 x 轴标题(element_text;继承自 axis.title.x)

axis.title.x.bottom
下方 x 轴标题(element_text;继承自 axis.title.x)

在 Tidyverse 中用数据进行沟通

element_* 函数族

element_text()
element_rect()
element_line()
element_blank()
ggplot(plot_data) +
  geom_histogram(aes(
    x = working_hours)) +
  labs(x = "每周工作小时数",
       y = "国家数量") +
  theme(
    text = element_blank()
  )

在 Tidyverse 中用数据进行沟通

让我们试试主题!

在 Tidyverse 中用数据进行沟通

Preparing Video For Download...