2011-09-18 10 views
30

Me gustaría crear un nuevo tema para ggplot basado en theme_bw().Copiando y modificando un tema predeterminado

me imagino los siguientes pasos son necesarios (en pseudocódigo):

  1. hacer una copia de theme_bw(): theme_new() <- theme_bw()
  2. modificar la copia: theme_update(axis.title.x = theme_text(family = base_family, size = base_size, vjust = 0.5))

Algún consejo sobre cómo implementar esto será muy apreciado!


Editar: @Andrie, he modificado su respuesta para mis necesidades:

theme_new <- theme_set(theme_bw()) 
theme_new <- theme_update(axis.title.x = theme_text(family = base_family, size = base_size, vjust = 0.5)) 

Sin embargo, me sale el siguiente error:

ggplot(mtcars, aes(factor(cyl))) + geom_bar() 

Error in match(gparname, names(gpars)) : object 'base_size' not found


Edit: 31/10/2017, respuesta proporcionada por @Andrie funciona bien. R versión 3.4.1, ggplot2_2.2.1

Respuesta

10

la wiki sugiere una manera de hacer esto utilizando 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

gracias, esto funcionó! Intenté esto antes, pero no me di cuenta de la parte 'base_size = base_size, base_family = base_family' y siempre recibí el error' Error in match (gparname, names (gpars)): object 'base_size' not found'. – donodarazao

+3

nota: esto ahora es redundante con el nuevo sistema de subdivisión introducido en ggplot2 0.9. – baptiste

+2

[Enlace que describe el nuevo sistema de temas y describe cómo modificarlo.] (Https://github.com/wch/ggplot2/wiki/New-theme-system) – Gregor

5

Trate como éste:

### 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() 
) 
6

Para las nuevas versiones , Basado en el artículo 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) 
} 

Nota que utilizo txt y txt_bold evitar escribir mismas cosas una y otra vez.

+0

El enlace está roto. – zx8754