geom_tile and facet_grid/facet_wrap for same height of tiles
Asked Answered
G

3

14

Using ggplot, I would like represent a graph tile with panel, but with same height tile for each panel. I have this graph :

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

But height of tiles is different between panel. I try to use 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 The problem with height of tiles is resolved, but labels of panel (Group 1 ... Group 3) are not on top of panel. Is it possible to change position of panel labels with facet_grid ? or combine facet_grid and facet_wrap ? Thanks for your help, and sorry for my English !

Giffy answered 17/12, 2013 at 15:36 Comment(1)
G
18

You can look at what ggplot contains before plotting, and rescale the panels accordingly.

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

Globular answered 17/12, 2013 at 16:26 Comment(1)
Thank you very much, it's exactly what I'm looking for! It's works very well!Giffy
T
1

It took me some time to find easier solution which is actually part of the facet_grid where you can set the space = "free_y". More info at recent question.

Teflon answered 24/7, 2017 at 17:46 Comment(2)
If that should stay as an answer, please post a full solution referring to the question and using its reprex here and do not just link to another question. The latter is better done in a comment.Furnace
The OP wanted the facet labels on top of each panel, facet_grid will not allow you to do this.Chromoprotein
C
0

The ggforce package has a neat little function for this called facet_col. It requires no messing around with the grid package!

All you have to do is replace the call to facet_grid with the appropriate facet_col alternative:

library(ggplot2)
library(ggforce)

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)))

base_size <- 9

ggplot(data.frame(dataSta), aes(factor(month), sites)) + 
  geom_tile(aes(fill = value), colour = "black") +

  # Here's the line to alter:
  facet_col(vars(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.spacing = unit(0,"lines"),
        strip.text = element_text(colour="red3", size=10, face=2))

enter image description here

Chromoprotein answered 16/6, 2021 at 12:47 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.