-1

I have a matrix X, which has 5 columns and 3825 rows (accounting for days). The values are either 0, 1 or NA.

  station1  station2    station3    station4    station5
     0          0          0            0          0
     1          0          0            0          0
     1          1          0            0          0
     0          1          0            0          
     0          0          0            0          

I want to create a horizontal barchart that will have length-x axis 3825 and 6 bars, one for each column. If the value in the cell is 1 then I want it to be red, if not I want it to be black. I do not want to sum up all the 1s in each column and present me the overall sum. I want to do that in order to have 5 horizontal bars with colors red and black, to be easier to check if there is a day-row when the 1s are present to more than 1 station.

I appreciate any help, I have been searching for days for the answer. I thought about:

barplot((c(X[,1],X[,2],X[,3],X[,4],X[,5]),nr=5), width=1, space=1,main="Heatwaves Dates", beside=FALSE, xlab="Days",  ylab="Stations",  ifelse(X==1, "red","black"), horiz=TRUE, xlim=c(1,3825),xpd=TRUE,border=NA,  names.arg=1:5, cex.names=0.75, las=1)

but apparently it doesn't work.

989
  • 12,579
  • 5
  • 31
  • 53

1 Answers1

0

You seem to be looking for a heatmap, rather than a stacked barplot.

set.seed(1)
X <- matrix(as.numeric(runif(3825*5)<0.4),ncol=5,
  dimnames=list(NULL,paste0("station",1:5)))
heatmap(t(X),Rowv=NA,Colv=NA,col=c("black","red"),labCol="")

heatmap

Use axis() to add labels to the x axis as desired. Look at ?heatmap for more information.

Stephan Kolassa
  • 7,953
  • 2
  • 28
  • 48