grid.arrange与不同的geom

时间:2015-01-26 18:53:28

标签: r ggplot2 alignment

我有以下data.table

dput(debt_dt_rate)

结构(清单(日期=结构(c)(16436,16467,16495,16526, 16556,16587,16617,16648,16679,16709,16740,16770,16801, 16832,16861,16892,16922,16953,16983,17014,17045,17075, 17106,17136,17167,17198,17226,17257,17287,17318,17348, 17379,17410,17440,17471,17501,17532,17563,17591,17622, 17652,17683,17713,17744,17775,17805,17836,17866,17897, 17928,17956,17987,18017,18048,18078,18109,18140),class =“Date”),     Total_Debt = c(16172660.3118,24748954.9334,23715774.9026,     22544915.6204,20583505.96055,19421496.9112,18318361.2343,     17262811.6254,16212190.2747,15295490.48442,14384236.50862,     13477977.2242,12746335.1577,12037756.1513,11333824.934,     10635020.45834,9260367.4557,8783398.3268,8312347.9855,     7844790.8162,7380479.8311,6923488.47,6469970.2799,6019742.9158,     5572998.8237,5133697.9165,4697791.5315,3489736.7093,3064608.30743,     2642979.3009,2286415.98173,1932701.98031,1581687.70227,     1249068.0842,1067287.38269,891533.1488,794507.4624,770837.1244,     623061.3247,540349.6035,459360.2506,381820.06611,306118.81,     274015.62,243173.29,216503.66,191173.67,167105.64,144363.66,     126123.44,107968.85,89954.1,71885.78,54007.14,36203.98,     18496.74,870.97),Rate_Mean = c(0.0110326585539761,0.011139704241154,     0.0111045496197387,0.0110799499979222,0.0110200792552765,     0.0109863297513123,0.0109469058035287,0.0108992365197961,     0.0108447251211066,0.0108248555800818,0.0108016128809191,     0.0107744736783219,0.0107920692771933,0.0108095814095823,     0.0108281568258678,0.0108478293086854,0.0107745577785952,     0.0107768206207373,0.0107769026221056,0.0107755046611141,     0.0107725226565144,0.0107675612519358,0.0107601178954451,     0.0107497957604701,0.0107357400329724,0.0107178851092619,     0.0106943643928971,0.0103882090387314,0.0102945851834854,     0.0101671606455755,0.00999913829559724,0.00976470573348041,     0.00942093915460458,0.00897332454625535,0.00878256033052308,     0.00850164591444073,0.00839145872734851,07820781529019079,     0.00796867039948853,0.0076497930780956,0.00721357444443409,     0.00658777083030085,0.0056487661966019,0.00564702816594543,     0.00563125903878259,0.00570209428501116,0.00577458636487441,     0.00584775920390838,0.00592,0.00592,0.00592,0.005925,     0.00592,0.00592,0.00592,0.00592,0.00592),SD = c(0.00314422907412743,     0.00406659363726963,0.00406149664508202,0.00406149664508202,     0.00406149664508202,0.00412099822831836,0.00418682167404248,     0.00418682167404248,0.00409890501673759,0.00409890501673759,     0.00409890501673759,0.00404788594847631,0.00411927400844535,     0.00411927400844535,0.00411927400844535,0.00427007694656846,     0.00447495257356909,0.00447495257356909,0.00447495257356909,     0.00447495257356909,0.004563532402646,0.004563532402646,     0.004563532402646,0.004563532402646,0.00463723035649911,     0.00463723035649911,0.00463723035649911,0.00469749071139946,     0.00469749071139946,0.0048184714095252,0.0048184714095252,     0.0048184714095252,0.00442812854438814,0.00413812865104072,     0.00413812865104072,0.00387755320635398,0.0042538274848789,     0.0042538274848789,0.0042538274848789,0.0042538274848789,     0.00453503518617237,0.00453503518617237,0.00370776899977794,     0.00370776899977794,0.00153795717809914,0.00153795717809914,     0.00153795717809914,0.00147265774638916,0,0,0,0,0,     0,0,0,NA)),row.names = c(NA,-57L),class = c(“data.table”, “data.frame”),. Name = c(“Date”,“Total_Debt”,“Rate_Mean”, “SD”),. internal.selfref =)

我希望使用grid.arrange使用geom_area生成上面的一个图表,然后使用geom_bargeom_errorbar生成相同的x轴。

以下是代码:

rect1<- data.frame (xmin=as.Date("2015-02-01"), xmax=as.Date("2015-07-01"),     ymin=-Inf, ymax=Inf)
rect2 <- data.frame (xmin=as.Date("2015-07-01"), xmax=as.Date("2015-12-01"), ymin=-Inf, ymax=Inf)


g4 <- ggplot() + geom_bar(data=debt_dt_rate, aes(x=Date, y=Rate_Mean),stat="identity",alpha = 5/10) + geom_errorbar(aes(x=Date,ymax=Rate_Mean + SD,
                              ymin=Rate_Mean - SD),
                          position=position_dodge(1),
                          data=debt_dt_rate) + scale_x_date(breaks = date_breaks("3 months"),labels=date_format("%b-%Y")) +  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +  scale_y_continuous("Interest Rate (%)",label=percent) + 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="red")+ theme(plot.margin = unit(c(-1,0.5,0.5,0.5), "lines"))

g5 <- ggplot() + geom_area(data=debt_dt_rate, aes(Date, Total_Debt),alpha = 5/10) + scale_y_continuous("Total Debt (R$)",label=dollar) +scale_x_date(breaks = date_breaks("3 months")) + theme(axis.text.x = element_text(angle = 45, hjust = 1)) +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="red")+ 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<- ggplot_gtable(ggplot_build(g4))
gp2<- ggplot_gtable(ggplot_build(g5))
maxWidth = unit.pmax(gp1$widths[2:3], gp2$widths[2:3])
gp1$widths[2:3] <- maxWidth
gp2$widths[2:3] <- maxWidth
grid.arrange(gp2, gp1)

当我从g5 chunk抑制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"))时,x轴在上图和下图中都会对齐。然而,他们之间有一个不方便的空间,我想摆脱它。

感谢您对此事的看法。

THX!

0 个答案:

没有答案