title: Two plots same x axis, two y axis - how to arrange plots in a grid
body: <p>I am trying to make a graph like <a href="https://coinmarketcap.com/currencies/bitcoin/#charts" rel="nofollow noreferrer">this one</a>, using <code>ggplot2</code> and <code>gridExtra</code> packages, but my two plots are not properly aligned one under the other. My dataset is the historical data for bitcoin from coinmarketcap.</p>
  
  <pre><code>library(ggplot2)
  library(scales)
  library(gridExtra)
  
  bitc$Date &lt;- as.Date(bitc$Date,"%m/%d/%Y")
  bitc$Close &lt;- bitc$Close/1000
  bitc$Market_Cap &lt;- bitc$Market_Cap/1000000000
  bitc$Volume &lt;- bitc$Volume/1000000000
  
  
  q &lt;- ggplot(bitc,aes(bitc$Date))+
    geom_line(aes(y=bitc$Close,colour="Close"))+
    theme_bw() +
    theme(plot.margin = unit(c(1,5,-30,6),units="points"),
          axis.title.y = element_text(vjust =0.25)) +
    scale_x_date(breaks=date_breaks("6 months"))+
    xlab("Timp")+
    ylab("Price (thous. USD)")
  
  q &lt;- q + geom_line(aes(y=bitc$Market_Cap/10,colour="Market_Cap"))
  q &lt;- q + scale_y_continuous(sec.axis = sec_axis(~.*10, name = "Market Cap (bld. USD)"))
  q
  
  p &lt;- ggplot(bitc, aes(bitc$Date))+
    theme_bw() +
    theme(plot.margin = unit(c(0,5,1,1),units="points")) +
    geom_line(aes(y=bitc$Volume, colour="Volum"))+
    scale_x_date(breaks=date_breaks("6 months"))+
    scale_color_manual(values="green")+
    xlab("Time")+
    ylab("Volume (bld. USD)")
  
  p
  grid.arrange(q,p,heights = c(4/5,1/5))
  </code></pre>
  
  <p><a href="https://imgur.com/a/mQEP3ZG" rel="nofollow noreferrer">https://imgur.com/a/mQEP3ZG</a></p>
  
  <p>The output of <code>dput(head(bitc,20))</code>:</p>
  
  <pre><code>bitc &lt;- structure(list(Date = structure(c(15823, 15824, 15825, 15826, 
  15827, 15828, 15829, 15830, 15831, 15832, 15833, 15834, 15835, 
  15836, 15837, 15838, 15839, 15840, 15841, 15842), class = "Date"), 
      Open = c(135.3, 134.44, 144, 139, 116.38, 106.25, 98.1, 112.9, 
      115.98, 112.25, 109.6, 113.2, 112.8, 117.7, 115.64, 114.82, 
      117.98, 111.4, 114.22, 118.21), High = c(135.98, 147.49, 
      146.93, 139.89, 125.6, 108.13, 115, 118.8, 124.66, 113.44, 
      115.78, 113.46, 122, 118.68, 117.45, 118.7, 119.8, 115.81, 
      118.76, 125.3), Low = c(132.1, 134, 134.05, 107.72, 92.28, 
      79.1, 92.5, 107.14, 106.64, 97.7, 109.6, 109.26, 111.55, 
      113.01, 113.43, 114.5, 110.25, 103.5, 112.2, 116.57), Close = c(0.13421, 
      0.14454, 0.139, 0.11699, 0.10521, 0.09775, 0.1125, 0.11591, 
      0.1123, 0.1115, 0.11357, 0.11267, 0.1172, 0.11524, 0.115, 
      0.11798, 0.1115, 0.11422, 0.11876, 0.12301), Volume = c(0, 
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
      Market_Cap = c(1.488566728, 1.603768865, 1.542813125, 1.298954594, 
      1.168517495, 1.085995169, 1.250316563, 1.288693176, 1.24902306, 
      1.2405936, 1.264049202, 1.254535382, 1.30547908, 1.284207489, 
      1.281982625, 1.315710011, 1.243874488, 1.274623813, 1.325726787, 
      1.373723882)), .Names = c("Date", "Open", "High", "Low", 
  "Close", "Volume", "Market_Cap"), row.names = c(NA, 20L), class = "data.frame")
  </code></pre>