2015-09-15 11 views
12

में पहलुओं का पूर्ण आकार सेट करना मैं एक रिपोर्ट के लिए कई पहलुओं वाले प्लॉट बना रहा हूं। पहलुओं की संख्या 2 से 8 के बीच बदलती है। आदर्श रूप में, मैं प्रत्येक पहलू (भूखंडों में) के पूर्ण आकार को समान (उदाहरण के लिए 4x4 सेमी) होना चाहता हूं ताकि उनकी तुलना करना आसान हो (और यह अच्छी तरह से दिखता है))।ggplot2

क्या यह संभव है?

df1 <- structure(list(group1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,1L, 1L, 1L, 1L, 1L), .Label = c("S1", "S2"), class = "factor"), group = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L), .Label = c("A", "B", "C", "D", "E"), class = "factor"), value = 1:12), class = "data.frame", row.names = c(NA, -12L), .Names = c("group1", "group", "value")) 

df2 <- structure(list(group1 = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("S1", "S2"), class = "factor"), group = structure(c(4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L), .Label = c("A", "B", "C", "D", "E"), class = "factor"), value = 13:20), class = "data.frame", row.names = c(NA, -8L), .Names = c("group1", "group", "value")) 

library(ggplot2) 

plot1 <- ggplot(df1) + geom_histogram(aes(x=value)) + facet_wrap(~group) 
plot2 <- ggplot(df2) + geom_histogram(aes(x=value)) + facet_wrap(~group) 

enter image description here enter image description here

उत्तर

10

मैं इस उद्देश्य के लिए निम्नलिखित समारोह का उपयोग किया है,

set_panel_size <- function(p=NULL, g=ggplotGrob(p), file=NULL, 
          margin = unit(1,"mm"), 
          width=unit(4, "cm"), 
          height=unit(4, "cm")){ 

    panels <- grep("panel", g$layout$name) 
    panel_index_w<- unique(g$layout$l[panels]) 
    panel_index_h<- unique(g$layout$t[panels]) 
    nw <- length(panel_index_w) 
    nh <- length(panel_index_h) 

if(getRversion() < "3.3.0"){ 

    # the following conversion is necessary 
    # because there is no `[<-`.unit method 
    # so promoting to unit.list allows standard list indexing 
    g$widths <- grid:::unit.list(g$widths) 
    g$heights <- grid:::unit.list(g$heights) 

    g$widths[panel_index_w] <- rep(list(width), nw) 
    g$heights[panel_index_h] <- rep(list(height), nh) 

} else { 

    g$widths[panel_index_w] <- rep(width, nw) 
    g$heights[panel_index_h] <- rep(height, nh) 

} 

    if(!is.null(file)) 
    ggsave(file, g, 
      width = convertWidth(sum(g$widths) + margin, 
           unitTo = "in", valueOnly = TRUE), 
      height = convertHeight(sum(g$heights) + margin, 
            unitTo = "in", valueOnly = TRUE)) 

    g 
} 

g1 <- set_panel_size(plot1) 
g2 <- set_panel_size(plot2) 
grid.arrange(g1,g2) 

enter image description here

+1

धन्यवाद, हालांकि मैं अभी भी कैसे बिना इस सुविधा का उपयोग करने के लिए नहीं मिलता है एक त्रुटि संदेश प्राप्त करना। क्या आप अपने उदाहरण डेटा के साथ वांछित परिणाम कैसे प्राप्त कर सकते हैं यह दिखाकर अपना जवाब विस्तारित कर सकते हैं? – beetroot

+0

मैंने अभी आर आरवेल के साथ यह कोशिश की है, और यह लाइन मेरे लिए काम नहीं करती है: 'g $ widths [panel_index_w] <- rep (list (width), nw)'। मुझे इसका उपयोग करने की ज़रूरत है: 'g $ widths [panel_index_w] <- rep (grid ::: unit.list (width), nw)'। –

+0

मेरी पिछली टिप्पणी का जवाब देने के लिए: लाइन 'g $ widths [panel_index_w] <- rep (list (width), nw)' 'g $ widths [panel_index_w] <- rep (width, nw) 'द्वारा प्रतिस्थापित की जानी चाहिए। फिर चीजें काम करते हैं। –