ggplot用于具有不同行数的数据帧

时间:2015-03-02 20:23:41

标签: r ggplot2

我想在一个具有不同行数的图中绘制三个数据帧。我怎么能在R中做到这一点。我可以绘制具有相同行数的多个数据帧,但不能。

如果想要一些数据。这是

 selu            
 47.05770
44.56958
45.83640
54.03050
51.02456
42.87440
47.90603
48.32686
45.52641
50.60718
52.70967
48.82412
50.42237
50.13677
49.09525
53.59709
51.51466
50.36027
50.87862
49.93741


 selm
 41.66651
41.31582
42.03833
39.62356
38.36130
36.48573
42.38672
35.25409
32.86074
45.82076
42.23158
44.24341


sell
42.64645
45.15626
44.93606
46.84997
46.11966
48.22673
46.02169
46.22279
49.61320
46.91678
47.42474
44.53632
41.92584
43.34548
37.15877
40.92660
36.46766
49.43382
50.75494
50.99439
47.70022
44.02300
44.80945
48.05542
46.93999
46.18448
48.04409
46.97649
45.24175
42.72160
43.40993
49.23922

我想要一个如下图所示的图,但是这两个数据框的值都相同,但上面的data.frames没有相同数量的值。在那种情况下,我如何制作这样的情节

enter image description here

1 个答案:

答案 0 :(得分:1)

对于每个向量没有任何时间段,我们只是猜测,所以这里你去...

a <- read.table(textConnection("selu            
47.05770
44.56958
45.83640
54.03050
51.02456
42.87440
47.90603
48.32686
45.52641
50.60718
52.70967
48.82412
50.42237
50.13677
49.09525
53.59709
51.51466
50.36027
50.87862
49.93741"), header=T, stringsAsFactors=F)

b <- read.table(textConnection("selm
41.66651
41.31582
42.03833
39.62356
38.36130
36.48573
42.38672
35.25409
32.86074
45.82076
42.23158
44.24341"), header=T, stringsAsFactors=F)

c <- read.table(textConnection("sell
42.64645
45.15626
44.93606
46.84997
46.11966
48.22673
46.02169
46.22279
49.61320
46.91678
47.42474
44.53632
41.92584
43.34548
37.15877
40.92660
36.46766
49.43382
50.75494
50.99439
47.70022
44.02300
44.80945
48.05542
46.93999
46.18448
48.04409
46.97649
45.24175
42.72160
43.40993
49.23922"), header=T, stringsAsFactors=F)

names(a) <- names(b) <- names(c) <- "data"
a$index <- 1:nrow(a)
b$index <- 1:nrow(b)
c$index <- 1:nrow(c)
a$name <- rep("selu", nrow(a))
b$name <- rep("selm", nrow(b))
c$name <- rep("sell", nrow(c))

all <- rbind(a,b,c)

library("ggplot2")
ggplot(all) + geom_line(aes(x=index, y=data, color=name))

This is the result