两个图相同的x轴,两个y轴-如何在网格中排列图

时间:2018-12-28 13:22:02

标签: r ggplot2

我正在尝试使用ggplot2gridExtra软件包制作类似this one的图形,但是我的两个图在另一个图的下方未正确对齐。我的数据集是来自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://imgur.com/a/mQEP3ZG

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

1 个答案:

答案 0 :(得分:0)

我认为您已经做得很好。我建议您尝试使用cowplot程序包,特别是功能plot_grid来将多个图表很好地排列到一个网格中。您可以找到软件包here的小插图。

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

使用您提供的数据和代码,以上plot_grid给出了以下信息:

enter image description here

如果需要交互式绘图,请尝试使用plotly软件包。它需要一个“ ggplot”并尝试使其具有交互性-试试看:

library(plotly)
ggplotly(q)

有关plotly的更多信息:on CRAN pageits main web pagethis book。可能会给您在ASE的同事和教授留下深刻的印象;)继续做好这份出色的工作和“ Sarbatori Fericite!”。