1

I am trying to make a graph like this one, using ggplot2 and gridExtra packages, but my two plots are not properly aligned one under the other. My dataset is the historical data for bitcoin from coinmarketcap.

library(ggplot2)
library(scales)
library(gridExtra)

bitc$Date <- as.Date(bitc$Date,"%m/%d/%Y")
bitc$Close <- bitc$Close/1000
bitc$Market_Cap <- bitc$Market_Cap/1000000000
bitc$Volume <- bitc$Volume/1000000000


q <- 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 <- q + geom_line(aes(y=bitc$Market_Cap/10,colour="Market_Cap"))
q <- q + scale_y_continuous(sec.axis = sec_axis(~.*10, name = "Market Cap (bld. USD)"))
q

p <- 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))

https://i.stack.imgur.com/Wohxa.jpg

The output of dput(head(bitc,20)):

bitc <- 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")
Valentin_Ștefan
  • 6,130
  • 2
  • 45
  • 68

1 Answers1

0

I think you did a great job already. I suggest you to try out the cowplot package, specifically the function plot_grid to nicely arrange multiple plots into a grid. You can find vignettes for the package here.

library(cowplot)
plot_grid(q,p, align = "hv", ncol = 1, rel_heights = c(4/5, 1/5))

With the data and the code that you provided, the above plot_grid gives this:

enter image description here

By the way, if you want interactive plots, try out the plotly package. It takes a "ggplot" and attempts to make it interactive - try this out:

library(plotly)
ggplotly(q)

More about plotly: on CRAN page, its main web page or this book. Could be nice to impress your colleagues and professors at ASE ;) Keep up the nice work & "Sarbatori Fericite!".

Valentin_Ștefan
  • 6,130
  • 2
  • 45
  • 68