Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a solution inspired by this <a href="https://stackoverflow.com/questions/11883844/inserting-a-table-under-the-legend-in-a-ggplot2-histogram">question</a></p> <p>First, made new variable <code>colvec2</code> that contains all colors only once.</p> <pre><code>colvec2 &lt;-c("white", "gray80", "gray60", "gray37", "black") colvec &lt;-c("white", "white","gray80", "gray80", "gray60", "gray60", "gray37", "gray37","black", "black") </code></pre> <p>Original plot saved as object and without the legend.</p> <pre><code>p&lt;-ggplot(water, aes(Date, Temp, group=Wetland, shape=Hydrology)) + geom_point(aes(color=Wetland),size=3) + scale_colour_manual(values=colvec) + facet_grid(Layer ~ Hydrology) + theme(legend.position="none") </code></pre> <p>Plot that contains only data of <code>Pulsed</code>. Set <code>shape=16</code> inside the <code>geom_point()</code>. With <code>scale_colour_manual()</code> changed legend title and colors.</p> <pre><code>p1&lt;-ggplot(subset(water,Hydrology=="Pulsed"), aes(Date, Temp, group=Wetland, shape=Hydrology)) + geom_point(aes(color=Wetland),size=3,shape=16) + scale_colour_manual("Pulsed Wetlands",values=colvec2) + facet_grid(Layer ~ Hydrology) </code></pre> <p>The same as <code>p1</code>, only for <code>Static</code>.</p> <pre><code>p2&lt;-ggplot(subset(water,Hydrology=="Static"), aes(Date, Temp, group=Wetland, shape=Hydrology)) + geom_point(aes(color=Wetland),size=3,shape=17) + scale_colour_manual("Static Wetlands",values=colvec2) + facet_grid(Layer ~ Hydrology) </code></pre> <p>Save plots <code>p1</code> and <code>p2</code> using <code>ggplot_build()</code> and <code>ggplot_gtable()</code>.</p> <pre><code>pt1&lt;-ggplot_gtable(ggplot_build(p1)) pt2&lt;-ggplot_gtable(ggplot_build(p2)) </code></pre> <p>Extract just grob of legends for both plots. This time legends are in 12. element (element containing <code>guide-box</code>)</p> <pre><code>leg1&lt;-pt1$grobs[[12]] leg2&lt;-pt2$grobs[[12]] </code></pre> <p>Now use <code>grid.arrange()</code> and <code>arrangeGrob()</code> to plot all parts.</p> <pre><code>library(gridExtra) grid.arrange(arrangeGrob(p,arrangeGrob(leg1,leg2), ncol = 2,widths=c(3/4,1/4))) </code></pre> <p><img src="https://i.stack.imgur.com/PWowB.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