是否可以在ggplot命令中将“ theme_light()”与“ theme”一起使用?

时间:2018-08-11 20:37:28

标签: r ggplot2

如果我写下面的图并使用theme(plot.title = element_text(hjust = 0.5))将标题“花”居中,一切似乎都可以正常工作

data(iris)
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) +
  ggtitle("Flowers") +
  theme(plot.title = element_text(hjust = 0.5))

但是,当我在末尾添加theme_light()以更改布局时,标题突然不再居中。有没有办法使它再次居中?

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) +
  ggtitle("Flowers") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme_light()

1 个答案:

答案 0 :(得分:2)

先致电theme_light(),以不覆盖theme(plot.title = element_text(hjust = 0.5))

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) +
  ggtitle("Flowers") +
  theme_light() +
  theme(plot.title = element_text(hjust = 0.5))

enter image description here


如果您查看theme_light()的代码,则会看到它分别称为theme_grey()theme()

function (base_size = 11, base_family = "", base_line_size = base_size/22, 
    base_rect_size = base_size/22) 
{
    half_line <- base_size/2
    theme_grey(base_size = base_size, base_family = base_family, 
        base_line_size = base_line_size, base_rect_size = base_rect_size) %+replace% 
        theme(panel.background = element_rect(fill = "white", 
            colour = NA), panel.border = element_rect(fill = NA, 
            colour = "grey70", size = rel(1)), panel.grid = element_line(colour = "grey87"), 
            panel.grid.major = element_line(size = rel(0.5)), 
            panel.grid.minor = element_line(size = rel(0.25)), 
            axis.ticks = element_line(colour = "grey70", size = rel(0.5)), 
            legend.key = element_rect(fill = "white", colour = NA), 
            strip.background = element_rect(fill = "grey70", 
                colour = NA), strip.text = element_text(colour = "white", 
                size = rel(0.8), margin = margin(0.8 * half_line, 
                  0.8 * half_line, 0.8 * half_line, 0.8 * half_line)), 
            complete = TRUE)
}

现在在theme_grey()中的某处,您会找到以下行:

plot.title = element_text(size = rel(1.2), hjust = 0

因此,当首先调用theme(plot.title = element_text(hjust = 0.5))时,它将被theme_light()覆盖。

在重复输入

之类的情况下
ggplot() +
  geom_something() +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme_light()

您可以定义自己的主题以保存一些输入内容

theme_WoeIs <-
  function(base_size = 11,
           base_family = "",
           base_line_size = base_size / 22,
           base_rect_size = base_size / 22,
           ...)  {
    half_line <- base_size / 2
    theme_grey(
      base_size = base_size,
      base_family = base_family,
      base_line_size = base_line_size,
      base_rect_size = base_rect_size
    ) %+replace%
      theme(
        panel.background = element_rect(fill = "white",
                                        colour = NA),
        panel.border = element_rect(
          fill = NA,
          colour = "grey70",
          size = rel(1)
        ),
        panel.grid = element_line(colour = "grey87"),
        panel.grid.major = element_line(size = rel(0.5)),
        panel.grid.minor = element_line(size = rel(0.25)),
        axis.ticks = element_line(colour = "grey70", size = rel(0.5)),
        legend.key = element_rect(fill = "white", colour = NA),
        strip.background = element_rect(fill = "grey70",
                                        colour = NA),
        strip.text = element_text(
          colour = "white",
          size = rel(0.8),
          margin = margin(0.8 * half_line,
                          0.8 * half_line, 0.8 * half_line, 0.8 * half_line)
          ),
        complete = TRUE,
        plot.title = element_text(hjust = 0.5), # here is your part
        ... # this is new as well
      )

  }

让我们尝试一下

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) +
  ggtitle("Flowers") +
  theme_WoeIs()