2015-02-13 11 views
5

में पहलू लिपटे लेआउट के लिए पाठ बॉक्स में जोड़ें मुझे पता है कि एक एक साजिश बड़े और छोटे व्यूपोर्ट गठबंधन करने के लिए ggplot2 या यहाँ तक कि के द्वारा बनाई गई व्याख्या करने में सक्षम है हूँ, के रूप में ggplot2 पुस्तक में प्रलेखित है। हालांकि, ऐसा लगता है कि वास्तविक साजिश क्षेत्रों में ये केवल एक ही काम है और "अंतिम साजिश" में नहीं। enter image description hereggplot2

यहाँ हम एक रेखीय प्रतिगमन चिकनी एक द्विपद डाटासेट लागू दिखा दस पैनलों, लेकिन है कि नहीं बिंदु देखें:

उदाहरण के लिए मैं इस तरह एक साजिश है। अब मैं साजिश के निचले दाएं हिस्से में एक पाठ के रूप में सारांश (डेटाफ्रेम में संग्रहीत) चाहता हूं, जैसे कि ... enter image description here

मुझे कोई उदाहरण नहीं मिला जो करीब आता है। किसी भी संकेत, मदद करता है या टिप्पणी बहुत सराहना कर रहे हैं!

+3

आप) पैकेज gridExtra को देखा है, और ggplotGrob इस्तेमाल किया (? – lawyeR

+0

!!! आप सही हैं, मैंने आपके सुझाव के अनुसार ग्रिड एक्स्ट्रा की जांच की है और इस पोस्ट में अंतिम टिप्पणी देखें: http://www.r-bloggers.com/extra-extra-get-your-gridextra/ – Jens

+0

यह पता चला है कि ग्रिडएक्स्ट्रा है बहुत बेकार मैं एनओएल = 2 का उपयोग कर दो पंक्ति या कॉलम लेआउट में टेबल और आकृति को प्लॉट करने में सक्षम था लेकिन यह वास्तव में जरूरतों को पूरा नहीं करता है। मैं कोशिश करना जारी रखूंगा और ggplotGrob फ़ंक्शन भी देखेंगे। – Jens

उत्तर

1

बल्कि खेल के लिए देर हो चुकी है, लेकिन मैं किसी भी समाधान है कि कई खाली पहलू रिक्त स्थान के लिए प्रदान, इसलिए यहाँ जाता है नहीं देखा है।

चरण 0। 2 रिक्त पहलुओं के साथ ggplot नमूना, इनबिल्ट हीरे डेटासेट का उपयोग करके:

library(ggplot2) 

p <- ggplot(diamonds, 
     aes(x = carat, y = price)) + 
    geom_point() + 
    geom_smooth() + 
    facet_wrap(~color) 
p 

step 0

चरण 1ggplotGrob

gp <- ggplotGrob(p) 

library(gtable) 

# visual check of gp's layout (in this case, it has 21 rows, 15 columns) 
gtable_show_layout(gp) 

step 1

का उपयोग कर चरण 2 gtable को भूखंड कन्वर्ट। (वैकल्पिक) टेक्स्टबॉक्स के लिए उपयोग किए जाने वाले अपूर्ण कोशिकाओं के सेल निर्देशांक प्राप्त करें। यदि आप ऊपर दिए गए लेआउट को पढ़ना पसंद करते हैं तो आप इसे छोड़ सकते हैं। इस मामले में शीर्ष बाएं सेल (16, 8) होगा और नीचे दाएं सेल (18, 12) होगा।

# get coordinates of empty panels to be blanked out 
empty.area <- gtable_filter(gp, "panel", trim = F) 
empty.area <- empty.area$layout[sapply(empty.area$grob, 
             function(x){class(x)[[1]]=="zeroGrob"}),] 

empty.area$t <- empty.area$t - 1 #extend up by 1 cell to cover facet header 
empty.area$b <- empty.area$b + 1 #extend down by 1 cell to cover x-axis 

> empty.area 
    t l b r z clip  name 
6 16 8 18 8 1 on panel-3-2 
9 16 12 18 12 1 on panel-3-3 

चरण 3।एक tableGrob

library(gridExtra) 

gp0 <- gtable_add_grob(x = gp, 
         grobs = tableGrob("some text", 
             theme = ttheme_minimal()), 
         t = min(empty.area$t), #16 in this case 
         l = min(empty.area$l), #8 
         b = max(empty.area$b), #18 
         r = max(empty.area$r), #12 
         name = "textbox") 
grid::grid.draw(gp0) 

दिखाते रूप ओवरले पाठ बॉक्स कुछ रूपांतरों:

gp1 <- gtable_add_grob(x = gp, 
         grobs = tableGrob("Simple line of comment that can go on & on for the sake of demonstration. Automatic line wrap not included.", 
             theme = ttheme_minimal()), 
         t = min(empty.area$t), 
         l = min(empty.area$l), 
         b = max(empty.area$b), 
         r = max(empty.area$r), 
         name = "textbox") 
grid::grid.draw(gp1) 

demo 1

gp2 <- gtable_add_grob(x = gp, 
         grobs = tableGrob("Simple line of comment that can go on & on. 
Automatic line wrap not included. \nAt least it understands the concept of line breaks.", 
             theme = ttheme_minimal()), 
         t = min(empty.area$t), 
         l = min(empty.area$l), 
         b = max(empty.area$b), 
         r = max(empty.area$r), 
         name = "textbox") 
grid::grid.draw(gp2) 

demo 2

gp3 <- gtable_add_grob(x = gp, 
         grobs = tableGrob(tibble::tribble(~col1, ~col2, 
                 "a.", "This is a line in a table", 
                 "b.", "This is another line in a table"), 
             rows = NULL, 
             theme = ttheme_minimal()), 
         t = min(empty.area$t), 
         l = min(empty.area$l), 
         b = max(empty.area$b), 
         r = max(empty.area$r), 
         name = "textbox") 
grid::grid.draw(gp3) 

demo 3

+0

बहुत बढ़िया! देर से लेकिन स्वागत से ज्यादा! यह एक समस्या है जिसे मैं अभी भी सामना करता हूं और इसके लिए समाधान नहीं मिला। बहुत धन्यवाद लिन! – Jens

0

ggplot2 2.2 के बाद से, मैं शीर्षक विकल्प के बजाय का उपयोग किया है। आप इसे आज़मा सकते हैं।

library(tidyverse) 

ggplot(data = mtcars, 
      mapping = aes(y=mpg, x=wt)) + 
    geom_point() + 
    facet_wrap(c("gear", "cyl"), labeller = "label_both") + 
    labs(title = "mtcars analysis", 
     subtitle = "This is a subtitle \n", 
     caption = "\n Note: This analysis compares the number of gears and cylinders", 
     x = "weight", y = "mpg") 

और यहाँ आप क्या मिलेगा करेंगे:

ggtest

मुझे आशा है कि आप में मदद करता है।

+0

दिलचस्प है, लेकिन कैप्शन आंकड़े के नीचे समाप्त होता है, और आकृति के निचले दाएं कोने को भर नहीं रहा है। फिर भी, आपके योगदान के लिए धन्यवाद। – Jens

 संबंधित मुद्दे