2011-07-18 15 views

Respuesta

86

para cambiar el color de fondo del panel, utilice el siguiente código:

myplot + theme(panel.background = element_rect(fill = 'green', colour = 'red')) 

Para cambiar el color de la trama (pero no el color del panel), que puede hacer:

myplot + theme(plot.background = element_rect(fill = 'green', colour = 'red')) 

Vea aquí para más detalles temáticos Quick reference sheet for legends, axes and themes.

+39

También está el 'theme_bw', que le da un fondo blanco y líneas grises de la cuadrícula. Lo uso todo el tiempo, ya que en la impresión se ve mucho mejor que el fondo gris predeterminado: 'myplot + theme_bw()' – ROLO

+0

@ROLO: ¡Agradable! ¿Hay alguna manera de aplicar esto a todas las tramas de forma predeterminada? – krlmlr

+9

poner esto en el comienzo de su script por defecto para B & W ggplots: 'ggplot <- function (...) { \t ggplot2 :: ggplot (...) + theme_bw() } ' – ROLO

44

Para evitar desaprobado opts y theme_rect uso:

myplot + theme(panel.background = element_rect(fill='green', colour='red')) 

Para definir su propio tema personalizado, basado en theme_gray pero con algunos de los cambios y algunos extras añadidos, incluyendo el control de color de cuadrícula/tamaño (más opciones disponible para jugar con at ggplot2.org):

theme_jack <- function (base_size = 12, base_family = "") { 
    theme_gray(base_size = base_size, base_family = base_family) %+replace% 
     theme(
      axis.text = element_text(colour = "white"), 
      axis.title.x = element_text(colour = "pink", size=rel(3)), 
      axis.title.y = element_text(colour = "blue", angle=45), 
      panel.background = element_rect(fill="green"), 
      panel.grid.minor.y = element_line(size=3), 
      panel.grid.major = element_line(colour = "orange"), 
      plot.background = element_rect(fill="red") 
    ) 
} 

para que su tema personalizado predeterminado cuando ggplot se llama en el futuro, sin enmascaramiento:

theme_set(theme_jack()) 

Si desea cambiar un elemento del tema establecido actualmente:

theme_update(plot.background = element_rect(fill="pink"), axis.title.x = element_text(colour = "red")) 

Para almacenar el tema por defecto actual como un objeto:

theme_pink <- theme_get() 

Tenga en cuenta que theme_pink es una lista que, theme_jack era una función. Entonces, para devolver el tema a theme_jack use theme_set(theme_jack()) mientras que para regresar a theme_pink use theme_set(theme_pink).

Puede reemplazar theme_gray por theme_bw en la definición de theme_jack si lo prefiere. Para el tema personalizado que se asemejan a theme_bw pero con todas las líneas de división (x, y, mayor y menor) desactivado:

theme_nogrid <- function (base_size = 12, base_family = "") { 
    theme_bw(base_size = base_size, base_family = base_family) %+replace% 
     theme(
      panel.grid = element_blank() 
    ) 
} 

último un tema más radical útil en tablas de choropleths u otros mapas en ggplot, basado en la discusión here pero actualizado para evitar la desaprobación. El objetivo aquí es eliminar el fondo gris y cualquier otra característica que pueda distraer del mapa.

theme_map <- function (base_size = 12, base_family = "") { 
    theme_gray(base_size = base_size, base_family = base_family) %+replace% 
     theme(
      axis.line=element_blank(), 
      axis.text.x=element_blank(), 
      axis.text.y=element_blank(), 
      axis.ticks=element_blank(), 
      axis.ticks.length=unit(0.3, "lines"), 
      axis.ticks.margin=unit(0.5, "lines"), 
      axis.title.x=element_blank(), 
      axis.title.y=element_blank(), 
      legend.background=element_rect(fill="white", colour=NA), 
      legend.key=element_rect(colour="white"), 
      legend.key.size=unit(1.2, "lines"), 
      legend.position="right", 
      legend.text=element_text(size=rel(0.8)), 
      legend.title=element_text(size=rel(0.8), face="bold", hjust=0), 
      panel.background=element_blank(), 
      panel.border=element_blank(), 
      panel.grid.major=element_blank(), 
      panel.grid.minor=element_blank(), 
      panel.margin=unit(0, "lines"), 
      plot.background=element_blank(), 
      plot.margin=unit(c(1, 1, 0.5, 0.5), "lines"), 
      plot.title=element_text(size=rel(1.2)), 
      strip.background=element_rect(fill="grey90", colour="grey50"), 
      strip.text.x=element_text(size=rel(0.8)), 
      strip.text.y=element_text(size=rel(0.8), angle=-90) 
     ) 
} 
+1

Esto es muy útil, gracias. FYI, he encontrado que el argumento 'plot.background' debe pasarse a' theme'. Los otros argumentos son opcionales. –

Cuestiones relacionadas