Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can achieve this particular plot also using just base plotting functions.</p> <pre><code>#Set alignment for tow plots. Extra zeros are needed to get space for axis at bottom. layout(matrix(c(0,1,2,0),ncol=1),heights=c(1,3,3,1)) #Set spaces around plot (0 for bottom and top) par(mar=c(0,5,0,5)) #1. plot plot(df$V2~df$TIME2,type="l",xlim=c(1000,2000),axes=F,ylab="") #Two rectangles - y coordinates are larger to ensure that all space is taken rect(1100,-15000,1300,15000,col="red",border="red") rect(1800,-15000,1850,15000,col="red",border="red") #plot again the same line (to show line over rectangle) par(new=TRUE) plot(df$V2~df$TIME2,type="l",xlim=c(1000,2000),axes=F,ylab="") #set axis axis(1,at=seq(800,2200,200),labels=NA) axis(4,at=seq(-15000,10000,5000),las=2) #The same for plot 2. rev() in ylim= ensures reverse axis. plot(df$VARIABLE1~df$TIME1,type="l",ylim=rev(range(df$VARIABLE1)+c(-0.1,0.1)),xlim=c(1000,2000),axes=F,ylab="") rect(1100,-15000,1300,15000,col="red",border="red") rect(1800,-15000,1850,15000,col="red",border="red") par(new=TRUE) plot(df$VARIABLE1~df$TIME1,type="l",ylim=rev(range(df$VARIABLE1)+c(-0.1,0.1)),xlim=c(1000,2000),axes=F,ylab="") axis(1,at=seq(800,2200,200)) axis(2,at=seq(-6.4,-8.4,-0.4),las=2) </code></pre> <p><img src="https://i.stack.imgur.com/COhqS.png" alt="enter image description here"></p> <h1>UPDATE - Solution with ggplot2</h1> <p>First, make two new data frames that contain information for rectangles.</p> <pre><code>rect1&lt;- data.frame (xmin=1100, xmax=1300, ymin=-Inf, ymax=Inf) rect2 &lt;- data.frame (xmin=1800, xmax=1850, ymin=-Inf, ymax=Inf) </code></pre> <p>Modified your original plot code - moved <code>data</code> and <code>aes</code> to inside <code>geom_line()</code>, then added two <code>geom_rect()</code> calls. Most essential part is <code>plot.margin=</code> in <code>theme()</code>. For each plot I set one of margins to <code>-1</code> line (upper for <code>p1</code> and bottom for <code>p2</code>) - that will ensure that plot will join. All other margins should be the same. For <code>p2</code> also removed axis ticks. Then put both plots together.</p> <pre><code>library(ggplot2) library(grid) library(gridExtra) p1&lt;- ggplot() + geom_line(data=df, aes(TIME1, VARIABLE1)) + scale_y_reverse() + labs(x="AGE") + scale_x_continuous(breaks = seq(1000,2000,200), limits = c(1000,2000)) + geom_rect(data=rect1,aes(xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax),alpha=0.1,fill="blue")+ geom_rect(data=rect2,aes(xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax),alpha=0.1,fill="blue")+ theme(plot.margin = unit(c(-1,0.5,0.5,0.5), "lines")) p2&lt;- ggplot() + geom_line(data=df, aes(TIME2, V2)) + labs(x=NULL) + scale_x_continuous(breaks = seq(1000,2000,200), limits = c(1000,2000)) + scale_y_continuous(limits=c(-14000,10000))+ geom_rect(data=rect1,aes(xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax),alpha=0.1,fill="blue")+ geom_rect(data=rect2,aes(xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax),alpha=0.1,fill="blue")+ theme(axis.text.x=element_blank(), axis.title.x=element_blank(), plot.title=element_blank(), axis.ticks.x=element_blank(), plot.margin = unit(c(0.5,0.5,-1,0.5), "lines")) gp1&lt;- ggplot_gtable(ggplot_build(p1)) gp2&lt;- ggplot_gtable(ggplot_build(p2)) maxWidth = unit.pmax(gp1$widths[2:3], gp2$widths[2:3]) gp1$widths[2:3] &lt;- maxWidth gp2$widths[2:3] &lt;- maxWidth grid.arrange(gp2, gp1) </code></pre> <p><img src="https://i.stack.imgur.com/RVQY6.png" alt="enter image description here"></p>
 

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