将R中的多个图对齐,使用相同的x轴

时间:2015-12-30 18:23:08

标签: r plot ggplot2 time-series

我想绘制多个时间序列数字。我希望它们在同一个x轴上对齐。

然而,由于不同地块中的y数量不同,很难将它们简单地绘制在一起。 Y标签占用不同的空间。

是否有其他方法可以对齐x轴。

var arr = {
  "id": "123",
   "members": [
     { "id": 1, "name": "Andrew" },
     { "id": 2, "name": "Jim" }
   ]
};
 console.log( arr.members.reduce( function(x,y){ return "\"" + x.name + "\", \"" + y.name + "\"" } ) );

enter image description here

1 个答案:

答案 0 :(得分:1)

如果您重塑数据并使用facet_wrap,它应该可以满足您的要求。

library(reshape2)
library(ggplot2)
testdata<-as.data.frame(cbind(x=1:100, y1=rnorm(50), y2=100000*rnorm(50)))
testdata.melt<- melt(testdata, id.var = 'x')
ggplot(testdata.melt, aes(x = x, y = value, group = variable)) +
    geom_line() +
    facet_wrap(~ variable, ncol = 1, scales = "free_y")

结果图是: enter image description here