I have a ggplot2 line ggplot2 with an odd number of faces. I would like to add the square square of the marginal distribution of x values ββin empty space. The box should be horizontal and have a common x axis with other faces. Since boxplot geometry is vertical by default, coord_flip() is required. Because of this, I donβt think itβs possible to include boxplot data in the same df as the other faces using the factor factor variable for the cut.
Using grid , I can identify a blank viewport and insert boxplot, but I want the x axes to line up. Answers to such questions (see here or here ) suggest using align_plots in the ggExtra package ggExtra but I do not believe that this will work with faceting. I have included a simple reproducible example below. If I get this working, I will also have to edit the empty Grob panel to create a new label that matches the other faces. Any suggestions would be appreciated.
library(ggplot2) #generate df for faceted line graphs df <- data.frame(x = rep(1:100, times=7), facet_var = as.factor(rep( 1:7, each=100)), y = runif(7*100) ) #create faceted line graphs p <- ggplot(data = df, aes(x, y)) + geom_line() + facet_wrap( ~ facet_var, ncol=2) #generate df for boxplot xdata <- runif(1000, min = 0, max = 100) boxdf <- data.frame(x=xdata, group=rep(1,length(xdata))) #create boxplot removing axes and margins q <- ggplot(data = boxdf, aes(as.factor(group),x)) + geom_boxplot() + coord_flip() + labs(x=NULL) + opts(axis.text.x = theme_blank(), axis.title.x=theme_blank(), axis.text.y = theme_blank(), axis.title.y=theme_blank(), axis.ticks = theme_segment(colour = "white"), panel.margin = 0, plot.margin = unit(rep(0,4), "lines") ) print(p) downViewport("panel-14-5") print(q, newpage=F)
Edit: After kohske's helpful answer, I tried to adapt the code for different x constraints and breaks. Here is a code with only x constraints and interruptions changed for the range (0.80). Maybe I'm missing something in the code that needs to be changed with restrictions.
library(ggplot2) df <- data.frame(x = rep(1:80, times=7), facet_var = as.factor(rep( 1:7, each=80)), y = runif(7*80) ) # label for marginal plot df <- rbind(df, data.frame(x = NA, y = NA, facet_var = "Boxplot wow")) p <- ggplot(data = df, aes(x, y)) + geom_line() + facet_wrap( ~ facet_var, ncol=2) + # set limits for adjustment coord_cartesian(xlim = c(0, 80)) + #scale_x_continuous(breaks = 1:4*20) opts() xdata <- runif(1000, min = 0, max = 80) boxdf <- data.frame(x=xdata, group=rep(1,length(xdata))) q <- ggplot(data = boxdf, aes(as.factor(group),x)) + geom_boxplot() + # set breaks and limits for adjustment coord_flip(ylim = c(0, 80)) + labs(x=NULL) + scale_y_continuous(breaks = 1:4*20) + # opts for full region drawing: # see https://kohske.wordpress.com/2010/12/25/drawing-on-full-region-in-ggplot2/ opts( legend.position = "none", panel.margin = unit(0,"null"), plot.margin = rep(unit(0,"null"),4), axis.ticks = theme_blank(), axis.text.x = theme_blank(), axis.text.y = theme_blank(), axis.title.x = theme_blank(), axis.title.y = theme_blank(), axis.ticks.length = unit(0,"null"), axis.ticks.margin = unit(0,"null") ) print(p) # remove unused panel grid.remove("panel-14-5") downViewport("panel-14-5") print(q, newpage=F)
