Home > Blockchain >  Multiple row variables for facet_grid - setting custom widths
Multiple row variables for facet_grid - setting custom widths

Time:08-25

I am trying to use ggplot to replicate the sparkline feature in excel, where you can put tiny little graphs inside a table. For my purposes, I would like to have the graphs replace the data values in the table (to save space) and I would like to be able to label the x and y axes of the sparkline graphs. So I turn to ggplot.

Here is the sample data:

labelvals <- data.frame(date=rep(c(2018, 2019), each=10), 
                        trial=1:20, 
                        material=sample(c("granite layer", "feldspar topper", "quartz base"), size=20, replace=TRUE ))

plottingdat <- expand.grid(trial=1:20, 
                           market=c("NY", "CA", "CHI", "FLA"), 
                           month=c("May", "June", "July", "August"),
                           rep=1:10)%>%
               left_join(labelvals)%>%
               mutate(test.result=rbinom(nrow(.), size=1, prob=.5))

The left section of this figure needs to act as a table. In my sample data there are 3 columns, but in the real thing there are 5 columns. I was overjoyed to find out that if you put multiple variables before the ~ in facet_grid, it will have my desired result- it will make a table of unique combos of the variables and align them nicely in a merged set of row layers.


ggplot(plottingdat, aes(x=month, y=test.result)) 
  geom_bar(stat="summary", fun="sum")     
  scale_y_continuous(breaks=c(0, 5, 10), labels=c("0", "0.5", "1")) 
  facet_grid(date  trial material~ market, switch = "y", scales="free_x") 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
       strip.text.y.left = element_text(angle = 0), 
         strip.text.y = element_text(margin = margin(1,1,1,1)),        
        strip.placement = "outside") 

enter image description here

This works well, but I'd like to adjust the width of the labels in the strips- each of the variables gets equal width, both the material variable which can be a dozen characters wide (and getting cut off), and the trial variable which is a two-character variable.

Is there a way we can make some more room on this for the graphs in the panels by adjusting the width of the strips for the different variables?

From this question, I thought maybe the answer lay in setting the margins for the strip.text.y argument in a theme, but they seem to apply to all variables at once.

CodePudding user response:

With ggh4x::facet_grid2(), you can set the strip so that it allows a variable size of the text.

Disclaimer: I'm the author of ggh4x.

library(ggplot2)
library(dplyr)
library(ggh4x)

labelvals <- data.frame(date=rep(c(2018, 2019), each=10), 
                        trial=1:20, 
                        material=sample(c("granite layer", "feldspar topper", "quartz base"), size=20, replace=TRUE ))

plottingdat <- expand.grid(trial=1:20, 
                           market=c("NY", "CA", "CHI", "FLA"), 
                           month=c("May", "June", "July", "August"),
                           rep=1:10)%>%
  left_join(labelvals)%>%
  mutate(test.result=rbinom(nrow(.), size=1, prob=.5))
#> Joining, by = "trial"

ggplot(plottingdat, aes(x=month, y=test.result)) 
  geom_bar(stat="summary", fun="sum")     
  scale_y_continuous(breaks=c(0, 5, 10), labels=c("0", "0.5", "1")) 
  facet_grid2(date  trial material~ market, switch = "y", scales="free_x",
             strip = strip_vanilla(size = "variable")) 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        strip.text.y.left = element_text(angle = 0), 
        strip.text.y = element_text(margin = margin(1,1,1,1)),        
        strip.placement = "outside")

Created on 2022-08-17 by the reprex package (v2.0.1)

  • Related