Home > Net >  Altering facet_grid() height only for one facet (row) is it possible?
Altering facet_grid() height only for one facet (row) is it possible?

Time:03-19

I'm trying to improve a simple but important detail in my facet_grid graphic.

I played with scale and space parameteres of facet_grid() function. But I would like to increase the height only for the bottom facet in order to be possible to read the sample count that appears cut off in the graph (highlighted in red). See Image1. The code used is:

    ggplot(salm_data, aes(x=SALM) )   geom_bar(stat = "count", position = position_dodge())
   facet_grid(YEAR ~ TYPE, scales = "free_x", space = "free") 
    geom_text(size=2.5, stat = "count", aes(label = after_stat(count)), vjust = -0.3)  theme_bw()   labs(y="Number of samples")   
theme(panel.grid.minor = element_blank()) 
 labs(x=expression(paste(italic("Klebsiella spp."), " ")))

Image 1: enter image description here Sample data:

salm_data<-structure(list(type = c("A", "B", "C", "D", "A", "B", "C", "D", 
"A", "B", "C", "D", "A", "C", "A", "B", "C", "D", "A", "B", "C", 
"D", "A", "B", "C", "D", "A", "C", "A", "B", "C", "D", "A", "B", 
"C", "D", "A", "B", "C", "D", "A", "C", "A", "B", "C", "D", "A", 
"B", "C", "D", "A", "B", "C", "D", "A", "C", "A", "B", "C", "D", 
"A", "B", "C", "D", "A", "B", "C", "D", "A", "C", "A", "B", "C", 
"D", "A", "B", "C", "D", "A", "B", "C", "D", "A", "C", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A"
), year = c(2001, 2001, 2001, 2001, 2001, 2002, 2001, 2002, 2001, 
2002, 2001, 2002, 2002, 2002, 2001, 2001, 2001, 2001, 2001, 2002, 
2001, 2002, 2001, 2002, 2001, 2002, 2002, 2002, 2001, 2001, 2001, 
2001, 2001, 2002, 2001, 2002, 2001, 2002, 2001, 2002, 2002, 2002, 
2001, 2001, 2001, 2001, 2001, 2002, 2001, 2002, 2001, 2002, 2001, 
2002, 2002, 2002, 2001, 2001, 2001, 2001, 2001, 2002, 2001, 2002, 
2001, 2002, 2001, 2002, 2002, 2002, 2001, 2001, 2001, 2001, 2001, 
2002, 2001, 2002, 2001, 2002, 2001, 2002, 2002, 2002, 2001, 2001, 
2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 
2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 
2001, 2001, 2001, 2001)), class = "data.frame", row.names = c(NA, 
-112L))

CodePudding user response:

Many thanks @Rfanatic. That's just the thing that I desired.

I create the gtable from package grid as you indicated but I have used also the library gtable to help me to discover the panel codification for my case. Here's the code if can also help someone in the future:

gt <- ggplot_gtable(ggplot_build(gg))
gt$widths[14] = 1*gt$widths[14]    
gt$heights[14] = 4*gt$heights[14]  
grid.draw(gt)

I use the position 14 because it was the row position number for the square that I have problems:

library(gtable)
gtable_show_layout(gt)

But the best for me it has been print the TableGrob:

print(gt)
#17 1 (14-14,11-11) panel-4-4 gTree[panel-16.gTree.8635]

I was interested in panel-4-4 in my example (only I printed here this line of the gt output). Moreover, due to I want to change the row height and width, I pick up 14 instead of 11.

Thanks .

  • Related