2013-12-17 17 views
10

Utilizzando ggplot, vorrei rappresentare un riquadro grafico con pannello, ma con la stessa altezza del riquadro per ogni pannello. Ho questo grafico:geom_tile e facet_grid/facet_wrap per la stessa altezza di piastrelle

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

Ma l'altezza delle piastrelle è diverso tra il pannello. Io cerco di usare 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 Il problema con l'altezza delle piastrelle è stato risolto, ma le etichette di panel (gruppo 1 ... Gruppo 3) non sono sulla parte superiore del pannello. È possibile cambiare la posizione delle etichette del pannello con facet_grid? o combinare facet_grid e facet_wrap? Grazie per il vostro aiuto, e mi dispiace per il mio inglese!

+0

Possibile duplicato: https://stackoverflow.com/a/32733035/7886302 – Undo

risposta

16

Puoi vedere cosa contiene ggplot prima di tracciare e ridimensionare i pannelli di conseguenza.

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

Grazie mille, è esattamente quello che sto cercando! Funziona molto bene! –

0

Mi c'è voluto un po 'di tempo per trovare la soluzione più semplice che è in realtà parte del facet_grid dove è possibile impostare la space = "free_y". Maggiori informazioni allo recent question.

+0

Se ciò dovesse rimanere una risposta, si prega di inviare una soluzione completa facendo riferimento alla domanda e utilizzando il suo reprex qui e non limitarsi a collegarsi ad un'altra domanda. Quest'ultimo è fatto meglio in un commento. –

Problemi correlati