2012-04-07 12 views
10

Parece que las tiras están siempre por encima de la trama creada por ggplot2. ¿Pueden ser movidos debajo de la trama?¿Cómo mostrar las etiquetas de la tira debajo de la trama al facetear?

Por ejemplo:

library(ggplot2) 
qplot(hwy, cty, data = mpg) + facet_grid(. ~ manufacturer) 

muestra la información de coche en la parte superior. ¿Se pueden mostrar en la parte inferior?

+2

Una pregunta similar se le preguntó en la lista de correo ggplot cierta edad vez. [Ver aquí] (http://groups.google.com/group/ggplot2/browse_thread/thread/415c00e3373c9cc8/8fb65cc4aa0849cf?lnk=gst&q=facet+text+label#8fb65cc4aa0849cf) –

+0

¡Gracias! No me di cuenta de que era tan difícil –

+0

Ver también: http://stackoverflow.com/questions/3261597/can-i-change-the-position-of-the-strip-label-in-ggplot-from-the- top-to-the-botto (otra respuesta negativa) –

Respuesta

6

Actualización: Usando ggplot2 versión 2.1.0, considere usar switch = 'x'. Vea ?facet_grid para más detalles.

Usando las funciones gtable, es fácil mover la tira. (O ver here de anteras versión - intercambiando eje X y tiras)

library(ggplot2) 
library(gtable) 
library(grid) 

p <- ggplot(mpg, aes(hwy, cty)) + geom_point() + facet_grid(. ~ manufacturer) + 
    theme(strip.text.x = element_text(angle = 90, vjust = 1), 
      strip.background = element_rect(fill = NA)) 

# Convert the plot to a grob 
gt <- ggplotGrob(p) 

# Get the positions of the panels in the layout: t = top, l = left, ... 
panels <-c(subset(gt$layout, grepl("panel", gt$layout$name), select = t:r)) 

# Add a row below the x-axis tick mark labels, 
# the same height as the strip 
gt = gtable_add_rows(gt, gt$height[min(panels$t)-1], max(panels$b) + 2) 

# Get the strip grob 
stripGrob = gtable_filter(gt, "strip-t") 

# Insert the strip grob into the new row 
gt = gtable_add_grob(gt, stripGrob, t = max(panels$b) + 3, l = min(panels$l), r = max(panels$r)) 

# remove the old strip 
gt = gt[-(min(panels$t)-1), ] 

grid.newpage() 
grid.draw(gt) 

enter image description here

+1

Si eres como yo y quieres las etiquetas de la tira * sobre * el eje x, puedes usar 'gt = gtable_add_grob (gt, stripGrob, t = max (paneles) $ b) + 1, l = min (paneles $ l), r = max (paneles $ r)) 'en lugar de lo que Sandy tiene – Nova

Cuestiones relacionadas