2013-12-17 5 views
10

ggplot का उपयोग करके, मैं पैनल के साथ एक ग्राफ टाइल का प्रतिनिधित्व करना चाहता हूं, लेकिन प्रत्येक पैनल के लिए समान ऊंचाई टाइल के साथ। मैं इस ग्राफ है:geom_tile और facet_grid/facet_wrap

dataSta <- list(sites=rep(paste("S", 1:31),each=12), month=rep(1:12,31), value=round(runif(31*12, min=0, max=3000)), panel=c(rep("Group 1",16*12),rep("Group 2", 12*12), rep("Group 3", 3*12))) 

    library(ggplot2) 
    library(grid) 
    base_size <- 9 

    windows() 
    ggplot(data.frame(dataSta), aes(factor(month), sites)) + 
     geom_tile(aes(fill = value), colour = "black")+ 
     facet_wrap(~panel, scale="free_y", nrow=3)+ 
     theme_grey(base_size = base_size) + 
     labs(x = "",y = "") + 
     scale_x_discrete(expand = c(0, 0)) +  
     scale_y_discrete(expand = c(0, 0)) +  
     theme(legend.title = element_blank(), 
      axis.ticks = element_blank(),  
      axis.text.x = element_text(size = base_size *0.8, hjust = 0), 
      panel.margin = unit(0,"lines"), 
      strip.text = element_text(colour="red3", size=10, face=2)) 

enter image description here

लेकिन टाइल्स की ऊंचाई पैनल के बीच अलग है। मैं facet_grid इस्तेमाल करने की कोशिश:

windows() 
ggplot(data.frame(dataSta), aes(factor(month), sites)) + 
    geom_tile(aes(fill = value), colour = "black")+ 
    facet_grid(panel~., scales="free_y", space="free")+ 
    theme_grey(base_size = base_size) + 
    labs(x = "",y = "") + 
    scale_x_discrete(expand = c(0, 0)) +  
    scale_y_discrete(expand = c(0, 0)) +  
    theme(legend.title = element_blank(), 
     axis.ticks = element_blank(),  
     axis.text.x = element_text(size = base_size *0.8, hjust = 0), 
     panel.margin = unit(0,"lines"), 
     strip.text = element_text(colour="red3", size=10, face=2)) 

enter image description here टाइल्स की ऊंचाई के साथ समस्या हल हो गई है, लेकिन पैनल के लेबल (समूह 1 ... समूह 3) पैनल के शीर्ष पर नहीं हैं। क्या facet_grid के साथ पैनल लेबल की स्थिति बदलना संभव है? या facet_grid और facet_wrap गठबंधन? आपकी मदद के लिए धन्यवाद, और मेरी अंग्रेजी के लिए खेद है!

+0

संभावित डुप्लिकेट: https://stackoverflow.com/a/32733035/7886302 – Undo

उत्तर

16

आप देख सकते हैं कि प्लॉटिंग से पहले क्या ggplot में शामिल है, और तदनुसार पैनलों को पुन: सहेज लें।

g <- ggplot_build(p) 
## find out how many y-breaks are in each panel 
## to infer the number of tiles 
vtiles <- sapply(lapply(g$panel$ranges, "[[", "y.major"), length) 

## convert the plot to a gtable object 
gt <- ggplot_gtable(g) 
## find out which items in the layout correspond to the panels 
## we refer to the "t" (top) index of the layout 
panels <- gt$layout$t[grepl("panel", gt$layout$name)] 
## replace the default panel heights (1null) with relative sizes 
## null units scale relative to each other, so we scale with the number of tiles 
gt$heights[panels] <-lapply(vtiles, unit, "null") 
## draw on a clean slate 
library(grid) 
grid.newpage() 
grid.draw(gt) 

enter image description here

+0

बहुत बहुत धन्यवाद, यह वही है जो मैं ढूंढ रहा हूं! यह बहुत अच्छा काम करता है! –

0

यह मेरे लिए आसान समाधान जो वास्तव में facet_grid जहां space = "free_y" सेट कर सकते हैं का हिस्सा है खोजने के लिए कुछ समय ले लिया। recent question पर अधिक जानकारी।

+0

यदि उसे उत्तर के रूप में रहना चाहिए, तो कृपया प्रश्न का जिक्र करते हुए एक पूर्ण समाधान पोस्ट करें और यहां अपने प्रतिनिधि का उपयोग करें और केवल किसी अन्य प्रश्न से लिंक न करें। उत्तरार्द्ध एक टिप्पणी में बेहतर किया जाता है। –

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