Conditional hatching with a vector in R

I am trying to create a simple barcode with negative and positive values ​​with my input as a vector. I would like the barcode to display positive values ​​colored in red and negative values ​​colored in blue. I understand that this problem is simple, but I cannot understand it.

Here is my vector:

x <- (1.9230769,  1.2961538,  0.2576923, -1.5500000, -1.3192308, 
0.2192308,  1.8346154, 1.6038462,  2.5653846,  4.1423077)

I tried to execute the code:

barplot(x, ylim=c(-8,8), if(x>0) {col="red"} else {col="blue"})

but i keep getting the error message

"In if (x> 0) {: the condition has a length> 1 and only the first element will be used"

How can I figure this out to run through the whole vector and conditionally display it with red and blue?

Thank,

Adam

+4
source share
1 answer

Using

barplot(x, ylim=c(-8,8), col=ifelse(x>0,"red","blue"))

col= , x ( ). . ifelse if, .

colored bar plot

+11

Source: https://habr.com/ru/post/1543803/


All Articles