0

@Ricardo Saporta has proposed here an easy way to combine base graphics and ggplot graphics in a multiple plots figure.

I use this way to plot a base graphic at left and a ggplot graphic at right which actually contains two graphics (this is an artifical example, not a real one, but it has the same structure as my real example) :

library(ggplot2)
library(gridExtra)
library(plotrix)

Mvalues <- matrix(rpois(9,1), ncol=3)
Mcolors <- matrix(rpois(9,5), ncol=3)

par(mfrow=c(1,2))
color2D.matplot(x=Mvalues, show.values=2, cellcolors=Mcolors, 
    xlab="x", ylab="y", axes=FALSE, vcex=0.4)
gg2 <- ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar(position="dodge") 
ta <- do.call(arrangeGrob, list(gg2,gg2))
vp.Left <- viewport(height=unit(1, "npc"), width=unit(0.5, "npc"), 
    just="left", y=0.5, x=0.5)
print(ta, vp=vp.Left)

plot

Very nice. But now I want the base graphic to have a larger width than the ggplot graphics. How to do ? I have unsuccessfully tried to do so with the layout() function.

Community
  • 1
  • 1
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
  • I don't think that there is an easy way to do it. You can play with the vp width and size. otherwise you can see this [question](http://stackoverflow.com/questions/14231321/make-panels-with-same-margins-when-combining-ggplot-and-base-graphics). but here he implements the first solution of the link that you mentioned. – agstudy Jan 16 '13 at 12:38
  • @agstudy It's easier than you think. See my answer. – Andrie Jan 16 '13 at 12:46
  • @Andrie Thank. I miss-understood the problem. I thought he wants the same plot region size. It is easier than I thought. – agstudy Jan 16 '13 at 12:53

1 Answers1

4

Here you go:

Use layout with different widths. Notice how I define the first column to be double the width of column 2:

layout(matrix(c(1, 2, 1, 3), ncol=2, byrow=TRUE), widths=c(2, 1))

Set up the graphics

layout(matrix(c(1, 2, 1, 3), ncol=2, byrow=TRUE), widths=c(2, 1))
color2D.matplot(x=Mvalues, show.values=2, cellcolors=Mcolors, 
                                xlab="x", ylab="y", axes=FALSE, vcex=0.4)
gg2 <- ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar(position="dodge") 
ta <- do.call(arrangeGrob, list(gg2,gg2))

Now define the viewport. Your code was almost there. I simply modified it to be right justified, and changed the width to be 33%:

vp <- viewport(height=unit(1, "npc"), width=unit(0.33, "npc"), 
               just="right", x=1, y=0.5)

Finally, print the remaining graphic:

print(ta, vp=vp)

enter image description here

Andrie
  • 176,377
  • 47
  • 447
  • 496
  • Thanks! This is exactly the way I tried, but it was unsuccessful. I do not master `layout()` neither `viewport`, surely I did something wrong. – Stéphane Laurent Jan 16 '13 at 12:49
  • @Andrie I wonder if you can get the same result using only Layout and keeping the viewport with its defaults arguments? – agstudy Jan 16 '13 at 12:54
  • Andrie, please I do not understand the matrix argument and the width argument of the `layout()` function. For instance if I want the first column to be 60% of the overall width and the second column to be 40%, what is the matrix and the width to set ? – Stéphane Laurent Jan 16 '13 at 13:00
  • The matrix argument only controls the plot order of the graphs, not the widths. Use the widhts argument for the, ahem, widths. – Andrie Jan 16 '13 at 13:01
  • @Andrie Sorry I have just edited my comment. So I put widths=c(6,4) ? – Stéphane Laurent Jan 16 '13 at 13:03
  • Sure, or `widths=c(3, 2)` or `widths=c(60, 40)`, which should all come to the same thing. – Andrie Jan 16 '13 at 13:04
  • @Andrie Ok thanks that works well. Although the figure is a little outside the window with my real example, but I think I'll find a way to manage. – Stéphane Laurent Jan 16 '13 at 13:09