0

I am following this: Plot with conditional colors based on values in R

I have the following code:

stripchart(round(df$pnl_act / round_to, 0) * round_to, 
           xlim=c(-2500,2500), 
           pch = 20, 
           method = "stack",
           xaxt = "n",
           main = paste("Act", pp1[[1]], pp2[[1]], pp3[[1]],threshold_vector[1], sep = "-"),
           col = ifelse((round(df$pnl_act / round_to, 0) * round_to) > 0, "red", "green")
           )

However, all my colors are green.

When I run this:

(round(df$pnl_act / round_to, 0) * round_to) > 0

I get:

 [1] FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE  TRUE FALSE
[20] FALSE

Any ideas where I went wrong?

Edit:

Here is the vector of data:

df$pnl_act

Output:

  [1]    0 -220 -270  -50  190 -210   90  170   50   60 -390  -50   40  410  -10 -180 -170  230 -100 -1000
rollback
  • 93
  • 4

1 Answers1

1

I am not sure if you can do such plotting with ifelse on stripchart. Here is an option plotting two stripcharts on top of each other with different colors.

x1 <- x[x > 0]
x2 <- x[x <= 0]
stripchart(x1, col = 'green', xlim = range(x))
stripchart(x2, col = 'red', add = TRUE)

enter image description here data

x <- c(0, -220, -270, -50, 190, -210, 90, 170, 50, 60, -390, -50, 
40, 410, -10, -180, -170, 230, -100, -1000)

I have simplified the data and removed extra details from stripchart code just to explain the concept. You can add them as per your requirement.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213