ggplot多行固定比例

时间:2013-01-11 15:53:32

标签: r plot scale ggplot2

我有两组时间序列数据。 我想在一个情节中绘制它们。 此外,绘图应该是y轴的固定比例,从-3到15,并显示a和b,其中x轴是时间和日期,y轴是v1。

数据a:              TimeAndDate v1

1  2007-09-30 00:00:00 0.3460000
2  2007-09-30 01:00:00 0.1660000
3  2007-09-30 02:00:00 0.2066667
4  2007-09-30 03:00:00 0.2956667
5  2007-09-30 04:00:00 0.2220000
6  2007-09-30 05:00:00 0.6090000
7  2007-09-30 06:00:00 0.8480000
8  2007-09-30 07:00:00 1.1986667
9  2007-09-30 08:00:00 1.4696667
10 2007-09-30 09:00:00 1.6280000

数据b:              TimeAndDate v1

1  2007-09-29 00:00:00 -1.3250000
2  2007-09-29 01:00:00 -0.4670000
3  2007-09-29 02:00:00 -0.4426667
4  2007-09-29 03:00:00 -0.6396667
5  2007-09-29 04:00:00 -0.7843333
6  2007-09-29 05:00:00 -0.5563333
7  2007-09-29 06:00:00 -0.7276667
8  2007-09-29 07:00:00 -0.6260000
9  2007-09-29 08:00:00 -0.7290000
10 2007-09-29 09:00:00  0.4976667

预期结果:

plot

我已将数据合并为代码:

 temp <- data.frame(HMn29_29$avg_val,HMn28_29$avg_val,HMn29_29$h)


   HMn29_29.avg_val HMn28_29.avg_val          HMn29_29.h
1        -1.3250000       -1.9225000 2007-09-29 00:00:00
2        -0.4670000       -1.6676667 2007-09-29 01:00:00
3        -0.4426667       -1.5820000 2007-09-29 02:00:00
4        -0.6396667       -1.5716667 2007-09-29 03:00:00
5        -0.7843333       -1.8290000 2007-09-29 04:00:00
6        -0.5563333       -1.6153333 2007-09-29 05:00:00
7        -0.7276667       -1.8093333 2007-09-29 06:00:00
8        -0.6260000       -1.9303333 2007-09-29 07:00:00
9        -0.7290000       -1.3980000 2007-09-29 08:00:00
10        0.4976667        1.2270000 2007-09-29 09:00:00

情节代码:

ggplot(temp, aes( HMn29_29.h)) + 
+     geom_line(aes(y = HMn28_29.avg_val, colour = "var0")) + 
+     geom_line(aes(y = HMn29_29.avg_val, colour = "var1"))

如何将y轴固定为-3到15?

2 个答案:

答案 0 :(得分:1)

我正在黑暗中捅你想要的东西,所以你去了:

我将数据集合并为一个数据框(df):

date time dose data
2007-09-30 00:00:00 0.3460000 A
2007-09-30 01:00:00 0.1660000 A
2007-09-30 02:00:00 0.2066667 A
2007-09-30 03:00:00 0.2956667 A
2007-09-30 04:00:00 0.2220000 A
2007-09-30 05:00:00 0.6090000 A
2007-09-30 06:00:00 0.8480000 A
2007-09-30 07:00:00 1.1986667 A
2007-09-30 08:00:00 1.4696667 A
2007-09-30 09:00:00 1.6280000 A
2007-09-29 00:00:00 -1.3250000 B
2007-09-29 01:00:00 -0.4670000 B
2007-09-29 02:00:00 -0.4426667 B
2007-09-29 03:00:00 -0.6396667 B
2007-09-29 04:00:00 -0.7843333 B
2007-09-29 05:00:00 -0.5563333 B
2007-09-29 06:00:00 -0.7276667 B
2007-09-29 07:00:00 -0.6260000 B
2007-09-29 08:00:00 -0.7290000 B
2007-09-29 09:00:00  0.4976667 B

然后使用以下代码绘制它:

ggplot(data=df, aes(x=time, y=dose, group=data, colour=data)) + geom_line() + geom_point()

这会产生: enter image description here

如果您想更改轴上的范围,您需要使用xlim()ylim()

答案 1 :(得分:1)

将表格放在一个表格中:

 temp <- data.frame(HMn29_29$avg_val,HMn28_29$avg_val,HMn29_29$h)

绘图:

temp <- data.frame(HMn29_29$avg_val,HMn28_29$avg_val,HMn29_29$h)
ggplot(temp, aes( HMn29_29.h)) + ylim(-2,13)+
     geom_line(aes(y = HMn28_29.avg_val, colour = "Temprature")) + 
    geom_line(aes(y = HMn29_29.avg_val, colour = "TempratureA"))+ opts(title = 'chart_title')

结果:

enter image description here