在单页上放置多个ggplot图时,日期标签会重叠

Abi*_*iel 3 r ggplot2

我试图使用gridExtra包的arrange()函数在页面上放置多个ggplot2时间序列图.不幸的是,我发现x轴标签被推到了一起; 虽然我的图表只占用了1/4的页面,但该图表似乎将相同数量的x轴标签作为整页图表.有一个更好的方法吗?我宁愿不必手动设置任何点,因为我将处理大量跨越不同日期范围并具有不同频率的图表.

以下是一些复制问题的示例代码.

dfm <- data.frame(index=seq(from=as.Date("2000-01-01"), length.out=100, by="year"), 
    x1=rnorm(100), 
    x2=rnorm(100))
mydata <- melt(dfm, id="index")

pdf("test.pdf")
plot1 <- ggplot(mydata, aes(index, value, color=variable))+geom_line()
plot2 <- ggplot(mydata, aes(index, value, color=variable))+geom_line()
plot3 <- ggplot(mydata, aes(index, value, color=variable))+geom_line()
plot4 <- ggplot(mydata, aes(index, value, color=variable))+geom_line()
arrange(plot1, plot2, plot3, plot4, ncol=2, nrow=2)
dev.off()
Run Code Online (Sandbox Code Playgroud)

ape*_*ape 5

或者旋转轴标签

+ opts(axis.text.x=theme_text(angle=45, hjust=1))
Run Code Online (Sandbox Code Playgroud)

请注意,opts在当前版本的ggplot2中已弃用.此功能已移至theme():

+ theme(axis.text.x = element_text(angle = 45, hjust = 1))
Run Code Online (Sandbox Code Playgroud)

或稀释x轴

+scale_x_datetime(major = "10 years")
Run Code Online (Sandbox Code Playgroud)

为了自动移动标签,我认为arrange()函数需要摆弄(虽然我不知道如何).