2012-06-11 19 views
11

En ggplot2 versión 0.9, el comportamiento de la alineación de un título de trazado cambió. Mientras que en v0.8.9 la alineación era relativa a la ventana de trazado, en v0.9 la alineación es relativa a la cuadrícula.¿Cómo alinear el título de ggplot con la ventana en lugar de trazar la cuadrícula?

Ahora, aunque estoy de acuerdo en que es un comportamiento deseable, con bastante frecuencia tengo títulos de trama muy largos.

Pregunta: ¿Hay una manera de alinear el título parcela con la ventana gráfica en lugar de la parrilla de trama?

Estoy buscando una solución que haga la alineación automática de la trama. En otras palabras, la alineación manual usando hjust no funcionaría para mí (ejecuto esto en cientos de gráficos para cada proyecto).

Cualquier solución que haya usado grid directamente también es aceptable.


Algunos ejemplos de código y trazado: (Observe cómo el título se trunca a la derecha de la ventana).

dat <- data.frame(
    text = c(
    "It made me feel very positive to brand X", 
    "It was clear and easy to understand", 
    "I didn't like it al all"), 
    value=runif(3) 
) 
library(ggplot2) 
ggplot(dat, aes(text, value)) + 
    geom_bar(stat="identity") + 
    coord_flip() + 
    opts(title="Thinking about the ad that you've just seen, do you agree with the following statements? I agree that...") + 
    theme_bw(16) 

enter image description here

Respuesta

13

En ggplot2 0.9 se puede cambiar fácilmente el diseño.

p <- ggplot(dat, aes(text, value)) + 
    geom_bar(stat="identity") + 
    coord_flip() + 
    opts(title="Thinking about the ad that you've just seen,\ndo you agree with the following statements?\nI agree that...") + 
    theme_bw(16) 

gt <- ggplot_gtable(ggplot_build(p)) 
gt$layout[which(gt$layout$name == "title"), c("l", "r")] <- c(1, max(gt$layout$r)) 
grid::grid.draw(gt) 

Tal vez, en la versión futura, ggplot2 proporcionará interfaces consistentes para ajustar el diseño.

enter image description here

+0

Gracias. Esto es muy útil de hecho. – Andrie

+0

también puede cambiar el truncamiento cambiando el clip a falso. – baptiste

+3

¿Alguien sabe si hay una manera más fácil de hacer esto con ggplot2_2.2.0? – MatthewR

0

Aquí hay una solución bajo ggplot2 2.2.1. Una función organiza el objeto de texto del título sobre el centro superior de ggplot.

library(ggplot2) 
library(grid) 
library(gridExtra) 

# A function that puts a title text object centered above a ggplot object "p" 
add_centered_title <- function(p, text, font_size){ 

    title.grob <- textGrob(
    label = text, 
    gp = gpar(fontsize = font_size) 
) 
    p1 <- arrangeGrob(p, top = title.grob) 
    grid.draw(p1) 
} 

# Create the chart from your sample data 
test_chart <- ggplot(dat, aes(text, value)) + 
    geom_bar(stat="identity") + 
    coord_flip() + 
    theme_bw(16) 

# Usage: 
add_centered_title(test_chart, 
        "Thinking about the ad that you've just seen, do you agree with the following statements? I agree that...", 
        10) 

# Or you can pipe a ggplot into this function using the %>% dplyr pipe: 
library(dplyr) 
test_chart %>% 
    add_centered_title("Thinking about the ad that you've just seen, do you agree with the following statements? I agree that...", 
        10) 
Cuestiones relacionadas