2012-10-02 13 views
5

मुझे अधिकतर कॉस्मेटिक समस्या मिली है। मैं ggplot2 लाइब्रेरी का उपयोग करके चार भूखंड बना रहा हूं जिसे मैं एक कॉलम में व्यवस्थित करता हूं (का उपयोग करके)। ग्राफ एक ही डेटा प्रदर्शित करते हैं लेकिन चार समूहों के लिए, एक्स-अक्ष समय होता है इसलिए मैं ग्राफ को एक ही कॉलम में रखना चाहता हूं।ggplot2 (किंवदंती और अक्ष आकार बदलते हैं) का उपयोग करते हुए आर में लगातार ग्राफ आकार

तो मैं शीर्ष ग्राफ में किंवदंती जोड़ता हूं, और एक्स-अक्ष के लिए लेबल नीचे ग्राफ में जोड़ता हूं। ये दो क्रियाएं ग्राफ के आकार को बदलती हैं; एक किंवदंती जोड़ना ग्राफ को बढ़ने का कारण बनता है, एक्स-अक्ष लेबल जोड़कर इन चीजों को समायोजित करने के लिए इसे कम करने का कारण बनता है।

क्या एक निश्चित ग्राफ आकार निर्दिष्ट करने का कोई तरीका है, जो मेरा लेआउट सुसंगत बनाएगा?

मेरे भूखंड:

library(ggplot2) 
library(reshape) 

raw_data <- structure(list(Sample = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 
23L, 24L, 25L, 26L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 
24L, 25L, 26L), Month = structure(c(12L, 12L, 11L, 11L, 10L, 
10L, 3L, 3L, 5L, 5L, 4L, 4L, 8L, 8L, 1L, 1L, 9L, 9L, 7L, 7L, 
6L, 6L, 2L, 2L, 12L, 12L, 12L, 12L, 11L, 11L, 10L, 10L, 3L, 3L, 
5L, 5L, 4L, 4L, 8L, 8L, 1L, 1L, 9L, 9L, 7L, 7L, 6L, 6L, 2L, 2L, 
12L, 12L), .Label = c("April", "Aug", "Dec", "Feb", "Jan", "July", 
"June", "March", "May", "Nov", "Oct", "Sep"), class = "factor"), 
    Channel = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("A", 
    "B"), class = "factor"), Amplitude = c(5000L, 
    5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 
    5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 
    5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 
    5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L, 5000L)), .Names = c("Sample", 
"Month", "Channel", "Amplitude"), row.names = c(NA, 52L), class = "data.frame") 



multiplot <- function(..., plotlist=NULL, cols) { 
    require(grid) 

    # Make a list from the ... arguments and plotlist 
    plots <- c(list(...), plotlist) 

    numPlots = length(plots) 

    # Make the panel 
    plotCols = cols       # Number of columns of plots 
    plotRows = ceiling(numPlots/plotCols) # Number of rows needed, calculated from # of cols 

    # Set up the page 
    grid.newpage() 
    pushViewport(viewport(layout = grid.layout(plotRows, plotCols))) 
    vplayout <- function(x, y) { 
      viewport(layout.pos.row = x, layout.pos.col = y) 
    } 

    # Make each plot, in the correct location 
    for (i in 1:numPlots) { 
     curRow = ceiling(i/plotCols) 
     curCol = (i-1) %% plotCols + 1 
     print(plots[[i]], vp = vplayout(curRow, curCol)) 
    } 

} 


mybarplot <- function(first=0, last=0) { 
    # Create the barplot 
    p <- ggplot(raw_data, aes(x=Sample, y=Amplitude, fill=Channel)) 

    # Make it a grouped barplot with already summarised values 
    p <- p + geom_bar(position="dodge", stat="identity") 


    # Apply a log10 transformation to the y-axis, and create appropriate axis ticks 
    p <- p + scale_y_log10(breaks = c(5,10,50,100,500,1000,5000,10000)) 

    # Zoom in (barplots will not show when axis change to remove 0, so have to zoom) 
    p <- p + coord_cartesian(ylim=c(1,15000), xlim=c(1,26)) 

    # Make it greyscale 
    p <- p + scale_fill_grey() 


    # Hide X label 
    p <- p + opts(axis.text.x=theme_blank(), axis.title.x=theme_blank(), axis.title.y=theme_blank()) 
    # Change X label size 
    p <- p + opts(axis.text.y=theme_text(size=7)) 



    # Change the Legend 
    p <- p + scale_fill_manual(values=c("black", "grey75", "grey25"), name="Channel", breaks=c("A", "B")) 

    #margins 
    # c(top,,bottom,) 
    top_margin <- unit(c( 1, 1, -0.25, 1), "lines") 
    middle_margin <- unit(c(-0.25, 1, -0.25, 1), "lines") 
    bottom_margin <- unit(c(-0.25, 1,  2, 1), "lines") 


    if (first) { 
     # Anchor legend box to top right corner 
     p <- p + opts(legend.justification=c(1,1), legend.position=c(1,1)) 
     # Put a white box around it 
     p <- p + opts(legend.background = theme_rect(fill="white")) 
     # Top margin 
     p <- p + opts(plot.margin = top_margin) 
     p <- p + scale_x_discrete(breaks = 1:26) 
    } else { 
     p <- p + opts(legend.position="none") 
     if (last) { 
      # Bottom margin 
      p <- p + opts(plot.margin = bottom_margin) 
       # label X-axis 
      p <- p + scale_x_discrete(breaks = 1:26, labels=c("Sep", "", "Oct", "", "Nov", "", "Dec", "", "Jan", "", "Feb", "", "March", "", "April", "", "May", "", "June", "", "July", "", "Aug", "", "Sep", "")) 

      p <- p + ylab("Amplitude") 
      p <- p + xlab("Sampling time") 
      # Angle x labels 
      #p <- p + opts(axis.text.x=theme_text(angle=-45, hjust=0.5)) 
      p <- p + opts(axis.text.x=theme_text(hjust=0.5)) 

      # Move X title 
      p <- p + opts(axis.title.x=theme_text(vjust=-0.5)) 
     } else { 
      p <- p + opts(plot.margin = middle_margin) 
      p <- p + scale_x_discrete(breaks = 1:26) 
     } 
    } 



} 


plot1 <- mybarplot(first=1) 
plot2 <- mybarplot() 
plot3 <- mybarplot() 
plot4 <- mybarplot(last=1) 

multiplot(plot1, plot2, plot3, plot4, cols=1) 

सत्र की जानकारी: plot

प्रतिलिपि प्रस्तुत करने योग्य परिणामों के लिए कोड

> sessionInfo() 
R version 2.15.1 (2012-06-22) 
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit) 

locale: 
[1] C 

attached base packages: 
[1] grid  stats  graphics grDevices utils  datasets methods base  

other attached packages: 
[1] reshape_0.8.4 plyr_1.7.1 ggplot2_0.9.1 

loaded via a namespace (and not attached): 
[1] MASS_7.3-18  RColorBrewer_1.0-5 colorspace_1.1-1 dichromat_1.2-4 digest_0.5.2  labeling_0.1  memoise_0.1  munsell_0.3  proto_0.3-9.2  reshape2_1.2.1  
[11] scales_0.2.1  stringr_0.6.1  
+0

क्या आप वह कोड दिखा सकते हैं जिसका उपयोग आपने साजिश बनाने के लिए किया था ताकि आपका प्रश्न [पुनरुत्पादित] हो सके (http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)। – Justin

+0

इसके बारे में क्षमा करें। इसे पुन: उत्पन्न करने के लिए कोड जोड़ा गया। – NFA

उत्तर

5

अपने उदाहरण प्रत्येक भूखंड समान है में, हालांकि मुझे लगता है कि नहीं है आपके अंतिम उत्पाद की योजना। मुझे लगता है कि ऐसा करने का सबसे आसान तरीका प्रत्येक साजिश को अलग से अलग करने के बजाय पहलू के साथ है।

dat <- data.frame(facetvar=letters[1:5], yvar=rep(1:10, each=5), xvar=rep(letters[6:10], each=5)) 
ggplot(dat, aes(x=xvar, y=yvar, group=facetvar)) + 
    geom_bar(stat='identity') + 
    facet_grid(facetvar~.) 

यदि आवश्यकता हो तो आप अपने डेटा को पहले से सब्सक्राइब कर सकते हैं और मनमाने ढंग से फ़ेसिंग चर का उपयोग कर सकते हैं।

ggplot(dat[sample(1:50, 40),], aes(x=xvar, y=yvar, group=facetvar)) + 
    geom_bar(stat='identity') + 
    facet_grid(facetvar~.) 

आवश्यकता होने पर आप facet_grid() करने के लिए scales.y='free' आपूर्ति कर सकते हैं।

+0

हाँ, डेटा मेरे उदाहरण में समान है, लेकिन यह अंतिम ग्राफ में नहीं होगा। यही कारण था क्योंकि यह इसे पुन: उत्पन्न करने का सबसे तेज़ तरीका था। आकार अंतर अभी भी है। पहलुओं का उपयोग करने के लिए मुझे अपने सभी डेटा को 1 फ्रेम में रखना होगा, है ना? जैसा कि अब है, मुझे 4 अलग-अलग फाइलों से डेटा मिलता है और मुझे पहले डेटा को ट्रांसफर और पिघलने की ज़रूरत है। मैं इसे कल काम पर करने में देखूंगा, लेकिन मुझे डर है कि उन्हें एक ही डेटाफ्रेम में समेकित करना मुश्किल होगा। – NFA

+0

आप सही हैं, पहलू के लिए आपको एक 'data.frame' की आवश्यकता है। हालांकि, आपके पिघलने के चरणों के बाद आप अक्सर मनमानी "पहलू" कॉलम जोड़ सकते हैं और उन सभी को मैश करने के लिए 'rbind()' जैसे कुछ का उपयोग कर सकते हैं। – Justin

+1

यह बिल्कुल मुश्किल नहीं होना चाहिए। चार मानसेटों में प्रतिक्रियाओं और भविष्यवाणियों के स्तंभों को समान मूल्यों में बदलें, प्रत्येक को एक कॉलम जोड़ें जो पहलू आईडी ('rep (आईडी, लंबाई (डेटा)) के माध्यम से देता है, उन्हें बाध्य करें, फिर एक पहलू विवरण – Chris

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