Replacing a panel in ggplot2

72 views
Skip to first unread message

Kim

unread,
May 6, 2013, 6:24:35 PM5/6/13
to ggp...@googlegroups.com
Dear group,
 
I have been using this code of kohske's to align ggplot2 axes using grid:
 
 
However, we had a UNIX/R update at work and now the code is not working.  The R version we are now using is 2.15.2 with ggplot2_0.9.3.
 
The part of the code that is not working is replacing the removed panel.  I re-ran Kohske's code but replaced
grid.remove("panel-14-5")
with
grid.remove("panel-8.16-7-16-7")
which I determined using
sapply(grid.gget(^panel-",grep=TRUE),"[[","name").
 
However, when I try to replace
downViewport("panel-14-5")
with
downViewport(("panel-8.16-7-16-7")
I get the error
Error in grid.Call.graphics(L_downviewport, name$name, strict):
Viewport 'panel-8.16-7-16-7' was not found.
 
Any suggestions on how I can successful replace the removed panel would be greatly appreciated.
 
Thank you so much for your help.
 
Kim

Kim

unread,
May 7, 2013, 9:15:32 AM5/7/13
to ggp...@googlegroups.com
Dear group,
 
As a follow-up, here is a reproducible example (Kohske's code - thanks Dennis).
 
library(ggplot2)
df <- data.frame(x = rep(1:100, times=7), facet_var = as.factor(rep( 1:7, each=100)), y = runif(7*100) )
 
# 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, 100))
 
xdata <- runif(1000, min = 20, 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, 100)) + labs(x=NULL) + scale_y_continuous(breaks = 1:5*20) +    
#opts for full region drawing:    
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)
 
It is the three bottom lines that do not work with R version 2.15.2 with ggplot2_0.9.3.  I have replaced them with
grid.remove("panel-8.16-7-16-7")
downViewport("panel-8.16-7-16-7")
 
but then I get the error
Error in grid.Call.graphics(L_downviewport, name$name, strict):
Viewport 'panel-8.16-7-16-7' was not found.
 
Any ideas as to how to successfully replace the panel?  Thanks in advance for your help!
Kim
Reply all
Reply to author
Forward
0 new messages