0
votes

I have a data frame that looks like this

head(data1, 30)
      date                cur.gas
1  2015-01-01 00:00:45      RD
2  2015-01-01 00:02:45      RD
3  2015-01-01 00:04:45      RD
4  2015-01-01 00:06:45      RD
5  2015-01-01 00:08:45      RD
6  2015-01-01 00:10:45      RD
7  2015-01-01 00:12:45      RD
8  2015-01-01 00:14:45      RD
9  2015-01-01 00:16:45      RD
10 2015-01-01 00:18:45      RD
11 2015-01-01 00:20:45      RD
12 2015-01-01 00:22:45      RD
13 2015-01-01 00:24:45      RD
14 2015-01-01 00:26:45      RD
15 2015-01-01 00:28:45      RD
16 2015-01-01 00:30:45      RD
17 2015-01-01 00:40:45      BL
18 2015-01-01 00:42:45      BL
19 2015-01-01 00:44:45      BL
20 2015-01-01 00:46:45      BL
21 2015-01-01 00:48:45      BL
22 2015-01-01 00:50:45      BL
23 2015-01-01 00:52:45      BL
24 2015-01-01 00:54:45      BL
25 2015-01-01 00:56:45      BL
26 2015-01-01 00:58:45      BL
27 2015-01-01 01:00:45      BL
28 2015-01-01 01:46:45      RD
29 2015-01-01 01:50:45      RD
30 2015-01-01 01:52:45      RD

Where the cur.gas column indicated which line (Red (RD) or blue (BL)) is feeding the sample into the analyser. I want to create horizontal bar plot from this data that would show me the colour corresponding to the current line (red and blue).

I have tried using ggplot:

ggplot(data=data1, aes(x=0.1, y = date, col=cur.gas)) +
geom_bar(stat = "identity", width = 0.1) + 
coord_flip() +
scale_fill_manual(values = c("red","blue")) +
theme(text = element_text(size = 10), axis.title = element_blank(), axis.text  = element_blank(),
axis.ticks = element_blank())

And the output plot looks like this: enter image description here

Which looks horible, and the colours are wrong (the legend gives red colour for BL and blue for RD).

Then I tried the barplot function:

attach(data1)
barplot(date,names.arg=cur.gas, horiz = TRUE, col = c("blue", "red"), legend = rownames(data1$cur.gas))

and I got this error:

Error in barplot.default(date, names.arg = cur.gas, horiz = TRUE, col = c("blue",  : 
'height' must be a vector or a matrix

I'm lost now. What should I do? Thanks a lot1

1
Ignoring the colours, it’s unclear what output you’re expecting.Konrad Rudolph
@KonradRudolph As I said above, all i want is a horizontal barplot that would show me which line colour is giving the data. So the date column is in 2 mins interval, and at each interval, there's a line feeding the sample into the analyser. The bar plot I expect would look like the one above, but with the right colour, and thinner, nothing else. I don't even need the axes and title, etc.Tung Linh
For example, in the plot you see above, the first blue part indicates that the "blue" line is giving the sample for many continuous time intervals, then the first red strip indicates that the "blue" line was switched out, and "red" line feeding in for that interval insteadTung Linh

1 Answers

1
votes

What barplot wants is INTERVALS.

data[,"end_date"] <- c(data[-1,1], as.POSIXct("2015-01-01 01:54:45")) # I deciced a last value arbitrarily
data[,"interval"] <- difftime(data$end_date, data$date)
difftime(data$date[1], tail(data$end_date, n=1), units="min") # total 114 min

barplot(matrix(data$interval, ncol=1), horiz=T, col=c(2,4)[data$cur.gas], axes=F)
axis(1, at=seq(0,114,10), las=2, cex.axis=0.8,  # If you don't need border, add border=NA
     labels=format(seq(data[1,1], tail(data$end_date, n=1), by="10 min"),"%H:%M:%S"))

But it is more easy to use geom_rect() (this code needs end_date)

ggplot(data) + 
  geom_rect(aes(xmin=date, xmax=end_date, ymin=0, ymax=1, fill=cur.gas), colour="black") +
  theme(axis.text.y=element_blank(), axis.ticks.y=element_blank()) + labs(x="time")
    # if you don't need border, delete colour="black".

enter image description here enter image description here