Abruzzo Forte e Gentile
Abruzzo Forte e Gentile

Reputation: 14869

about barplot for data with negative values

I would like to plot a stacked bar chart for a matrix of data containing both positive and negative values.

I used the code below and I was expecting all positive values stacked above x-axis and all negative values stacked below x-axis but this is not the case.

  test<-matrix(c(1,-2,-3,4,5,-6),ncol=2)
  colnames(test)=c("A","B")
  rownames(test)=c("x","y","z")
  barplot(test)

If I look at the help I can't find something about this specific topic.

Do I have to use some extra command or extra parameter to achieve that?

Upvotes: 2

Views: 8967

Answers (1)

xb.
xb.

Reputation: 1677

A quick (but not the best) workaround:

test1 <- test2 <- test
test1[test1<0] <- 0
test2[test2>0] <- 0
myrange <- c(min(colSums(test2)),max(colSums(test1)))
barplot(test1,ylim=myrange)
barplot(test2,add=TRUE,ylim=rev(myrange))

enter image description here

Upvotes: 4

Related Questions