2012-02-28 16 views
23

मैं ggplots के संयोजन के लिए par() या लेआउट() फ़ंक्शंस का उपयोग करने के बारे में सोच रहा हूं। क्या उन कार्यों का उपयोग करना संभव होगा?par() या लेआउट() फ़ंक्शन का उपयोग करते हुए ggplot2 (एकल प्लॉट में नहीं) की संयुक्त प्लॉट?

कहें कि मैं हिस्टोग्राम के लिए स्कैटरप्लॉट और ggplot के लिए ggplot प्लॉट करना चाहता हूं। और मैं दो भूखंडों को जोड़ना चाहता हूं (एक सिंगल प्लॉट में नहीं)। क्या यह लागू है?

मैंने ggplot फ़ंक्शंस का उपयोग किये बिना आर में सरल साजिश के साथ इसे आजमाया। और यह वास्तव में काम करता है।

इधर, त्वरित-आर से एक नमूना है लिंक: http://www.statmethods.net/advgraphs/layout.html

# 4 figures arranged in 2 rows and 2 columns 
attach(mtcars) 
par(mfrow=c(2,2)) 
plot(wt,mpg, main="Scatterplot of wt vs. mpg") 
plot(wt,disp, main="Scatterplot of wt vs disp") 
hist(wt, main="Histogram of wt") 
boxplot(wt, main="Boxplot of wt") 

# One figure in row 1 and two figures in row 2 
attach(mtcars) 
layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE)) 
hist(wt) 
hist(mpg) 
hist(disp) 

लेकिन जब मैं ggplot उपयोग करने के लिए, और साजिश गठबंधन की कोशिश, मैं एक उत्पादन नहीं मिलता है।

+0

ggplot2 है आधार ग्राफिक्स नहीं है ताकि आप ggplot2 और लेआउट या par (mfrow) को गठबंधन नहीं कर सकें। आपको gridExtra में प्लॉट.रेंज आदि का उपयोग करने की आवश्यकता है। या, आप ग्रिड सिस्टम में व्यूपोर्ट के साथ खेल सकते हैं। – kohske

+0

चूंकि आपने 'लेआउट()' जैसे कुछ के लिए कहा था, तो शायद आपको 'grid.arrange() '(कोहस्के का उल्लेख किया गया) जैसे कुछ चाहिए। [यह लिंक किया गया पोस्ट] (http://stackoverflow.com/questions/7993722/creating-arbitrary-panes-in-ggplot2/7996205#7996205) (और विशेष रूप से बेन बोल्कर का जवाब) आपको एक अच्छा प्रारंभिक बिंदु देगा। –

उत्तर

32
library(ggplot2) 
library(grid) 


vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y) 


plot1 <- qplot(mtcars,x=wt,y=mpg,geom="point",main="Scatterplot of wt vs. mpg") 
plot2 <- qplot(mtcars,x=wt,y=disp,geom="point",main="Scatterplot of wt vs disp") 
plot3 <- qplot(wt,data=mtcars) 
plot4 <- qplot(wt,mpg,data=mtcars,geom="boxplot") 
plot5 <- qplot(wt,data=mtcars) 
plot6 <- qplot(mpg,data=mtcars) 
plot7 <- qplot(disp,data=mtcars) 

# 4 figures arranged in 2 rows and 2 columns 
grid.newpage() 
pushViewport(viewport(layout = grid.layout(2, 2))) 
print(plot1, vp = vplayout(1, 1)) 
print(plot2, vp = vplayout(1, 2)) 
print(plot3, vp = vplayout(2, 1)) 
print(plot4, vp = vplayout(2, 2)) 


# One figure in row 1 and two figures in row 2 
grid.newpage() 
pushViewport(viewport(layout = grid.layout(2, 2))) 
print(plot5, vp = vplayout(1, 1:2)) 
print(plot6, vp = vplayout(2, 1)) 
print(plot7, vp = vplayout(2, 2)) 
+0

आप सभी लोगों के लिए धन्यवाद, मेरी समस्या हल हो गई है। –

+1

मायासोरा, स्क्रिप्ट/कोड के लिए धन्यवाद। लेकिन जब मैं plot1 और plot2 मुद्रित करता हूं तो मुझे एक त्रुटि मिली, जो कहती है "eval में त्रुटि (expr, envir, enclos): ऑब्जेक्ट 'wt' नहीं मिला"। हालांकि, मैंने प्लॉट 1 और प्लॉट 2 में अपने तर्क को पुन: व्यवस्थित करके इसे ठीक किया है। प्लॉट 1 <- qplot (x = wt, y = mpg, data = mtcars, geom = "point", main = "wt बनाम mpg का स्कैटरप्लॉट") और plot2 <- qplot (x = wt, y = disp , डेटा = mtcars, geom = "point", मुख्य = "wt बनाम disp का स्कैटरप्लॉट") त्वरित प्रतिक्रिया के लिए फिर से धन्यवाद। –

+0

@ मायासोरा महान उत्तरों के लिए धन्यवाद। मैं मुख्य ग्रिड में मुख्य शीर्षक के रूप में एक शीर्षक कैसे जोड़ सकता हूं। मैंने grid.text विकल्प देखा लेकिन 2 से 4 ग्रिड में यह शीर्ष और मध्य में अच्छी तरह से नहीं आता है। –

7

जवाब grid.layout काम करता है को शामिल, मैं इसे का उपयोग किया है, और मैं इसे ऊपर से मतदान किया। हालांकि, मुझे आमतौर पर यह समाधान बहुत कठिन और त्रुटि-प्रवण लगता है, और मुझे लगता है कि अधिकांश ggplot2 उत्साही हैं जो नियमित रूप से इसे एक फ़ंक्शन में लपेटते हैं। मुझे पिछली खोजों में ऐसे कई रैपिंग फ़ंक्शंस मिले हैं, लेकिन मेरा वर्तमान वर्कहोर समाधान grid addon package called gridExtra में है। यह उपयोगी तर्क हैं, लेकिन डिफ़ॉल्ट पंक्तियां/स्तंभ सेटअप अक्सर है आप पहली जगह में क्या चाहता था:

library("ggplot2") 
# Generate list of arbitrary ggplots 
plot1 <- qplot(data = mtcars, x=wt, y=mpg, geom="point",main="Scatterplot of wt vs. mpg") 
plot2 <- qplot(data = mtcars, x=wt, y=disp, geom="point",main="Scatterplot of wt vs disp") 
plot3 <- qplot(wt,data=mtcars) 
plot4 <- qplot(wt,mpg,data=mtcars,geom="boxplot") 
plot5 <- qplot(wt,data=mtcars) 
plot6 <- qplot(mpg,data=mtcars) 
plot7 <- qplot(disp,data=mtcars) 
# You might have produced myPlotList using instead lapply, mc.lapply, plyr::dlply, etc 
myPlotList = list(plot1, plot2, plot3, plot4, plot5, plot6, plot7) 
library("gridExtra") 
do.call(grid.arrange, myPlotList) 

सूचना कैसे वास्तविक कोड "एक ग्राफिक में मेरी भूखंडों गठबंधन" एक पंक्ति थी (gridExtra लोड करने के बाद) । आप तर्क दे सकते हैं कि एक सूची में भूखंडों डाल एक अतिरिक्त पंक्ति था, लेकिन वास्तव में मैं वैकल्पिक रूप से इस्तेमाल किया जा सकता था

grid.arrange(plot1, plot2, plot3, plot4, plot5, plot6, plot7) 

ggplots की कम संख्या यह बेहतर हो सकता है के लिए। हालांकि, n_plots > 4 के लिए आप नाम देने और उन्हें टाइप करने के लिए नाराज होना शुरू कर देंगे।

+0

यदि आप दो कॉलम चाहते थे, उदाहरण के लिए, grid.arrange के परिणामस्वरूप, अंतिम पंक्ति में क्या होगा? मैंने args = list (ncol = 2) – lawyeR

+4

पर विविधताएं आजमाई हैं: यहां एक लाइनर है: 'do.call (grid.arrange, c (myPlotList, list (ncol = 2))) –

12

मुझे लगता है कि इस उपयोग के लिए अधिक ध्यान देने योग्य है layOut पूर्व में wq पैकेज (राजधानी "ओ") पर ध्यान दें। इसे wq पैकेज से हटा दिया गया है, इसलिए मैंने नीचे कोड डाला है और इसे सामान्य ggplot शैली से मेल खाने के लिए lay_out का नाम दिया है। यह base::layout जैसा है कि भूखंड पंक्तियों और स्तंभों में रखे गए विभिन्न आकारों के हो सकते हैं। lay_out पर प्रत्येक तर्क एक 3-तत्व सूची है जिसमें साजिश, पंक्ति सूचकांक जिसमें साजिश है, और कॉलम इंडेक्स जिसमें इसे साजिश करना है।

उदाहरण के लिए

, @Paul McMurdie के भूखंडों का उपयोग कर,

lay_out(list(plot1, 1, 1), 
     list(plot2, 1, 2), 
     list(plot3, 2, 1), 
     list(plot4, 2, 2), 
     list(plot5, 3, 1:2), 
     list(plot6, 4, 1:2), 
     list(plot7, 1:2, 3)) 

enter image description here

lay_out = function(...) {  
    x <- list(...) 
    n <- max(sapply(x, function(x) max(x[[2]]))) 
    p <- max(sapply(x, function(x) max(x[[3]]))) 
    grid::pushViewport(grid::viewport(layout = grid::grid.layout(n, p)))  

    for (i in seq_len(length(x))) { 
     print(x[[i]][[1]], vp = grid::viewport(layout.pos.row = x[[i]][[2]], 
      layout.pos.col = x[[i]][[3]])) 
    } 
} 

(कोड wq पैकेज के पिछले संस्करण के से प्राप्त, commit history on the unofficial Github CRAN mirror से।)

+0

मुझे वास्तव में यह समाधान पसंद है। grid.arrange भूखंडों की अच्छी नियुक्ति की पेशकश नहीं करता है। – ahs85

+0

@ ग्रेगो। पैकेज wq (पानी की गुणवत्ता) में फ़ंक्शन layOut() अब प्रतीत नहीं होता है। क्या वो सही है? – lawyeR

+1

@lawyeR जाहिर है तो। मैंने अपने जवाब में कोड जोड़ा है। – Gregor