Advertisement
Guest User

Untitled

a guest
Jul 24th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. gLegend <- function(a.gplot){
  2. tmp <- ggplot_gtable(ggplot_build(a.gplot))
  3. leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  4. legend <- tmp$grobs[[leg]]
  5. return(legend)
  6. }
  7.  
  8. set.seed(1)
  9. main.df <- data.frame(group=rep(LETTERS[1:4],3),
  10. y=rnorm(12),x=c(rep(1,4),rep(2,4),rep(3,4)),col=rep(c("gray","blue","red","magenta"),3))
  11. main.df$group <- factor(main.df$group,levels=LETTERS[1:4])
  12. sub.df <- data.frame(group=c("B","C","D"),x=1:3,effect=runif(3,0,1),col=c("blue","red","magenta"))
  13. sub.df$group <- factor(sub.df$group,levels=LETTERS[2:4])
  14.  
  15. main.plot <- ggplot(main.df,aes(x=x,y=y,color=factor(group)))+geom_point(size=3)+facet_wrap(~group,ncol=4)+scale_fill_manual(values=c("gray","blue","red","magenta"),labels=c("A","B","C","D"),name="group")+scale_colour_manual(values=c("gray","blue","red","magenta"),labels=c("A","B","C","D"),name="group")+scale_x_continuous(breaks=unique(main.df$x))
  16. sub.plot <- ggplot(sub.df,aes(x=x,y=effect,color=factor(group)))+geom_point(size=2)+scale_fill_manual(values=c("blue","red","magenta"),labels=c("B","C","D"),name="group",guide=FALSE)+scale_colour_manual(values=c("blue","red","magenta"),labels=c("B","C","D"),name="group",guide=FALSE)+labs(x="group",y="effect")+ggtitle("effect summary")+scale_x_continuous(breaks=unique(effects.df$x),labels=c("B","C","D"))
  17.  
  18. sub.plot.grob <- ggplotGrob(sub.plot)
  19. combined.plot <- arrangeGrob(main.plot+theme(legend.position="none"),widths=c(0.75,0.25),arrangeGrob(20,sub.plot.grob),ncol=2)
  20.  
  21. Error in tmp$grobs[[leg]] :
  22. attempt to select less than one element in get1index
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement