2011-09-18 8 views
30

Vorrei creare un nuovo tema per ggplot basato su theme_bw().Copia e modifica di un tema predefinito

immagino i seguenti passaggi sono necessari (in pseudocodice):

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

Qualche consiglio su come implementare questo sarà molto apprezzato!


Edit: @Andrie, ho modificato la tua risposta per le mie esigenze:

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

Tuttavia, ottengo il seguente errore:

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

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


Modifica: 31/10/2017, la risposta fornita da @Andrie funziona bene. R versione 3.4.1, ggplot2_2.2.1

risposta

10

l'wiki suggerisce un modo per farlo utilizzando 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

grazie, ha funzionato! L'ho provato prima, ma non ho trovato la parte 'base_size = base_size, base_family = base_family' e ho sempre ricevuto l'errore' Errore nella corrispondenza (gparname, nomi (gpars)): oggetto 'base_size' non trovato'. – donodarazao

+3

nota: questo è ora ridondante con il nuovo sistema di temi introdotto in ggplot2 0.9. – baptiste

+2

[Link che descrive il nuovo sistema tematico e descrive come modificarlo.] (Https://github.com/wch/ggplot2/wiki/New-theme-system) – Gregor

5

Prova come questo:

### 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

Per le versioni più recenti , Basata sull'articolo 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 che uso txt e txt_bold di evitare di scrivere stesse cose più e più volte.

+0

Il collegamento è interrotto. – zx8754

Problemi correlati