series' value (R ggplot)

I've got a time-series data frame with pressure readings taken at regular intervals.

                  time   pressure        diff
1 2014-09-09 09:12:29  1.6191598  0.00000000
2 2014-09-09 09:12:28  3.0137784 -0.07668387
3 2014-09-09 09:12:27  1.1958183  0.58693260
4 2014-09-09 09:12:26  2.2803681  1.07774954
5 2014-09-09 09:12:25 -0.7614310 -0.17864232
6 2014-09-09 09:12:24  0.9914106 -0.70121973

I can easily make a line plot of the pressure using ggplot2. But below this line plot, I'd like to have a horizontal bar where the fill colour depends on the pressure differential between two consecutive samples (df field diff ).
For example, the bar would be white at times where the pressure differential is zero (ie pressure has not changed bewteen two consecutive samples). The fill colour would go towards a deeper shade of (say) red as the differential increases positively, and blue as it increases in the negative values.

generate sample data:

 dfTimeSeries <- data.frame(time = Sys.time()-seq(1:10), 
                            pressure = rnorm(10,1), 
                            diff = c(0,diff(dfTimeSeries$pressure)))

First part of the plot

 ggplot(data = dfTimeSeries)+
     geom_line(aes(x=time, y=pressure))

How can I code this horizontal bar that would span along the entire x (time) axis and whose fill colour would vary based on the on the diff field of my df for the corresponding timestamp?


I ended up putting geom_rect objects side-to-side, one rectangle for each value of diff . Here's the basic concept:

ggplot(data = dfTimeSeries)+
  geom_line(aes(x=time, y=pressure))+
  geom_rect(aes(xmax=time+.5, xmin=time-.5,ymax=-3,ymin=-2, fill=diff))

I'd need to to adjust the colour scale, fit the bars in a different graph, and do a bit more cosmetic arrangmeents, etc.

链接地址: http://www.djcxy.com/p/86152.html

上一篇: 控制系列线的颜色和

下一篇: 系列'值(R ggplot)