Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

r - Is it possible to use the "theme_light()" together with "theme" in a ggplot command?

If I write the following plot seen below and use theme(plot.title = element_text(hjust = 0.5)) to center the title "Flowers", everything seems to work fine:

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

However when I add theme_light() at the end to change the layout, suddenly the title is no longer centered. Is there a way to make it centered again?

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) +
  ggtitle("Flowers") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme_light()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Call theme_light() first to not overwrite 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


If you look at the code of theme_light() you'll see it calls theme_grey() and 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)
}

Now somewhere in theme_grey() you'll find the following line:

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

So when call theme(plot.title = element_text(hjust = 0.5)) first it will be overwritten by theme_light().

In cases where you'll find yourself repeatedly typing something like

ggplot() +
  geom_something() +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme_light()

you might define your own theme to save some typing

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
      )

  }

Let's give it a try

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...