Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is the example for combining multiple plots with alignment:</p> <pre><code>library(ggplot2) library(grid) set.seed (123) xvar &lt;- c(rnorm (100, 50, 30), rnorm (100, 40, 10), rnorm (100, 70, 10)) yvar &lt;- xvar + rnorm (length (xvar), 0, 20) myd &lt;- data.frame (xvar, yvar) p1 &lt;- ggplot(myd,aes(x=xvar,y=yvar))+ stat_density2d(aes(fill=..level..), geom="polygon") + coord_cartesian(c(0, 150), c(0, 150)) + opts(legend.position = "none") p2 &lt;- ggplot(myd, aes(x = xvar)) + stat_density() + coord_cartesian(c(0, 150)) p3 &lt;- ggplot(myd, aes(x = yvar)) + stat_density() + coord_flip(c(0, 150)) gt &lt;- ggplot_gtable(ggplot_build(p1)) gt2 &lt;- ggplot_gtable(ggplot_build(p2)) gt3 &lt;- ggplot_gtable(ggplot_build(p3)) gt1 &lt;- ggplot2:::gtable_add_cols(gt, unit(0.3, "null"), pos = -1) gt1 &lt;- ggplot2:::gtable_add_rows(gt1, unit(0.3, "null"), pos = 0) gt1 &lt;- ggplot2:::gtable_add_grob(gt1, gt2$grobs[[which(gt2$layout$name == "panel")]], 1, 4, 1, 4) gt1 &lt;- ggplot2:::gtable_add_grob(gt1, gt2$grobs[[which(gt2$layout$name == "axis-l")]], 1, 3, 1, 3, clip = "off") gt1 &lt;- ggplot2:::gtable_add_grob(gt1, gt3$grobs[[which(gt3$layout$name == "panel")]], 4, 6, 4, 6) gt1 &lt;- ggplot2:::gtable_add_grob(gt1, gt3$grobs[[which(gt3$layout$name == "axis-b")]], 5, 6, 5, 6, clip = "off") grid.newpage() grid.draw(gt1) </code></pre> <p><img src="https://i.stack.imgur.com/TL3w0.png" alt="enter image description here"></p> <p>note that this works with gglot2 0.9.1, and in the future release you may do it more easily.</p> <p><em>And finally</em></p> <p>you can do that by:</p> <pre><code>library(ggplot2) library(grid) set.seed (123) xvar &lt;- c(rnorm (100, 50, 30), rnorm (100, 40, 10), rnorm (100, 70, 10)) yvar &lt;- xvar + rnorm (length (xvar), 0, 20) myd &lt;- data.frame (xvar, yvar) p1 &lt;- ggplot(myd,aes(x=xvar,y=yvar))+ stat_density2d(aes(fill=..level..), geom="polygon") + geom_polygon(aes(x, y), data.frame(x = c(-Inf, -Inf, 30, 30), y = c(-Inf, 30, 30, -Inf)), alpha = 0.5, colour = NA, fill = "red") + geom_polygon(aes(x, y), data.frame(x = c(Inf, Inf, 80, 80), y = c(Inf, 80, 80, Inf)), alpha = 0.5, colour = NA, fill = "green") + coord_cartesian(c(0, 120), c(0, 120)) + opts(legend.position = "none") xd &lt;- data.frame(density(myd$xvar)[c("x", "y")]) p2 &lt;- ggplot(xd, aes(x, y)) + geom_area(data = subset(xd, x &lt; 30), fill = "red") + geom_area(data = subset(xd, x &gt; 80), fill = "green") + geom_line() + coord_cartesian(c(0, 120)) yd &lt;- data.frame(density(myd$yvar)[c("x", "y")]) p3 &lt;- ggplot(yd, aes(x, y)) + geom_area(data = subset(yd, x &lt; 30), fill = "red") + geom_area(data = subset(yd, x &gt; 80), fill = "green") + geom_line() + coord_flip(c(0, 120)) gt &lt;- ggplot_gtable(ggplot_build(p1)) gt2 &lt;- ggplot_gtable(ggplot_build(p2)) gt3 &lt;- ggplot_gtable(ggplot_build(p3)) gt1 &lt;- ggplot2:::gtable_add_cols(gt, unit(0.3, "null"), pos = -1) gt1 &lt;- ggplot2:::gtable_add_rows(gt1, unit(0.3, "null"), pos = 0) gt1 &lt;- ggplot2:::gtable_add_grob(gt1, gt2$grobs[[which(gt2$layout$name == "panel")]], 1, 4, 1, 4) gt1 &lt;- ggplot2:::gtable_add_grob(gt1, gt2$grobs[[which(gt2$layout$name == "axis-l")]], 1, 3, 1, 3, clip = "off") gt1 &lt;- ggplot2:::gtable_add_grob(gt1, gt3$grobs[[which(gt3$layout$name == "panel")]], 4, 6, 4, 6) gt1 &lt;- ggplot2:::gtable_add_grob(gt1, gt3$grobs[[which(gt3$layout$name == "axis-b")]], 5, 6, 5, 6, clip = "off") grid.newpage() grid.draw(gt1) </code></pre> <p><img src="https://i.stack.imgur.com/dBYgM.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload