2012-06-11 13 views
11

ggplot2 संस्करण 0.9 में प्लॉट शीर्षक के संरेखण का व्यवहार बदल गया। जबकि v0.8.9 में संरेखण प्लॉट विंडो से संबंधित था, v0.9 में संरेखण साजिश ग्रिड के सापेक्ष है।साजिश ग्रिड की बजाय विंडो के साथ ggplot शीर्षक को संरेखित कैसे करें?

अब, जबकि मैं ज्यादातर सहमत हूं कि यह वांछनीय व्यवहार है, मैं अक्सर बहुत लंबे साजिश खिताब रखता हूं।

प्रश्न: साजिश ग्रिड की बजाय साजिश खिड़की के साथ साजिश शीर्षक को संरेखित करने का कोई तरीका है?

मैं ऐसे समाधान की तलाश में हूं जो साजिश का स्वचालित संरेखण करता है। दूसरे शब्दों में, hjust का उपयोग कर मैन्युअल संरेखण मेरे लिए काम नहीं करेगा (मैं इसे प्रत्येक प्रोजेक्ट के लिए सैकड़ों भूखंडों पर चलाता हूं)।

grid का उपयोग करने वाला कोई भी समाधान सीधे स्वीकार्य है।


कुछ नमूना कोड और साजिश: (ध्यान दें कि खिड़की के दाईं ओर शीर्षक कैसे छोटा हो जाता है)।

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

उत्तर

13

ggplot2 0.9 में आप आसानी से लेआउट बदल सकते हैं।

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) 

शायद, भविष्य के संस्करण में, ggplot2 लेआउट को ट्वीव करने के लिए लगातार इंटरफेस प्रदान करेगा।

enter image description here

+0

धन्यवाद। यह वास्तव में बहुत उपयोगी है। – Andrie

+0

आप झूठी को क्लिप बदलकर छंटनी भी बदल सकते हैं। – baptiste

+3

क्या किसी को पता है कि ggplot2_2.2.0 के साथ ऐसा करने का कोई आसान तरीका है? – MatthewR

0

यहाँ ggplot2 2.2.1 के तहत एक समाधान है। एक फ़ंक्शन 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) 
संबंधित मुद्दे