Copying and modifying a default theme

前端 未结 4 1348
你的背包
你的背包 2020-12-13 13:51

I would like to create a new theme for ggplot that is based on theme_bw().

I imagine the following steps are necessary (in pseudocode):

相关标签:
4条回答
  • 2020-12-13 14:01

    Try like this one:

    ### Set up a blank theme
    theme_none <- theme(
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
      panel.background = element_blank(),
      axis.title.x = element_text(colour=NA),
      axis.title.y = element_blank(),
      axis.text.x = element_blank(),
      axis.text.y = element_blank(),
      axis.line = element_blank()
      #axis.ticks.length = element_blank()
    )
    
    0 讨论(0)
  • 2020-12-13 14:05

    For the newer versions, based on the article here

    txt <- element_text(size = 14, colour = "black", face = "plain")
    bold_txt <- element_text(size = 14, colour = "black", face = "bold")
    
    theme_whatever <- function(base_size = 14, base_family = "Palatino")
    {
      theme_bw(base_size = base_size, base_family = base_family) +
      theme(
        legend.key = element_blank(), 
        strip.background = element_blank(), 
    
        text = txt, 
        plot.title = txt, 
    
        axis.title = txt, 
        axis.text = txt, 
    
        legend.title = bold_txt, 
        legend.text = txt ) 
    }
    

    Note that I use txt and txt_bold to avoid writing same stuff over and over again.

    0 讨论(0)
  • 2020-12-13 14:14

    Your code just needs a few small changes to work (mainly removing brackets and adding brackets at the right places)

    theme_new <- theme_set(theme_bw())
    
    theme_new <- theme_update(
        panel.background = element_rect(fill="lightblue"))
    
    ggplot(mtcars, aes(factor(cyl))) + geom_bar()
    

    enter image description here


    Reference:

    • CRAN: extending-ggplot2
    • Tidyverse: themes
    • GitHub: New-theme-system
    0 讨论(0)
  • 2020-12-13 14:23

    the wiki suggests one way to do this using modifyList,

    theme_new <- function (base_size = 12, base_family = "", ...){
     modifyList (theme_bw (base_size = base_size, base_family = base_family),
              list (axis.title.x = theme_text(family = base_family, 
                    size = base_size, vjust = 0.5)))
    }
    
    0 讨论(0)
提交回复
热议问题